OCalendarTime.cc

Go to the documentation of this file.
00001 /*
00002  * This file is part of openSDK.
00003  *
00004  * Copyright (C) 2006-2007 openSDK team
00005  *
00006  * openSDK is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; version 2.
00009  *
00010  * openSDK is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with openSDK; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  *     $Id: OCalendarTime.cc,v 1.8 2007/02/14 17:34:38 nuno-lopes Exp $
00020  */
00021 
00022 #include <OPENR/OCalendarTime.h>
00023 #include <cstdio>
00024 #include <cstring>
00025 #include <time.h>
00026 #include "Platform.h"
00027 
00028 // we cannot use strftime() because of non-C locals
00029 static const char monthNames[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
00030 static const char DOWNames[][4]   = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // sunday starts at 0
00031 
00032 
00033 static OStatus convert_to_calendar(OCalendarTime& calendar, const OTime& time)
00034 {
00035         struct tm timeStruct;
00036         time_t clock = time.GetClock() + TIME_DIFF_SYSTEM;
00037 
00038         if (gmtime_r(&clock, &timeStruct) == NULL){
00039                 return oFAIL;
00040         }
00041 
00042         calendar.year_    = timeStruct.tm_year + 1900 - otimeSTART_YEAR;
00043         calendar.month_   = timeStruct.tm_mon + 1;
00044         calendar.day_     = timeStruct.tm_mday;
00045         calendar.hour_    = timeStruct.tm_hour;
00046         calendar.min_     = timeStruct.tm_min;
00047         calendar.sec_     = timeStruct.tm_sec;
00048         calendar.dow_     = timeStruct.tm_wday;
00049         calendar.timeDif_ = time.GetTimeDif();
00050 
00051         return oSUCCESS;
00052 }
00053 
00054 static const time_t convert_to_timestamp(const OCalendarTime& calendar)
00055 {
00056         time_t clock;
00057         sbyte timeDif;
00058         calendar.GetTime(&clock, &timeDif);
00059         return clock + (timeDif * 3600);
00060 }
00061 
00062 
00063 OCalendarTime::OCalendarTime()
00064 {
00065         OCalendarTime(0, 0);
00066 }
00067 
00068 OCalendarTime::OCalendarTime(time_t clock, sbyte timeDif)
00069 {
00070         OTime time(clock, timeDif);
00071         convert_to_calendar(*this, time);
00072 }
00073 
00074 OCalendarTime::OCalendarTime(const OTime& time)
00075 {
00076         convert_to_calendar(*this, time);
00077 }
00078 
00079 OCalendarTime::OCalendarTime(const OCalendarTime& calendar)
00080 {
00081         *this = calendar;
00082 }
00083 
00084 OCalendarTime& OCalendarTime::operator=(const OCalendarTime& calendar)
00085 {
00086         year_    = calendar.year_;
00087         month_   = calendar.month_;
00088         day_     = calendar.day_;
00089         hour_    = calendar.hour_;
00090         min_     = calendar.min_;
00091         sec_     = calendar.sec_;
00092         dow_     = calendar.dow_;
00093         timeDif_ = calendar.timeDif_;
00094 
00095         return *this;
00096 }
00097 
00098 bool OCalendarTime::operator<(const OCalendarTime& calendar) const
00099 {
00100         return (convert_to_timestamp(*this) < convert_to_timestamp(calendar));
00101 }
00102 
00103 bool OCalendarTime::operator<=(const OCalendarTime& calendar) const
00104 {
00105         return (convert_to_timestamp(*this) <= convert_to_timestamp(calendar));
00106 }
00107 
00108 bool OCalendarTime::operator>(const OCalendarTime& calendar) const
00109 {
00110         return (convert_to_timestamp(*this) > convert_to_timestamp(calendar));
00111 }
00112 
00113 bool OCalendarTime::operator>=(const OCalendarTime& calendar) const
00114 {
00115         return (convert_to_timestamp(*this) >= convert_to_timestamp(calendar));
00116 }
00117 
00118 bool OCalendarTime::operator==(const OCalendarTime& calendar) const
00119 {
00120         return (convert_to_timestamp(*this) == convert_to_timestamp(calendar));
00121 }
00122 
00123 OStatus OCalendarTime::SetTime(time_t clock, sbyte timeDif)
00124 {
00125         OTime time(clock, timeDif);
00126         return convert_to_calendar(*this, time);
00127 }
00128 
00129 OStatus OCalendarTime::SetTime(const OTime& time)
00130 {
00131         return convert_to_calendar(*this, time);
00132 }
00133 
00134 OStatus OCalendarTime::GetTime(time_t* clock, sbyte* timeDif) const
00135 {
00136         struct tm timeStruct;
00137         timeStruct.tm_year   = year_ + otimeSTART_YEAR - 1900;
00138         timeStruct.tm_mon    = month_ - 1;
00139         timeStruct.tm_mday   = day_;
00140         timeStruct.tm_hour   = hour_;
00141         timeStruct.tm_min    = min_;
00142         timeStruct.tm_sec    = sec_;
00143         timeStruct.tm_isdst  = 0;
00144 
00145         if ((*clock = mktime(&timeStruct)) == -1) {
00146                 return oFAIL;
00147         }
00148 
00149         *timeDif = timeDif_;
00150         *clock -= TIME_DIFF_SYSTEM;
00151         return oSUCCESS;
00152 }
00153 
00154 OStatus OCalendarTime::GetTime(OTime* time) const
00155 {
00156         return GetTime(&(time->clock_), &(time->timeDif_));
00157 }
00158 
00159 bool OCalendarTime::ProperDate() const
00160 {
00161         OTime dummy;
00162         return (GetTime(&dummy) == oSUCCESS ? true : false);
00163 }
00164 
00165 OStatus OCalendarTime::CorrectDayOfTheWeek()
00166 {
00167         OTime dummy;
00168         return ((GetTime(&dummy) == oSUCCESS && convert_to_calendar(*this, dummy) == oSUCCESS) ? oSUCCESS : oFAIL);
00169 }
00170 
00171 void OCalendarTime::Print() const
00172 {
00173         // TODO
00174 }
00175 
00176 void OCalendarTime::Sprint(char* str) const
00177 {
00178         RFC2822(str);
00179 }
00180 
00181 void OCalendarTime::RFC2822(char* str) const
00182 {
00183         if (dow_ < 0 || dow_ > 6) {
00184                 strcpy(str, "OCalendarTime::RFC2822() : Invalid day of week");
00185         } else if (month_ < 1 || month_ > 12) {
00186                 strcpy(str, "OCalendarTime::RFC2822() : Invalid month");
00187         } else {
00188                 sprintf(str, "%s, %02d %s %d %02d:%02d:%02d %+02d00", DOWNames[dow_], day_, monthNames[month_-1], year_+otimeSTART_YEAR, hour_, min_, sec_, timeDif_);
00189         }
00190 }

Generated on Sun Dec 2 23:04:30 2007 for openSDK by  doxygen 1.3.9.1