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 /* Check for bad argument */
109 if (!str || *str == 0) {
113 if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
114 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
122 if (tm.tm_year >= 1900) {
127 tm.tm_wday = tm.tm_yday = 0;
133 return (utime_t)ttime;
138 * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
139 * the number of microseconds since Epoch Time (1 Jan 1970) UTC.
142 btime_t get_current_btime()
145 if (gettimeofday(&tv, NULL) != 0) {
146 tv.tv_sec = (long)time(NULL); /* fall back to old method */
149 return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
152 /* Convert btime to Unix time */
153 time_t btime_to_unix(btime_t bt)
155 return (time_t)(bt/1000000);
158 /* Convert btime to utime */
159 utime_t btime_to_utime(btime_t bt)
161 return (utime_t)(bt/1000000);
165 * Return the week of the month, base 0 (wom)
166 * given tm_mday and tm_wday. Value returned
167 * can be from 0 to 4 => week1, ... week5
169 int tm_wom(int mday, int wday)
171 int fs; /* first sunday */
172 fs = (mday%7) - wday;
177 // Dmsg3(100, "mday=%d wom=0 wday=%d <= fs=%d\n", mday, wday, fs);
180 int wom = 1 + (mday - fs - 1) / 7;
181 // Dmsg4(100, "mday=%d wom=%d wday=%d fs=%d\n", mday, wom, wday, fs);
186 * Given a Unix date return the week of the year.
187 * The returned value can be 0-53. Officially
188 * the weeks are numbered from 1 to 53 where week1
189 * is the week in which the first Thursday of the
190 * year occurs (alternatively, the week which contains
191 * the 4th of January). We return 0, if the week of the
192 * year does not fall in the current year.
194 int tm_woy(time_t stime)
196 int woy, fty, tm_yday;
199 memset(&tm, 0, sizeof(struct tm));
200 (void)localtime_r(&stime, &tm);
201 tm_yday = tm.tm_yday;
205 (void)localtime_r(&time4, &tm);
206 fty = 1 - tm.tm_wday;
210 woy = tm_yday - fty + 4;
217 /* Deprecated. Do not use. */
218 void get_current_time(struct date_time *dt)
224 (void)gmtime_r(&now, &tm);
225 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,
226 tm.tm_hour, tm.tm_min, tm.tm_sec);
229 Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
231 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,
232 tm.tm_hour, tm.tm_min, tm.tm_sec);
237 /* date_encode -- Encode civil date as a Julian day number. */
239 /* Deprecated. Do not use. */
240 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
243 /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
249 ASSERT(day > 0 && day < 32);
259 /* Determine whether date is in Julian or Gregorian calendar based on
260 canonical date of calendar reform. */
262 if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
265 a = ((int) (y / 100));
269 return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
273 /* time_encode -- Encode time from hours, minutes, and seconds
274 into a fraction of a day. */
276 /* Deprecated. Do not use. */
277 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
278 float32_t second_fraction)
280 ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
281 return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
285 /* date_time_encode -- Set day number and fraction from date
288 /* Deprecated. Do not use. */
289 void date_time_encode(struct date_time *dt,
290 uint32_t year, uint8_t month, uint8_t day,
291 uint8_t hour, uint8_t minute, uint8_t second,
292 float32_t second_fraction)
294 dt->julian_day_number = date_encode(year, month, day);
295 dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
298 /* date_decode -- Decode a Julian day number into civil date. */
300 /* Deprecated. Do not use. */
301 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
304 fdate_t z, f, a, alpha, b, c, d, e;
313 alpha = floor((z - 1867216.25) / 36524.25);
314 a = z + 1 + alpha - floor(alpha / 4);
318 c = floor((b - 122.1) / 365.25);
319 d = floor(365.25 * c);
320 e = floor((b - d) / 30.6001);
322 *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
323 *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
324 *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
327 /* time_decode -- Decode a day fraction into civil time. */
329 /* Deprecated. Do not use. */
330 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
331 uint8_t *second, float32_t *second_fraction)
335 ij = (uint32_t) ((time - floor(time)) * 86400.0);
336 *hour = (uint8_t) (ij / 3600L);
337 *minute = (uint8_t) ((ij / 60L) % 60L);
338 *second = (uint8_t) (ij % 60L);
339 if (second_fraction != NULL) {
340 *second_fraction = (float32_t)(time - floor(time));
344 /* date_time_decode -- Decode a Julian day and day fraction
345 into civil date and time. */
347 /* Deprecated. Do not use. */
348 void date_time_decode(struct date_time *dt,
349 uint32_t *year, uint8_t *month, uint8_t *day,
350 uint8_t *hour, uint8_t *minute, uint8_t *second,
351 float32_t *second_fraction)
353 date_decode(dt->julian_day_number, year, month, day);
354 time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
357 /* tm_encode -- Encode a civil date and time from a tm structure
358 * to a Julian day and day fraction.
361 /* Deprecated. Do not use. */
362 void tm_encode(struct date_time *dt,
366 uint8_t month, day, hour, minute, second;
368 year = tm->tm_year + 1900;
369 month = tm->tm_mon + 1;
374 dt->julian_day_number = date_encode(year, month, day);
375 dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
379 /* tm_decode -- Decode a Julian day and day fraction
380 into civil date and time in tm structure */
382 /* Deprecated. Do not use. */
383 void tm_decode(struct date_time *dt,
387 uint8_t month, day, hour, minute, second;
389 date_decode(dt->julian_day_number, &year, &month, &day);
390 time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
391 tm->tm_year = year - 1900;
392 tm->tm_mon = month - 1;
400 /* date_time_compare -- Compare two dates and times and return
401 the relationship as follows:
408 /* Deprecated. Do not use. */
409 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
411 if (dt1->julian_day_number == dt2->julian_day_number) {
412 if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
415 return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
417 return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;