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
|
//
// time.c
//
// Time routines
//
// Copyright (C) 2002 Michael Ringgaard. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the project nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
#include <config.h>
#include <drizzled/type/time.h>
#include <drizzled/util/gmtime.h>
namespace drizzled
{
namespace util
{
#define YEAR0 1900
#define EPOCH_YR 1970
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
#define FIRSTSUNDAY(timp) (((timp)->tm_yday - (timp)->tm_wday + 420) % 7)
#define FIRSTDAYOF(timp) (((timp)->tm_wday - (timp)->tm_yday + 420) % 7)
#define TIME_MAX INT64_MIN
int _daylight = 0; // Non-zero if daylight savings time is used
long _dstbias = 0; // Offset for Daylight Saving Time
type::Time::epoch_t _timezone = 0; // Difference in seconds between GMT and local time
const char *_tzname[2] = {"GMT", "GMT"}; // Standard/daylight savings time zone names
const char *_days[] =
{
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
const char *_days_abbrev[] =
{
"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"
};
const char *_months[] =
{
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
const char *_months_abbrev[] =
{
"Jan", "Feb", "Mar",
"Apr", "May", "Jun",
"Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"
};
const int _ytab[2][12] =
{
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
struct tm *gmtime(const type::Time::epoch_t &timer, struct tm *tmbuf)
{
uint64_t dayclock, dayno;
int year = EPOCH_YR;
if (timer < 0)
return NULL;
dayclock = (uint64_t) timer % SECS_DAY;
dayno = (uint64_t) timer / SECS_DAY;
tmbuf->tm_sec = dayclock % 60;
tmbuf->tm_min = (dayclock % 3600) / 60;
tmbuf->tm_hour = dayclock / 3600;
tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday
while (dayno >= (uint64_t) YEARSIZE(year))
{
dayno -= YEARSIZE(year);
year++;
}
tmbuf->tm_year = year - YEAR0;
tmbuf->tm_yday = dayno;
tmbuf->tm_mon = 0;
while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf->tm_mon])
{
dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
tmbuf->tm_mon++;
}
tmbuf->tm_mday = dayno + 1;
tmbuf->tm_isdst = 0;
return tmbuf;
}
void gmtime(const type::Time::epoch_t &timer, type::Time &tmbuf)
{
uint64_t dayclock, dayno;
int32_t year= EPOCH_YR;
if (timer < 0)
return;
tmbuf.reset();
dayclock= (uint64_t) timer % SECS_DAY;
dayno= (uint64_t) timer / SECS_DAY;
tmbuf.second= dayclock % 60;
tmbuf.minute= (dayclock % 3600) / 60;
tmbuf.hour= dayclock / 3600;
while (dayno >= (uint64_t) YEARSIZE(year))
{
dayno -= YEARSIZE(year);
year++;
}
tmbuf.year= year;
while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf.month])
{
dayno -= _ytab[LEAPYEAR(year)][tmbuf.month];
tmbuf.month++;
}
tmbuf.month++;
tmbuf.day= dayno +1;
tmbuf.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
}
void localtime(const type::Time::epoch_t &timer, type::Time &tmbuf)
{
type::Time::epoch_t t;
t = timer - _timezone;
return util::gmtime(t, tmbuf);
}
struct tm *localtime(const type::Time::epoch_t &timer, struct tm *tmbuf)
{
type::Time::epoch_t t;
t = timer - _timezone;
return util::gmtime(t, tmbuf);
}
// We don't use this code.
#if 0
time_t mktime(struct tm *tmbuf)
{
long day, year;
int tm_year;
int yday, month;
/*unsigned*/ long seconds;
int overflow;
long dst;
tmbuf->tm_min += tmbuf->tm_sec / 60;
tmbuf->tm_sec %= 60;
if (tmbuf->tm_sec < 0)
{
tmbuf->tm_sec += 60;
tmbuf->tm_min--;
}
tmbuf->tm_hour += tmbuf->tm_min / 60;
tmbuf->tm_min = tmbuf->tm_min % 60;
if (tmbuf->tm_min < 0)
{
tmbuf->tm_min += 60;
tmbuf->tm_hour--;
}
day = tmbuf->tm_hour / 24;
tmbuf->tm_hour= tmbuf->tm_hour % 24;
if (tmbuf->tm_hour < 0)
{
tmbuf->tm_hour += 24;
day--;
}
tmbuf->tm_year += tmbuf->tm_mon / 12;
tmbuf->tm_mon %= 12;
if (tmbuf->tm_mon < 0)
{
tmbuf->tm_mon += 12;
tmbuf->tm_year--;
}
day += (tmbuf->tm_mday - 1);
while (day < 0)
{
if(--tmbuf->tm_mon < 0)
{
tmbuf->tm_year--;
tmbuf->tm_mon = 11;
}
day += _ytab[LEAPYEAR(YEAR0 + tmbuf->tm_year)][tmbuf->tm_mon];
}
while (day >= _ytab[LEAPYEAR(YEAR0 + tmbuf->tm_year)][tmbuf->tm_mon])
{
day -= _ytab[LEAPYEAR(YEAR0 + tmbuf->tm_year)][tmbuf->tm_mon];
if (++(tmbuf->tm_mon) == 12)
{
tmbuf->tm_mon = 0;
tmbuf->tm_year++;
}
}
tmbuf->tm_mday = day + 1;
year = EPOCH_YR;
if (tmbuf->tm_year < year - YEAR0) return (time_t) -1;
seconds = 0;
day = 0; // Means days since day 0 now
overflow = 0;
// Assume that when day becomes negative, there will certainly
// be overflow on seconds.
// The check for overflow needs not to be done for leapyears
// divisible by 400.
// The code only works when year (1970) is not a leapyear.
tm_year = tmbuf->tm_year + YEAR0;
if (TIME_MAX / 365 < tm_year - year)
overflow++;
day = (tm_year - year) * 365;
if (TIME_MAX - day < (tm_year - year) / 4 + 1) overflow++;
day += (tm_year - year) / 4 + ((tm_year % 4) && tm_year % 4 < year % 4);
day -= (tm_year - year) / 100 + ((tm_year % 100) && tm_year % 100 < year % 100);
day += (tm_year - year) / 400 + ((tm_year % 400) && tm_year % 400 < year % 400);
yday = month = 0;
while (month < tmbuf->tm_mon)
{
yday += _ytab[LEAPYEAR(tm_year)][month];
month++;
}
yday += (tmbuf->tm_mday - 1);
if (day + yday < 0) overflow++;
day += yday;
tmbuf->tm_yday = yday;
tmbuf->tm_wday = (day + 4) % 7; // Day 0 was thursday (4)
seconds = ((tmbuf->tm_hour * 60L) + tmbuf->tm_min) * 60L + tmbuf->tm_sec;
if ((TIME_MAX - seconds) / SECS_DAY < day) overflow++;
seconds += day * SECS_DAY;
// Now adjust according to timezone and daylight saving time
if (((_timezone > 0) && (TIME_MAX - _timezone < seconds))
|| ((_timezone < 0) && (seconds < -_timezone)))
overflow++;
seconds += _timezone;
if (tmbuf->tm_isdst)
dst = _dstbias;
else
dst = 0;
if (dst > seconds) overflow++; // dst is always non-negative
seconds -= dst;
if (overflow) return (time_t) -1;
if ((time_t) seconds != seconds) return (time_t) -1;
return (time_t) seconds;
}
#endif
} /* namespace util */
} /* namespace drizzled */
|