1 /*
2 * SysDB - src/core/time.c
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/time.h"
35 #include <time.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <string.h>
41 /*
42 * public API
43 */
45 /* 1 second (in nano-seconds) */
46 #define SEC 1000000000L
48 const sdb_time_t SDB_INTERVAL_YEAR = 3652425L * 24L * 60L * 60L * 100000L;
49 const sdb_time_t SDB_INTERVAL_MONTH = 30436875L * 24L * 60L * 60L * 1000L;
50 const sdb_time_t SDB_INTERVAL_DAY = 24L * 60L * 60L * SEC;
51 const sdb_time_t SDB_INTERVAL_HOUR = 60L * 60L * SEC;
52 const sdb_time_t SDB_INTERVAL_MINUTE = 60L * SEC;
53 const sdb_time_t SDB_INTERVAL_SECOND = SEC;
55 sdb_time_t
56 sdb_gettime(void)
57 {
58 struct timespec ts_now = { 0, 0 };
60 if (clock_gettime(CLOCK_REALTIME, &ts_now))
61 return 0;
62 return TIMESPEC_TO_SDB_TIME(ts_now);
63 } /* sdb_gettime */
65 int
66 sdb_sleep(sdb_time_t reg, sdb_time_t *rem)
67 {
68 struct timespec ts_reg, ts_rem = { 0, 0 };
69 int status;
71 ts_reg.tv_sec = (time_t)SDB_TIME_TO_SECS(reg);
72 ts_reg.tv_nsec = (long int)(reg % (sdb_time_t)1000000000);
74 status = nanosleep(&ts_reg, &ts_rem);
75 if (rem)
76 *rem = TIMESPEC_TO_SDB_TIME(ts_rem);
77 return status;
78 } /* sdb_sleep */
80 size_t
81 sdb_strftime(char *s, size_t len, sdb_time_t t)
82 {
83 char tmp[len];
84 time_t tstamp;
85 struct tm tm;
86 long tz;
88 memset(&tm, 0, sizeof(tm));
89 tstamp = (time_t)SDB_TIME_TO_SECS(t);
90 if (! localtime_r (&tstamp, &tm))
91 return 0;
93 if (! strftime(tmp, len, "%F %T", &tm))
94 return 0;
95 tmp[sizeof(tmp) - 1] = '\0';
97 tz = -timezone / 36;
98 if (tm.tm_isdst > 0)
99 tz += 100;
101 t %= SDB_INTERVAL_SECOND;
102 if (! t)
103 return snprintf(s, len, "%s %+05ld", tmp, tz);
104 return snprintf(s, len, "%s.%09ld %+05ld", tmp, t, tz);
105 } /* sdb_strftime */
107 size_t
108 sdb_strfinterval(char *s, size_t len, sdb_time_t interval)
109 {
110 size_t n = 0;
111 size_t i;
113 /* special case the optional fractional part for seconds */
114 bool have_seconds = 0;
116 struct {
117 sdb_time_t interval;
118 const char *suffix;
119 } specs[] = {
120 { SDB_INTERVAL_YEAR, "Y" },
121 { SDB_INTERVAL_MONTH, "M" },
122 { SDB_INTERVAL_DAY, "D" },
123 { SDB_INTERVAL_HOUR, "h" },
124 { SDB_INTERVAL_MINUTE, "m" },
125 { SDB_INTERVAL_SECOND, "" },
126 };
128 #define LEN (len > n ? len - n : 0)
129 for (i = 0; i < SDB_STATIC_ARRAY_LEN(specs); ++i) {
130 if (interval >= specs[i].interval) {
131 n += snprintf(s + n, LEN, "%"PRIsdbTIME"%s",
132 interval / specs[i].interval, specs[i].suffix);
133 interval %= specs[i].interval;
134 if (specs[i].interval == SDB_INTERVAL_SECOND)
135 have_seconds = 1;
136 }
137 }
139 if (interval) {
140 n += snprintf(s + n, LEN, ".%09"PRIsdbTIME, interval);
141 have_seconds = 1;
143 /* removing trailing zeroes */
144 if (n <= len)
145 while (s[n - 1] == '0')
146 s[n--] = '\0';
147 }
149 if (! n) {
150 n = snprintf(s, len, "0");
151 have_seconds = 1;
152 }
154 if (have_seconds)
155 n += snprintf(s + n, LEN, "s");
156 #undef LEN
158 if (len)
159 s[len - 1] = '\0';
160 return n;
161 } /* sdb_strfinterval */
163 sdb_time_t
164 sdb_strpunit(const char *s)
165 {
166 struct {
167 const char *s;
168 sdb_time_t unit;
169 } units[] = {
170 { "Y", SDB_INTERVAL_YEAR },
171 { "M", SDB_INTERVAL_MONTH },
172 { "D", SDB_INTERVAL_DAY },
173 { "h", SDB_INTERVAL_HOUR },
174 { "m", SDB_INTERVAL_MINUTE },
175 { "s", SDB_INTERVAL_SECOND },
176 { "ms", SDB_INTERVAL_SECOND / 1000L },
177 { "us", SDB_INTERVAL_SECOND / 1000000L },
178 { "ns", 1 },
179 };
181 size_t i;
183 for (i = 0; i < SDB_STATIC_ARRAY_LEN(units); ++i)
184 if (! strcmp(s, units[i].s))
185 return units[i].unit;
186 return 0;
187 } /* sdb_strpunit */
189 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */