6
// Copyright (C) 2002 Michael Ringgaard. All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions
12
// 1. Redistributions of source code must retain the above copyright
13
// notice, this list of conditions and the following disclaimer.
14
// 2. Redistributions in binary form must reproduce the above copyright
15
// notice, this list of conditions and the following disclaimer in the
16
// documentation and/or other materials provided with the distribution.
17
// 3. Neither the name of the project nor the names of its contributors
18
// may be used to endorse or promote products derived from this software
19
// without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
25
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#include <drizzled/type/time.h>
37
#include <drizzled/util/gmtime.h>
44
#define SECS_DAY (24L * 60L * 60L)
45
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
46
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
47
#define FIRSTSUNDAY(timp) (((timp)->tm_yday - (timp)->tm_wday + 420) % 7)
48
#define FIRSTDAYOF(timp) (((timp)->tm_wday - (timp)->tm_yday + 420) % 7)
50
#define TIME_MAX INT64_MIN
52
const int _ytab[2][12] =
54
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
55
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
58
struct tm *gmtime(const type::epoch_t &timer, struct tm *tmbuf)
60
uint64_t dayclock, dayno;
66
dayclock = (uint64_t) timer % SECS_DAY;
67
dayno = (uint64_t) timer / SECS_DAY;
69
tmbuf->tm_sec = dayclock % 60;
70
tmbuf->tm_min = (dayclock % 3600) / 60;
71
tmbuf->tm_hour = dayclock / 3600;
72
tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday
73
while (dayno >= (uint64_t) YEARSIZE(year))
75
dayno -= YEARSIZE(year);
78
tmbuf->tm_year = year - YEAR0;
79
tmbuf->tm_yday = dayno;
81
while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf->tm_mon])
83
dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
86
tmbuf->tm_mday = dayno + 1;
92
void gmtime(const type::epoch_t &timer, type::Time &tmbuf)
94
uint64_t dayclock, dayno;
95
int32_t year= EPOCH_YR;
102
dayclock= (uint64_t) timer % SECS_DAY;
103
dayno= (uint64_t) timer / SECS_DAY;
105
tmbuf.second= dayclock % 60;
106
tmbuf.minute= (dayclock % 3600) / 60;
107
tmbuf.hour= dayclock / 3600;
108
while (dayno >= (uint64_t) YEARSIZE(year))
110
dayno -= YEARSIZE(year);
114
while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf.month])
116
dayno -= _ytab[LEAPYEAR(year)][tmbuf.month];
121
tmbuf.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
124
} /* namespace util */
125
} /* namespace drizzled */