2 * Bacula time and date routines -- John Walker
7 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
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 GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public
20 License along with this program; if not, write to the Free
21 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 void bstrftime(char *dt, int maxlen, uint32_t tim)
35 /* ***FIXME**** the format and localtime_r() should be user configurable */
36 localtime_r(&ttime, &tm);
37 strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
40 void get_current_time(struct date_time *dt)
47 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,
48 tm.tm_hour, tm.tm_min, tm.tm_sec);
51 Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
53 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,
54 tm.tm_hour, tm.tm_min, tm.tm_sec);
59 /* date_encode -- Encode civil date as a Julian day number. */
61 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
64 /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
70 ASSERT(day > 0 && day < 32);
80 /* Determine whether date is in Julian or Gregorian calendar based on
81 canonical date of calendar reform. */
83 if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
86 a = ((int) (y / 100));
90 return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
94 /* time_encode -- Encode time from hours, minutes, and seconds
95 into a fraction of a day. */
97 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
98 float32_t second_fraction)
100 ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
101 return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
105 /* date_time_encode -- Set day number and fraction from date
108 void date_time_encode(struct date_time *dt,
109 uint32_t year, uint8_t month, uint8_t day,
110 uint8_t hour, uint8_t minute, uint8_t second,
111 float32_t second_fraction)
113 dt->julian_day_number = date_encode(year, month, day);
114 dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
117 /* date_decode -- Decode a Julian day number into civil date. */
119 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
122 fdate_t z, f, a, alpha, b, c, d, e;
131 alpha = floor((z - 1867216.25) / 36524.25);
132 a = z + 1 + alpha - floor(alpha / 4);
136 c = floor((b - 122.1) / 365.25);
137 d = floor(365.25 * c);
138 e = floor((b - d) / 30.6001);
140 *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
141 *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
142 *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
145 /* time_decode -- Decode a day fraction into civil time. */
147 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
148 uint8_t *second, float32_t *second_fraction)
152 ij = (uint32_t) ((time - floor(time)) * 86400.0);
153 *hour = (uint8_t) (ij / 3600L);
154 *minute = (uint8_t) ((ij / 60L) % 60L);
155 *second = (uint8_t) (ij % 60L);
156 if (second_fraction != NULL) {
157 *second_fraction = time - floor(time);
161 /* date_time_decode -- Decode a Julian day and day fraction
162 into civil date and time. */
164 void date_time_decode(struct date_time *dt,
165 uint32_t *year, uint8_t *month, uint8_t *day,
166 uint8_t *hour, uint8_t *minute, uint8_t *second,
167 float32_t *second_fraction)
169 date_decode(dt->julian_day_number, year, month, day);
170 time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
173 /* tm_encode -- Encode a civil date and time from a tm structure
174 * to a Julian day and day fraction.
177 void tm_encode(struct date_time *dt,
181 uint8_t month, day, hour, minute, second;
183 year = tm->tm_year + 1900;
184 month = tm->tm_mon + 1;
189 dt->julian_day_number = date_encode(year, month, day);
190 dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
194 /* tm_decode -- Decode a Julian day and day fraction
195 into civil date and time in tm structure */
197 void tm_decode(struct date_time *dt,
201 uint8_t month, day, hour, minute, second;
203 date_decode(dt->julian_day_number, &year, &month, &day);
204 time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
205 tm->tm_year = year - 1900;
206 tm->tm_mon = month - 1;
214 /* date_time_compare -- Compare two dates and times and return
215 the relationship as follows:
222 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
224 if (dt1->julian_day_number == dt2->julian_day_number) {
225 if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
228 return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
230 return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;