/src/samba/lib/util/time_basic.c
Line | Count | Source |
1 | | /* |
2 | | * Unix SMB/CIFS implementation. |
3 | | * time handling functions |
4 | | * |
5 | | * Copyright (C) Andrew Tridgell 1992-2004 |
6 | | * Copyright (C) Stefan (metze) Metzmacher 2002 |
7 | | * Copyright (C) Jeremy Allison 2007 |
8 | | * Copyright (C) Andrew Bartlett 2011 |
9 | | * |
10 | | * This program is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU General Public License as published by |
12 | | * the Free Software Foundation; either version 3 of the License, or |
13 | | * (at your option) any later version. |
14 | | * |
15 | | * This program is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU General Public License |
21 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 | | */ |
23 | | |
24 | | #include "replace.h" |
25 | | #include "system/time.h" |
26 | | #include "lib/util/time_basic.h" |
27 | | |
28 | | /** |
29 | | a gettimeofday wrapper |
30 | | **/ |
31 | | _PUBLIC_ void GetTimeOfDay(struct timeval *tval) |
32 | 0 | { |
33 | 0 | #if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID) |
34 | 0 | gettimeofday(tval,NULL); |
35 | | #else |
36 | | gettimeofday(tval); |
37 | | #endif |
38 | 0 | } |
39 | | |
40 | | /**************************************************************************** |
41 | | Return the date and time as a string |
42 | | ****************************************************************************/ |
43 | | |
44 | | char *timeval_str_buf(const struct timeval *tp, bool rfc5424, bool hires, |
45 | | struct timeval_buf *dst) |
46 | 0 | { |
47 | 0 | time_t t; |
48 | 0 | struct tm *tm; |
49 | 0 | size_t len; |
50 | |
|
51 | 0 | t = (time_t)tp->tv_sec; |
52 | 0 | tm = localtime(&t); |
53 | |
|
54 | 0 | if (tm == NULL) { |
55 | 0 | if (hires) { |
56 | 0 | snprintf(dst->buf, sizeof(dst->buf), |
57 | 0 | "%ld.%06ld seconds since the Epoch", |
58 | 0 | (long)tp->tv_sec, (long)tp->tv_usec); |
59 | 0 | } else { |
60 | 0 | snprintf(dst->buf, sizeof(dst->buf), |
61 | 0 | "%ld seconds since the Epoch", (long)t); |
62 | 0 | } |
63 | 0 | return dst->buf; |
64 | 0 | } |
65 | | |
66 | 0 | len = snprintf(dst->buf, sizeof(dst->buf), |
67 | 0 | (rfc5424 ? |
68 | 0 | "%04d-%02d-%02dT%02d:%02d:%02d" : |
69 | 0 | "%04d/%02d/%02d %02d:%02d:%02d"), |
70 | 0 | 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, |
71 | 0 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
72 | |
|
73 | 0 | if ((rfc5424 || hires) && (len < sizeof(dst->buf))) { |
74 | 0 | len += snprintf(dst->buf + len, sizeof(dst->buf) - len, |
75 | 0 | ".%06ld", (long)tp->tv_usec); |
76 | 0 | } |
77 | |
|
78 | 0 | if (rfc5424 && (len < sizeof(dst->buf))) { |
79 | 0 | struct tm tm_utc, tm_local; |
80 | 0 | int offset; |
81 | |
|
82 | 0 | tm_local = *tm; |
83 | | /* It is reasonable to assume that if localtime() |
84 | | * worked above, then gmtime() should also work |
85 | | * without error. */ |
86 | 0 | tm_utc = *gmtime(&t); |
87 | |
|
88 | 0 | offset = (tm_local.tm_hour - tm_utc.tm_hour) * 60 + |
89 | 0 | (tm_local.tm_min - tm_utc.tm_min); |
90 | |
|
91 | 0 | snprintf(dst->buf + len, sizeof(dst->buf) - len, |
92 | 0 | "%c%02d:%02d", |
93 | 0 | (offset >=0 ? '+' : '-'), |
94 | 0 | abs(offset) / 60, |
95 | 0 | abs(offset) % 60); |
96 | 0 | } |
97 | |
|
98 | 0 | return dst->buf; |
99 | 0 | } |