~drizzle-trunk/drizzle/development

2088.8.1 by Brian Aker
Merge in gmtime.
1
//
2
// time.c
3
//
4
// Time routines
5
//
6
// Copyright (C) 2002 Michael Ringgaard. All rights reserved.
7
//
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions
10
// are met:
11
// 
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. 
20
// 
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 
31
// SUCH DAMAGE.
32
// 
33
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
34
#include <config.h>
2088.8.3 by Brian Aker
Merge in modifications for gmtime to be 64bit on all platforms (that have an
35
36
#include <drizzled/type/time.h>
2088.8.2 by Brian Aker
Switch to internal gmtime
37
#include <drizzled/util/gmtime.h>
38
2318.6.115 by Olaf van der Spek
Remove unused code
39
namespace drizzled {
40
namespace util {
41
2088.8.1 by Brian Aker
Merge in gmtime.
42
#define YEAR0                   1900
43
#define EPOCH_YR                1970
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)
49
2088.8.3 by Brian Aker
Merge in modifications for gmtime to be 64bit on all platforms (that have an
50
#define TIME_MAX                INT64_MIN
2088.8.1 by Brian Aker
Merge in gmtime.
51
52
const int _ytab[2][12] = 
53
{
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}
56
};
57
2318.6.112 by Olaf van der Spek
Move type::Time typedefs to common_fwd
58
struct tm *gmtime(const type::epoch_t &timer, struct tm *tmbuf)
2088.8.1 by Brian Aker
Merge in gmtime.
59
{
2088.8.3 by Brian Aker
Merge in modifications for gmtime to be 64bit on all platforms (that have an
60
  uint64_t dayclock, dayno;
2088.8.1 by Brian Aker
Merge in gmtime.
61
  int year = EPOCH_YR;
62
2097.1.4 by Brian Aker
Merge in fix for runaway calls to gmtime.
63
  if (timer < 0)
64
    return NULL;
65
2088.8.3 by Brian Aker
Merge in modifications for gmtime to be 64bit on all platforms (that have an
66
  dayclock = (uint64_t) timer % SECS_DAY;
67
  dayno = (uint64_t) timer / SECS_DAY;
2088.8.1 by Brian Aker
Merge in gmtime.
68
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
2088.8.3 by Brian Aker
Merge in modifications for gmtime to be 64bit on all platforms (that have an
73
  while (dayno >= (uint64_t) YEARSIZE(year)) 
2088.8.1 by Brian Aker
Merge in gmtime.
74
  {
75
    dayno -= YEARSIZE(year);
76
    year++;
77
  }
78
  tmbuf->tm_year = year - YEAR0;
79
  tmbuf->tm_yday = dayno;
80
  tmbuf->tm_mon = 0;
2088.8.3 by Brian Aker
Merge in modifications for gmtime to be 64bit on all platforms (that have an
81
  while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf->tm_mon]) 
2088.8.1 by Brian Aker
Merge in gmtime.
82
  {
83
    dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
84
    tmbuf->tm_mon++;
85
  }
86
  tmbuf->tm_mday = dayno + 1;
87
  tmbuf->tm_isdst = 0;
88
89
  return tmbuf;
90
}
91
2318.6.112 by Olaf van der Spek
Move type::Time typedefs to common_fwd
92
void gmtime(const type::epoch_t &timer, type::Time &tmbuf)
2088.8.4 by Brian Aker
Fix storage, skip step, use gmtime.
93
{
94
  uint64_t dayclock, dayno;
95
  int32_t year= EPOCH_YR;
96
2097.1.4 by Brian Aker
Merge in fix for runaway calls to gmtime.
97
  if (timer < 0)
98
    return;
99
2088.8.4 by Brian Aker
Fix storage, skip step, use gmtime.
100
  tmbuf.reset();
101
102
  dayclock= (uint64_t) timer % SECS_DAY;
103
  dayno= (uint64_t) timer / SECS_DAY;
104
105
  tmbuf.second= dayclock % 60;
106
  tmbuf.minute= (dayclock % 3600) / 60;
107
  tmbuf.hour= dayclock / 3600;
108
  while (dayno >= (uint64_t) YEARSIZE(year)) 
109
  {
110
    dayno -= YEARSIZE(year);
111
    year++;
112
  }
113
  tmbuf.year= year;
114
  while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf.month]) 
115
  {
116
    dayno -= _ytab[LEAPYEAR(year)][tmbuf.month];
117
    tmbuf.month++;
118
  }
119
  tmbuf.month++;
120
  tmbuf.day= dayno +1;
2088.8.9 by Brian Aker
Merge in namespace of enum.
121
  tmbuf.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
2088.8.4 by Brian Aker
Fix storage, skip step, use gmtime.
122
}
123
2088.8.1 by Brian Aker
Merge in gmtime.
124
} /* namespace util */
125
} /* namespace drizzled */