-
Notifications
You must be signed in to change notification settings - Fork 256
/
btime.c
444 lines (375 loc) · 12.3 KB
/
btime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* BAREOS floating point time and date routines -- John Walker
*
* Later double precision integer time/date routines -- Kern Sibbald
*
*/
/* Concerning times. There are a number of differnt time standards
* in BAREOS (fdate_t, ftime_t, time_t (Unix standard), btime_t, and
* utime_t). fdate_t and ftime_t are deprecated and should no longer
* be used, and in general, Unix time time_t should no longer be used,
* it is being phased out.
*
* Epoch is the base of Unix time in seconds (time_t, ...)
* and is 1 Jan 1970 at 0:0 UTC
*
* The major two times that should be left are:
* btime_t (64 bit integer in microseconds base Epoch)
* utime_t (64 bit integer in seconds base Epoch)
*/
#include "bareos.h"
#include <math.h>
/* Formatted time for user display: dd-Mon-yyyy hh:mm */
char *bstrftime(char *dt, int maxlen, utime_t utime)
{
time_t time = (time_t)utime;
struct tm tm;
/* ***FIXME**** the format and localtime_r() should be user configurable */
(void)localtime_r(&time, &tm);
strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
return dt;
}
/* Formatted time for user display: dd-Mon-yyyy hh:mm:ss */
char *bstrftimes(char *dt, int maxlen, utime_t utime)
{
time_t time = (time_t)utime;
struct tm tm;
/* ***FIXME**** the format and localtime_r() should be user configurable */
(void)localtime_r(&time, &tm);
strftime(dt, maxlen, "%d-%b-%Y %H:%M:%S", &tm);
return dt;
}
/* Formatted time for user display: dd-Mon hh:mm */
char *bstrftime_ny(char *dt, int maxlen, utime_t utime)
{
time_t time = (time_t)utime;
struct tm tm;
/* ***FIXME**** the format and localtime_r() should be user configurable */
(void)localtime_r(&time, &tm);
strftime(dt, maxlen, "%d-%b %H:%M", &tm);
return dt;
}
/* Formatted time for user display with weekday: weekday dd-Mon hh:mm */
char *bstrftime_wd(char *dt, int maxlen, utime_t utime)
{
time_t time = (time_t)utime;
struct tm tm;
/* ***FIXME**** the format and localtime_r() should be user configurable */
(void)localtime_r(&time, &tm);
strftime(dt, maxlen, "%a %d-%b %H:%M", &tm);
return dt;
}
/* Formatted time for user display: dd-Mon-yy hh:mm (no century) */
char *bstrftime_nc(char *dt, int maxlen, utime_t utime)
{
time_t time = (time_t)utime;
struct tm tm;
char *p, *q;
/* ***FIXME**** the format and localtime_r() should be user configurable */
(void)localtime_r(&time, &tm);
/* NOTE! since the compiler complains about %y, I use %y and cut the century */
strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
/* overlay the century */
p = dt+7;
q = dt+9;
while (*q) {
*p++ = *q++;
}
*p = 0;
return dt;
}
/* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
char *bstrutime(char *dt, int maxlen, utime_t utime)
{
time_t time = (time_t)utime;
struct tm tm;
(void)localtime_r(&time, &tm);
strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
return dt;
}
/* Convert standard time string yyyy-mm-dd hh:mm:ss to Unix time */
utime_t str_to_utime(const char *str)
{
struct tm tm;
time_t time;
/* Check for bad argument */
if (!str || *str == 0) {
return 0;
}
if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
return 0;
}
if (tm.tm_mon > 0) {
tm.tm_mon--;
} else {
return 0;
}
if (tm.tm_year >= 1900) {
tm.tm_year -= 1900;
} else {
return 0;
}
tm.tm_wday = tm.tm_yday = 0;
tm.tm_isdst = -1;
time = mktime(&tm);
if (time == -1) {
time = 0;
}
return (utime_t)time;
}
/*
* BAREOS's time (btime_t) is an unsigned 64 bit integer that contains
* the number of microseconds since Epoch Time (1 Jan 1970) UTC.
*/
btime_t get_current_btime()
{
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0) {
tv.tv_sec = (long)time(NULL); /* fall back to old method */
tv.tv_usec = 0;
}
return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
}
/* Convert btime to Unix time */
time_t btime_to_unix(btime_t bt)
{
return (time_t)(bt/1000000);
}
/* Convert btime to utime */
utime_t btime_to_utime(btime_t bt)
{
return (utime_t)(bt/1000000);
}
/*
* Return the week of the month, base 0 (wom)
* given tm_mday and tm_wday. Value returned
* can be from 0 to 4 => week1, ... week5
*/
int tm_wom(int mday, int wday)
{
int fs; /* first sunday */
fs = (mday%7) - wday;
if (fs <= 0) {
fs += 7;
}
if (mday <= fs) {
// Dmsg3(100, "mday=%d wom=0 wday=%d <= fs=%d\n", mday, wday, fs);
return 0;
}
int wom = 1 + (mday - fs - 1) / 7;
// Dmsg4(100, "mday=%d wom=%d wday=%d fs=%d\n", mday, wom, wday, fs);
return wom;
}
/*
* Given a Unix date return the week of the year.
* The returned value can be 0-53. Officially
* the weeks are numbered from 1 to 53 where week1
* is the week in which the first Thursday of the
* year occurs (alternatively, the week which contains
* the 4th of January). We return 0, if the week of the
* year does not fall in the current year.
*/
int tm_woy(time_t stime)
{
int woy, fty, tm_yday;
time_t time4;
struct tm tm;
memset(&tm, 0, sizeof(struct tm));
(void)localtime_r(&stime, &tm);
tm_yday = tm.tm_yday;
tm.tm_mon = 0;
tm.tm_mday = 4;
tm.tm_isdst = 0; /* 4 Jan is not DST */
time4 = mktime(&tm);
(void)localtime_r(&time4, &tm);
fty = 1 - tm.tm_wday;
if (fty <= 0) {
fty += 7;
}
woy = tm_yday - fty + 4;
if (woy < 0) {
return 0;
}
return 1 + woy / 7;
}
/* Deprecated. Do not use. */
void get_current_time(struct date_time *dt)
{
struct tm tm;
time_t now;
now = time(NULL);
(void)gmtime_r(&now, &tm);
Dmsg6(200, "m=%d d=%d y=%d h=%d m=%d s=%d\n", tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900,
tm.tm_hour, tm.tm_min, tm.tm_sec);
tm_encode(dt, &tm);
#ifdef DEBUG
Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
tm_decode(dt, &tm);
Dmsg6(200, "m=%d d=%d y=%d h=%d m=%d s=%d\n", tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900,
tm.tm_hour, tm.tm_min, tm.tm_sec);
#endif
}
/* date_encode -- Encode civil date as a Julian day number. */
/* Deprecated. Do not use. */
fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
{
/* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
int32_t a, b, m;
uint32_t y;
ASSERT(month < 13);
ASSERT(day > 0 && day < 32);
m = month;
y = year;
if (m <= 2) {
y--;
m += 12;
}
/* Determine whether date is in Julian or Gregorian calendar based on
canonical date of calendar reform. */
if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
b = 0;
} else {
a = ((int) (y / 100));
b = 2 - a + (a / 4);
}
return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
day + b - 1524.5);
}
/* time_encode -- Encode time from hours, minutes, and seconds
into a fraction of a day. */
/* Deprecated. Do not use. */
ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
float32_t second_fraction)
{
ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
second_fraction;
}
/* date_time_encode -- Set day number and fraction from date
and time. */
/* Deprecated. Do not use. */
void date_time_encode(struct date_time *dt,
uint32_t year, uint8_t month, uint8_t day,
uint8_t hour, uint8_t minute, uint8_t second,
float32_t second_fraction)
{
dt->julian_day_number = date_encode(year, month, day);
dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
}
/* date_decode -- Decode a Julian day number into civil date. */
/* Deprecated. Do not use. */
void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
uint8_t *day)
{
fdate_t z, f, a, alpha, b, c, d, e;
date += 0.5;
z = floor(date);
f = date - z;
if (z < 2299161.0) {
a = z;
} else {
alpha = floor((z - 1867216.25) / 36524.25);
a = z + 1 + alpha - floor(alpha / 4);
}
b = a + 1524;
c = floor((b - 122.1) / 365.25);
d = floor(365.25 * c);
e = floor((b - d) / 30.6001);
*day = (uint8_t) (b - d - floor(30.6001 * e) + f);
*month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
*year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
}
/* time_decode -- Decode a day fraction into civil time. */
/* Deprecated. Do not use. */
void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
uint8_t *second, float32_t *second_fraction)
{
uint32_t ij;
ij = (uint32_t) ((time - floor(time)) * 86400.0);
*hour = (uint8_t) (ij / 3600L);
*minute = (uint8_t) ((ij / 60L) % 60L);
*second = (uint8_t) (ij % 60L);
if (second_fraction != NULL) {
*second_fraction = (float32_t)(time - floor(time));
}
}
/* date_time_decode -- Decode a Julian day and day fraction
into civil date and time. */
/* Deprecated. Do not use. */
void date_time_decode(struct date_time *dt,
uint32_t *year, uint8_t *month, uint8_t *day,
uint8_t *hour, uint8_t *minute, uint8_t *second,
float32_t *second_fraction)
{
date_decode(dt->julian_day_number, year, month, day);
time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
}
/* tm_encode -- Encode a civil date and time from a tm structure
* to a Julian day and day fraction.
*/
/* Deprecated. Do not use. */
void tm_encode(struct date_time *dt,
struct tm *tm)
{
uint32_t year;
uint8_t month, day, hour, minute, second;
year = tm->tm_year + 1900;
month = tm->tm_mon + 1;
day = tm->tm_mday;
hour = tm->tm_hour;
minute = tm->tm_min;
second = tm->tm_sec;
dt->julian_day_number = date_encode(year, month, day);
dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
}
/* tm_decode -- Decode a Julian day and day fraction
into civil date and time in tm structure */
/* Deprecated. Do not use. */
void tm_decode(struct date_time *dt,
struct tm *tm)
{
uint32_t year;
uint8_t month, day, hour, minute, second;
date_decode(dt->julian_day_number, &year, &month, &day);
time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
tm->tm_year = year - 1900;
tm->tm_mon = month - 1;
tm->tm_mday = day;
tm->tm_hour = hour;
tm->tm_min = minute;
tm->tm_sec = second;
}
/* date_time_compare -- Compare two dates and times and return
the relationship as follows:
-1 dt1 < dt2
0 dt1 = dt2
1 dt1 > dt2
*/
/* Deprecated. Do not use. */
int date_time_compare(struct date_time *dt1, struct date_time *dt2)
{
if (dt1->julian_day_number == dt2->julian_day_number) {
if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
return 0;
}
return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
}
return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;
}