Drizzled Public API Documentation

gmtime.cc
00001 //
00002 // time.c
00003 //
00004 // Time routines
00005 //
00006 // Copyright (C) 2002 Michael Ringgaard. All rights reserved.
00007 //
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions
00010 // are met:
00011 // 
00012 // 1. Redistributions of source code must retain the above copyright 
00013 //    notice, this list of conditions and the following disclaimer.  
00014 // 2. Redistributions in binary form must reproduce the above copyright
00015 //    notice, this list of conditions and the following disclaimer in the
00016 //    documentation and/or other materials provided with the distribution.  
00017 // 3. Neither the name of the project nor the names of its contributors
00018 //    may be used to endorse or promote products derived from this software
00019 //    without specific prior written permission. 
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00022 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024 // ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00025 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00031 // SUCH DAMAGE.
00032 // 
00033 
00034 #include <config.h>
00035 
00036 #include <drizzled/type/time.h>
00037 #include <drizzled/util/gmtime.h>
00038 
00039 namespace drizzled {
00040 namespace util {
00041 
00042 #define YEAR0                   1900
00043 #define EPOCH_YR                1970
00044 #define SECS_DAY                (24L * 60L * 60L)
00045 #define LEAPYEAR(year)          (!((year) % 4) && (((year) % 100) || !((year) % 400)))
00046 #define YEARSIZE(year)          (LEAPYEAR(year) ? 366 : 365)
00047 #define FIRSTSUNDAY(timp)       (((timp)->tm_yday - (timp)->tm_wday + 420) % 7)
00048 #define FIRSTDAYOF(timp)        (((timp)->tm_wday - (timp)->tm_yday + 420) % 7)
00049 
00050 #define TIME_MAX                INT64_MIN
00051 
00052 const int _ytab[2][12] = 
00053 {
00054   {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
00055   {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
00056 };
00057 
00058 struct tm *gmtime(const type::epoch_t &timer, struct tm *tmbuf)
00059 {
00060   uint64_t dayclock, dayno;
00061   int year = EPOCH_YR;
00062 
00063   if (timer < 0)
00064     return NULL;
00065 
00066   dayclock = (uint64_t) timer % SECS_DAY;
00067   dayno = (uint64_t) timer / SECS_DAY;
00068 
00069   tmbuf->tm_sec = dayclock % 60;
00070   tmbuf->tm_min = (dayclock % 3600) / 60;
00071   tmbuf->tm_hour = dayclock / 3600;
00072   tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday
00073   while (dayno >= (uint64_t) YEARSIZE(year)) 
00074   {
00075     dayno -= YEARSIZE(year);
00076     year++;
00077   }
00078   tmbuf->tm_year = year - YEAR0;
00079   tmbuf->tm_yday = dayno;
00080   tmbuf->tm_mon = 0;
00081   while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf->tm_mon]) 
00082   {
00083     dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
00084     tmbuf->tm_mon++;
00085   }
00086   tmbuf->tm_mday = dayno + 1;
00087   tmbuf->tm_isdst = 0;
00088 
00089   return tmbuf;
00090 }
00091 
00092 void gmtime(const type::epoch_t &timer, type::Time &tmbuf)
00093 {
00094   uint64_t dayclock, dayno;
00095   int32_t year= EPOCH_YR;
00096 
00097   if (timer < 0)
00098     return;
00099 
00100   tmbuf.reset();
00101 
00102   dayclock= (uint64_t) timer % SECS_DAY;
00103   dayno= (uint64_t) timer / SECS_DAY;
00104 
00105   tmbuf.second= dayclock % 60;
00106   tmbuf.minute= (dayclock % 3600) / 60;
00107   tmbuf.hour= dayclock / 3600;
00108   while (dayno >= (uint64_t) YEARSIZE(year)) 
00109   {
00110     dayno -= YEARSIZE(year);
00111     year++;
00112   }
00113   tmbuf.year= year;
00114   while (dayno >= (uint64_t) _ytab[LEAPYEAR(year)][tmbuf.month]) 
00115   {
00116     dayno -= _ytab[LEAPYEAR(year)][tmbuf.month];
00117     tmbuf.month++;
00118   }
00119   tmbuf.month++;
00120   tmbuf.day= dayno +1;
00121   tmbuf.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
00122 }
00123 
00124 } /* namespace util */
00125 } /* namespace drizzled */