PSTime/src/TimeUtils.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: TimeUtils.cpp 7381 2013-12-16 22:43:48Z salnikov@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Class TimeUtils...
00007 //
00008 // Author List:
00009 //      Andrei Salnikov
00010 //
00011 //------------------------------------------------------------------------
00012 
00013 //-----------------------
00014 // This Class's Header --
00015 //-----------------------
00016 #include "PSTime/TimeUtils.h"
00017 
00018 //-----------------
00019 // C/C++ Headers --
00020 //-----------------
00021 
00022 //-------------------------------
00023 // Collaborating Class Headers --
00024 //-------------------------------
00025 
00026 //-----------------------------------------------------------------------
00027 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00028 //-----------------------------------------------------------------------
00029 
00030 #define USE_TIMEGM 1
00031 
00032 
00033 #if !USE_TIMEGM
00034 
00035 namespace {
00036 
00037   bool is_leap_year(int year) 
00038   {
00039     if (year % 400 == 0) return true;
00040     if (year % 100 == 0) return false;
00041     if (year % 4 == 0) return true;
00042     return false;
00043   }
00044   
00045   int mdays[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
00046 
00047   // Returns the number of days in a month. 
00048   int days(int year, int month) 
00049   {
00050     int days = mdays[month];
00051     if (is_leap_year(year) and month > 0) ++ days;
00052     return days;
00053   }
00054   
00055   int run_days[12] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
00056 
00057   // Returns the number of days since the beginning of the year 
00058   // right after the month ends. 
00059   int running_days(int year, int month) 
00060   {
00061     int days = run_days[month];
00062     if (is_leap_year(year) and month > 0) ++ days;
00063     return days;
00064   }
00065   
00066   // number of leap years between given year and 1970
00067   // not including given year
00068   int leap_years(int year) {
00069     return (year-1969)/4 - (year-1901)/100 + (year-1601)/400;
00070   }
00071   
00072 }
00073 
00074 #endif
00075 
00076 //              ----------------------------------------
00077 //              -- Public Function Member Definitions --
00078 //              ----------------------------------------
00079 
00080 namespace PSTime {
00081 namespace TimeUtils {
00082 
00083 // Convert broken time to UTC time (in UTC timezone)
00084 time_t 
00085 timegm(struct tm* timeptr)
00086 {
00087 #if USE_TIMEGM
00088   
00089   return ::timegm(timeptr);
00090 
00091 #else 
00092   
00093   // normalize, completely ignore leap seconds
00094   while (timeptr->tm_sec > 59) {
00095     timeptr->tm_sec -= 60;
00096     timeptr->tm_min += 1;
00097   }
00098   while (timeptr->tm_min > 59) {
00099     timeptr->tm_min -= 60;
00100     timeptr->tm_hour += 1;
00101   }
00102   while (timeptr->tm_hour > 23) {
00103     timeptr->tm_hour -= 24;
00104     timeptr->tm_mday += 1;
00105   }
00106   while (timeptr->tm_mday > ::days(timeptr->tm_year, timeptr->tm_mon)) {
00107     timeptr->tm_mday -= ::days(timeptr->tm_year, timeptr->tm_mon);
00108     timeptr->tm_mon += 1;
00109   }
00110   while (timeptr->tm_mon > 11) {
00111     timeptr->tm_mon -= 12;
00112     timeptr->tm_year += 1;
00113   }
00114 
00115   int year = timeptr->tm_year+1900;
00116   int days = (year-1970)*365 + ::leap_years(year);
00117   days += timeptr->tm_mon == 0 ? 0 : ::running_days(year, timeptr->tm_mon-1);
00118   days += timeptr->tm_mday-1;
00119 
00120   time_t result = days * time_t(24*3600);
00121   result += timeptr->tm_hour*3600 + timeptr->tm_min*60 + timeptr->tm_sec;
00122   
00123   return result;
00124 
00125 #endif
00126 }
00127 
00128 
00129 } // namespace PSTime
00130 } // namespace TimeUtils

Generated on 19 Dec 2016 for PSANAclasses by  doxygen 1.4.7