2 * Bacula time and date routines -- John Walker
7 Copyright (C) 2000-2006 Kern Sibbald
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 version 2 as amended with additional clauses defined in the
12 file LICENSE in the main source directory.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 the file LICENSE for additional details.
22 /* Concerning times. There are a number of differnt time standards
23 * in Bacula (fdate_t, ftime_t, time_t (Unix standard), btime_t, and
24 * utime_t). fdate_t and ftime_t are deprecated and should no longer
25 * be used, and in general, Unix time time_t should no longer be used,
26 * it is being phased out.
28 * Epoch is the base of Unix time in seconds (time_t, ...)
29 * and is 1 Jan 1970 at 0:0 UTC
31 * The major two times that should be left are:
32 * btime_t (64 bit integer in microseconds base Epoch)
33 * utime_t (64 bit integer in seconds base Epoch)
39 /* Formatted time for user display: dd-Mon-yyyy hh:mm */
40 char *bstrftime(char *dt, int maxlen, utime_t tim)
42 time_t ttime = (time_t)tim;
45 /* ***FIXME**** the format and localtime_r() should be user configurable */
46 (void)localtime_r(&ttime, &tm);
47 strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
51 /* Formatted time for user display: dd-Mon-yyyy hh:mm:ss */
52 char *bstrftimes(char *dt, int maxlen, utime_t tim)
54 time_t ttime = (time_t)tim;
57 /* ***FIXME**** the format and localtime_r() should be user configurable */
58 (void)localtime_r(&ttime, &tm);
59 strftime(dt, maxlen, "%d-%b-%Y %H:%M:%S", &tm);
64 /* Formatted time for user display: dd-Mon hh:mm */
65 char *bstrftime_ny(char *dt, int maxlen, utime_t tim)
67 time_t ttime = (time_t)tim;
70 /* ***FIXME**** the format and localtime_r() should be user configurable */
71 (void)localtime_r(&ttime, &tm);
72 strftime(dt, maxlen, "%d-%b %H:%M", &tm);
77 /* Formatted time for user display: dd-Mon-yy hh:mm (no century) */
78 char *bstrftime_nc(char *dt, int maxlen, utime_t tim)
80 time_t ttime = (time_t)tim;
83 /* ***FIXME**** the format and localtime_r() should be user configurable */
84 (void)localtime_r(&ttime, &tm);
85 /* NOTE! since the compiler complains about %y, I use %y and cut the century */
86 strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
92 /* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
93 char *bstrutime(char *dt, int maxlen, utime_t tim)
95 time_t ttime = (time_t)tim;
97 (void)localtime_r(&ttime, &tm);
98 strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
102 /* Convert standard time string yyyy-mm-dd hh:mm:ss to Unix time */
103 utime_t str_to_utime(char *str)
108 if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
109 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
117 if (tm.tm_year >= 1900) {
122 tm.tm_wday = tm.tm_yday = 0;
128 return (utime_t)ttime;
133 * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
134 * the number of microseconds since Epoch Time (1 Jan 1970) UTC.
137 btime_t get_current_btime()
140 if (gettimeofday(&tv, NULL) != 0) {
141 tv.tv_sec = (long)time(NULL); /* fall back to old method */
144 return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
147 /* Convert btime to Unix time */
148 time_t btime_to_unix(btime_t bt)
150 return (time_t)(bt/1000000);
153 /* Convert btime to utime */
154 utime_t btime_to_utime(btime_t bt)
156 return (utime_t)(bt/1000000);
160 * Return the week of the month, base 0 (wom)
161 * given tm_mday and tm_wday. Value returned
162 * can be from 0 to 4 => week1, ... week5
164 int tm_wom(int mday, int wday)
166 int fs; /* first sunday */
167 fs = (mday%7) - wday;
172 // Dmsg3(100, "mday=%d wom=0 wday=%d <= fs=%d\n", mday, wday, fs);
175 int wom = 1 + (mday - fs - 1) / 7;
176 // Dmsg4(100, "mday=%d wom=%d wday=%d fs=%d\n", mday, wom, wday, fs);
181 * Given a Unix date return the week of the year.
182 * The returned value can be 0-53. Officially
183 * the weeks are numbered from 1 to 53 where week1
184 * is the week in which the first Thursday of the
185 * year occurs (alternatively, the week which contains
186 * the 4th of January). We return 0, if the week of the
187 * year does not fall in the current year.
189 int tm_woy(time_t stime)
191 int woy, fty, tm_yday;
194 memset(&tm, 0, sizeof(struct tm));
195 (void)localtime_r(&stime, &tm);
196 tm_yday = tm.tm_yday;
200 (void)localtime_r(&time4, &tm);
201 fty = 1 - tm.tm_wday;
205 woy = tm_yday - fty + 4;
212 /* Deprecated. Do not use. */
213 void get_current_time(struct date_time *dt)
219 (void)gmtime_r(&now, &tm);
220 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,
221 tm.tm_hour, tm.tm_min, tm.tm_sec);
224 Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
226 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,
227 tm.tm_hour, tm.tm_min, tm.tm_sec);
232 /* date_encode -- Encode civil date as a Julian day number. */
234 /* Deprecated. Do not use. */
235 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
238 /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
244 ASSERT(day > 0 && day < 32);
254 /* Determine whether date is in Julian or Gregorian calendar based on
255 canonical date of calendar reform. */
257 if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
260 a = ((int) (y / 100));
264 return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
268 /* time_encode -- Encode time from hours, minutes, and seconds
269 into a fraction of a day. */
271 /* Deprecated. Do not use. */
272 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
273 float32_t second_fraction)
275 ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
276 return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
280 /* date_time_encode -- Set day number and fraction from date
283 /* Deprecated. Do not use. */
284 void date_time_encode(struct date_time *dt,
285 uint32_t year, uint8_t month, uint8_t day,
286 uint8_t hour, uint8_t minute, uint8_t second,
287 float32_t second_fraction)
289 dt->julian_day_number = date_encode(year, month, day);
290 dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
293 /* date_decode -- Decode a Julian day number into civil date. */
295 /* Deprecated. Do not use. */
296 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
299 fdate_t z, f, a, alpha, b, c, d, e;
308 alpha = floor((z - 1867216.25) / 36524.25);
309 a = z + 1 + alpha - floor(alpha / 4);
313 c = floor((b - 122.1) / 365.25);
314 d = floor(365.25 * c);
315 e = floor((b - d) / 30.6001);
317 *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
318 *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
319 *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
322 /* time_decode -- Decode a day fraction into civil time. */
324 /* Deprecated. Do not use. */
325 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
326 uint8_t *second, float32_t *second_fraction)
330 ij = (uint32_t) ((time - floor(time)) * 86400.0);
331 *hour = (uint8_t) (ij / 3600L);
332 *minute = (uint8_t) ((ij / 60L) % 60L);
333 *second = (uint8_t) (ij % 60L);
334 if (second_fraction != NULL) {
335 *second_fraction = (float32_t)(time - floor(time));
339 /* date_time_decode -- Decode a Julian day and day fraction
340 into civil date and time. */
342 /* Deprecated. Do not use. */
343 void date_time_decode(struct date_time *dt,
344 uint32_t *year, uint8_t *month, uint8_t *day,
345 uint8_t *hour, uint8_t *minute, uint8_t *second,
346 float32_t *second_fraction)
348 date_decode(dt->julian_day_number, year, month, day);
349 time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
352 /* tm_encode -- Encode a civil date and time from a tm structure
353 * to a Julian day and day fraction.
356 /* Deprecated. Do not use. */
357 void tm_encode(struct date_time *dt,
361 uint8_t month, day, hour, minute, second;
363 year = tm->tm_year + 1900;
364 month = tm->tm_mon + 1;
369 dt->julian_day_number = date_encode(year, month, day);
370 dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
374 /* tm_decode -- Decode a Julian day and day fraction
375 into civil date and time in tm structure */
377 /* Deprecated. Do not use. */
378 void tm_decode(struct date_time *dt,
382 uint8_t month, day, hour, minute, second;
384 date_decode(dt->julian_day_number, &year, &month, &day);
385 time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
386 tm->tm_year = year - 1900;
387 tm->tm_mon = month - 1;
395 /* date_time_compare -- Compare two dates and times and return
396 the relationship as follows:
403 /* Deprecated. Do not use. */
404 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
406 if (dt1->julian_day_number == dt2->julian_day_number) {
407 if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
410 return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
412 return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;