~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/util/gmtime.cc

  • Committer: Mark Atwood
  • Date: 2011-10-27 05:08:12 UTC
  • mfrom: (2445.1.11 rf)
  • Revision ID: me@mark.atwood.name-20111027050812-1icvs72lb0u4xdc4
mergeĀ lp:~olafvdspek/drizzle/refactor8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
34
#include <config.h>
 
35
 
 
36
#include <drizzled/type/time.h>
 
37
#include <drizzled/util/gmtime.h>
 
38
 
 
39
namespace drizzled {
 
40
namespace util {
 
41
 
 
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
 
 
50
#define TIME_MAX                INT64_MIN
 
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
 
 
58
struct tm *gmtime(const type::epoch_t &timer, struct tm *tmbuf)
 
59
{
 
60
  uint64_t dayclock, dayno;
 
61
  int year = EPOCH_YR;
 
62
 
 
63
  if (timer < 0)
 
64
    return NULL;
 
65
 
 
66
  dayclock = (uint64_t) timer % SECS_DAY;
 
67
  dayno = (uint64_t) timer / SECS_DAY;
 
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
 
73
  while (dayno >= (uint64_t) YEARSIZE(year)) 
 
74
  {
 
75
    dayno -= YEARSIZE(year);
 
76
    year++;
 
77
  }
 
78
  tmbuf->tm_year = year - YEAR0;
 
79
  tmbuf->tm_yday = dayno;
 
80
  tmbuf->tm_mon = 0;
 
81
  while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf->tm_mon]) 
 
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
 
 
92
void gmtime(const type::epoch_t &timer, type::Time &tmbuf)
 
93
{
 
94
  uint64_t dayclock, dayno;
 
95
  int32_t year= EPOCH_YR;
 
96
 
 
97
  if (timer < 0)
 
98
    return;
 
99
 
 
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;
 
121
  tmbuf.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
 
122
}
 
123
 
 
124
} /* namespace util */
 
125
} /* namespace drizzled */