Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/lib/libspl/timestamp.c
48378 views
1
// SPDX-License-Identifier: CDDL-1.0
2
/*
3
* CDDL HEADER START
4
*
5
* The contents of this file are subject to the terms of the
6
* Common Development and Distribution License (the "License").
7
* You may not use this file except in compliance with the License.
8
*
9
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10
* or https://opensource.org/licenses/CDDL-1.0.
11
* See the License for the specific language governing permissions
12
* and limitations under the License.
13
*
14
* When distributing Covered Code, include this CDDL HEADER in each
15
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16
* If applicable, add the following below this CDDL HEADER, with the
17
* fields enclosed by brackets "[]" replaced with your own identifying
18
* information: Portions Copyright [yyyy] [name of copyright owner]
19
*
20
* CDDL HEADER END
21
*/
22
/*
23
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24
* Use is subject to license terms.
25
*/
26
27
#include <stdio.h>
28
#include <time.h>
29
#include <langinfo.h>
30
#include "statcommon.h"
31
32
#ifndef _DATE_FMT
33
#ifdef D_T_FMT
34
#define _DATE_FMT D_T_FMT
35
#else /* D_T_FMT */
36
#define _DATE_FMT "%+"
37
#endif /* !D_T_FMT */
38
#endif /* _DATE_FMT */
39
40
/*
41
* Print timestamp as decimal reprentation of time_t value (-T u was specified)
42
* or in date(1) format (-T d was specified).
43
*/
44
void
45
print_timestamp(uint_t timestamp_fmt)
46
{
47
time_t t = time(NULL);
48
static const char *fmt = NULL;
49
50
/* We only need to retrieve this once per invocation */
51
if (fmt == NULL)
52
fmt = nl_langinfo(_DATE_FMT);
53
54
if (timestamp_fmt == UDATE) {
55
(void) printf("%lld\n", (longlong_t)t);
56
} else if (timestamp_fmt == DDATE) {
57
char dstr[64];
58
struct tm tm;
59
int len;
60
61
len = strftime(dstr, sizeof (dstr), fmt, localtime_r(&t, &tm));
62
if (len > 0)
63
(void) printf("%s\n", dstr);
64
}
65
}
66
67
/*
68
* Return timestamp as decimal reprentation (in string) of time_t
69
* value (-T u was specified) or in date(1) format (-T d was specified).
70
*/
71
void
72
get_timestamp(uint_t timestamp_fmt, char *buf, int len)
73
{
74
time_t t = time(NULL);
75
static const char *fmt = NULL;
76
77
/* We only need to retrieve this once per invocation */
78
if (fmt == NULL)
79
fmt = nl_langinfo(_DATE_FMT);
80
81
if (timestamp_fmt == UDATE) {
82
(void) snprintf(buf, len, "%lld", (longlong_t)t);
83
} else if (timestamp_fmt == DDATE) {
84
struct tm tm;
85
strftime(buf, len, fmt, localtime_r(&t, &tm));
86
}
87
}
88
89
/*
90
* Format the provided time stamp to human readable format
91
*/
92
void
93
format_timestamp(time_t t, char *buf, int len)
94
{
95
struct tm tm;
96
static const char *fmt = NULL;
97
98
if (t == 0) {
99
snprintf(buf, len, "-");
100
return;
101
}
102
103
/* We only need to retrieve this once per invocation */
104
if (fmt == NULL)
105
fmt = nl_langinfo(_DATE_FMT);
106
strftime(buf, len, fmt, localtime_r(&t, &tm));
107
}
108
109