]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/btime.c
First cut 1.27 see kes23Oct02
[bacula/bacula] / bacula / src / lib / btime.c
1 /*
2  * Bacula time and date routines -- John Walker
3  *
4  *   Version $Id$
5  */
6 /*
7    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
8
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.
13
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.
18
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,
22    MA 02111-1307, USA.
23
24  */
25
26
27 #include "bacula.h"
28 #include <math.h>
29
30 void bstrftime(char *dt, int maxlen, uint32_t tim)
31 {
32    time_t ttime = tim;
33    struct tm tm;
34    
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);
38 }
39
40 void get_current_time(struct date_time *dt)
41 {
42    struct tm tm;
43    time_t now;
44
45    now = time(NULL);
46    gmtime_r(&now, &tm);
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);
49    tm_encode(dt, &tm);
50 #ifdef DEBUG
51    Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
52    tm_decode(dt, &tm);
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);
55 #endif
56 }
57
58 /*
59  * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
60  *   the number of microseconds since Epoch Time (1 Jan 1970).
61  */
62
63 btime_t get_current_btime()
64 {
65    struct timeval tv;
66    if (gettimeofday(&tv, NULL) != 0) {
67       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
68       tv.tv_usec = 0;
69    }
70    return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
71 }
72
73 /* Convert btime to Unix time */
74 time_t btime_to_etime(btime_t bt)
75 {
76    return (time_t)(bt/1000000);                    
77 }
78
79
80 /*  date_encode  --  Encode civil date as a Julian day number.  */
81
82 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
83 {
84
85     /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
86
87     int32_t a, b, m;
88     uint32_t y;
89
90     ASSERT(month < 13);
91     ASSERT(day > 0 && day < 32);
92
93     m = month;
94     y = year;
95
96     if (m <= 2) {
97         y--;
98         m += 12;
99     }
100
101     /* Determine whether date is in Julian or Gregorian calendar based on
102        canonical date of calendar reform. */
103
104     if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
105         b = 0;
106     } else {
107         a = ((int) (y / 100));
108         b = 2 - a + (a / 4);
109     }
110
111     return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
112                 day + b - 1524.5);
113 }
114
115 /*  time_encode  --  Encode time from hours, minutes, and seconds
116                      into a fraction of a day.  */
117
118 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
119                    float32_t second_fraction)
120 {
121     ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
122     return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
123                      second_fraction;
124 }
125
126 /*  date_time_encode  --  Set day number and fraction from date
127                           and time.  */
128
129 void date_time_encode(struct date_time *dt,
130                       uint32_t year, uint8_t month, uint8_t day,
131                       uint8_t hour, uint8_t minute, uint8_t second,
132                       float32_t second_fraction)
133 {
134     dt->julian_day_number = date_encode(year, month, day);
135     dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
136 }
137
138 /*  date_decode  --  Decode a Julian day number into civil date.  */
139
140 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
141                  uint8_t *day)
142 {
143     fdate_t z, f, a, alpha, b, c, d, e;
144
145     date += 0.5;
146     z = floor(date);
147     f = date - z;
148
149     if (z < 2299161.0) {
150         a = z;
151     } else {
152         alpha = floor((z - 1867216.25) / 36524.25);
153         a = z + 1 + alpha - floor(alpha / 4);
154     }
155
156     b = a + 1524;
157     c = floor((b - 122.1) / 365.25);
158     d = floor(365.25 * c);
159     e = floor((b - d) / 30.6001);
160
161     *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
162     *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
163     *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
164 }
165
166 /*  time_decode  --  Decode a day fraction into civil time.  */
167
168 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
169                  uint8_t *second, float32_t *second_fraction)
170 {
171     uint32_t ij;
172
173     ij = (uint32_t) ((time - floor(time)) * 86400.0);
174     *hour = (uint8_t) (ij / 3600L);
175     *minute = (uint8_t) ((ij / 60L) % 60L);
176     *second = (uint8_t) (ij % 60L);
177     if (second_fraction != NULL) {
178         *second_fraction = time - floor(time);
179     }
180 }
181
182 /*  date_time_decode  --  Decode a Julian day and day fraction
183                           into civil date and time.  */
184
185 void date_time_decode(struct date_time *dt,
186                       uint32_t *year, uint8_t *month, uint8_t *day,
187                       uint8_t *hour, uint8_t *minute, uint8_t *second,
188                       float32_t *second_fraction)
189 {
190     date_decode(dt->julian_day_number, year, month, day);
191     time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
192 }
193
194 /*  tm_encode  --  Encode a civil date and time from a tm structure   
195  *                 to a Julian day and day fraction.
196  */
197
198 void tm_encode(struct date_time *dt,
199                       struct tm *tm) 
200 {
201     uint32_t year;
202     uint8_t month, day, hour, minute, second;
203
204     year = tm->tm_year + 1900;
205     month = tm->tm_mon + 1;
206     day = tm->tm_mday;
207     hour = tm->tm_hour;
208     minute = tm->tm_min;
209     second = tm->tm_sec;
210     dt->julian_day_number = date_encode(year, month, day);
211     dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
212 }
213
214
215 /*  tm_decode  --  Decode a Julian day and day fraction
216                    into civil date and time in tm structure */
217
218 void tm_decode(struct date_time *dt,
219                       struct tm *tm) 
220 {
221     uint32_t year;
222     uint8_t month, day, hour, minute, second;
223
224     date_decode(dt->julian_day_number, &year, &month, &day);
225     time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
226     tm->tm_year = year - 1900;
227     tm->tm_mon = month - 1;
228     tm->tm_mday = day;
229     tm->tm_hour = hour;
230     tm->tm_min = minute;
231     tm->tm_sec = second;
232 }
233
234
235 /*  date_time_compare  --  Compare two dates and times and return
236                            the relationship as follows:
237
238                                     -1    dt1 < dt2
239                                      0    dt1 = dt2
240                                      1    dt1 > dt2
241 */
242
243 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
244 {
245     if (dt1->julian_day_number == dt2->julian_day_number) {
246         if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
247             return 0;
248         }
249         return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
250     }
251     return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;
252 }