summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2020-05-26 01:06:52 +0000
committerDamien Miller <djm@mindrot.org>2020-05-27 10:13:58 +1000
commit756c6f66aee83a5862a6f936a316f761532f3320 (patch)
tree7a838eb2a7ae4effdadb4798e56f713f10b8524b /misc.c
parent2a63ce5cd6d0e782783bf721462239b03757dd49 (diff)
upstream: add fmt_timeframe() (from bgpd) to format a time
interval in a human- friendly format. Switch copyright for this file from BSD to MIT to make it easier to add Henning's copyright for this function. ok markus@ OpenBSD-Commit-ID: 414a831c662df7e68893e5233e86f2cac081ccf9
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c71
1 files changed, 51 insertions, 20 deletions
diff --git a/misc.c b/misc.c
index 554ceb0b1..3e661ac77 100644
--- a/misc.c
+++ b/misc.c
@@ -1,29 +1,23 @@
1/* $OpenBSD: misc.c,v 1.147 2020/04/25 06:59:36 dtucker Exp $ */ 1/* $OpenBSD: misc.c,v 1.148 2020/05/26 01:06:52 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved. 4 * Copyright (c) 2005-2020 Damien Miller. All rights reserved.
5 * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
5 * 6 *
6 * Redistribution and use in source and binary forms, with or without 7 * Permission to use, copy, modify, and distribute this software for any
7 * modification, are permitted provided that the following conditions 8 * purpose with or without fee is hereby granted, provided that the above
8 * are met: 9 * copyright notice and this permission notice appear in all copies.
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 10 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 18 */
26 19
20
27#include "includes.h" 21#include "includes.h"
28 22
29#include <sys/types.h> 23#include <sys/types.h>
@@ -551,6 +545,43 @@ convtime(const char *s)
551 return total; 545 return total;
552} 546}
553 547
548#define TF_BUFS 8
549#define TF_LEN 9
550
551const char *
552fmt_timeframe(time_t t)
553{
554 char *buf;
555 static char tfbuf[TF_BUFS][TF_LEN]; /* ring buffer */
556 static int idx = 0;
557 unsigned int sec, min, hrs, day;
558 unsigned long long week;
559
560 buf = tfbuf[idx++];
561 if (idx == TF_BUFS)
562 idx = 0;
563
564 week = t;
565
566 sec = week % 60;
567 week /= 60;
568 min = week % 60;
569 week /= 60;
570 hrs = week % 24;
571 week /= 24;
572 day = week % 7;
573 week /= 7;
574
575 if (week > 0)
576 snprintf(buf, TF_LEN, "%02lluw%01ud%02uh", week, day, hrs);
577 else if (day > 0)
578 snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min);
579 else
580 snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec);
581
582 return (buf);
583}
584
554/* 585/*
555 * Returns a standardized host+port identifier string. 586 * Returns a standardized host+port identifier string.
556 * Caller must free returned string. 587 * Caller must free returned string.