• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/DcHttpTime.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 Beesnest 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 Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 /**
00022  * DcHttpTime.cpp: implementation of the DcHttpTime class.
00023  * for documentation see the header file.
00024  */
00025 
00026 #include "DcHTTPTime.h"
00027 
00028 CdcHttpTime::t_num_map CdcHttpTime::oMap;
00029 CdcMutex CdcHttpTime::oMutex;
00030 
00031 CdcHttpTime::CdcHttpTime (bool gmt)
00032 {
00033     nTheTime = time (NULL);
00034     struct tm* pbt;
00035     oMutex.Lock ();
00036     if (gmt)
00037         pbt = gmtime (&nTheTime);
00038     else
00039         pbt = localtime (&nTheTime);
00040     if (pbt != NULL)
00041         oBrokenTime = *pbt;
00042     oMutex.Unlock ();
00043 }
00044 
00045 
00046 CdcHttpTime::CdcHttpTime (time_t time, bool gmt)
00047 :nTheTime (time)
00048 {
00049     struct tm* pbt;
00050     oMutex.Lock ();
00051     if (gmt)
00052         pbt = gmtime (&nTheTime);
00053     else
00054         pbt = localtime (&nTheTime);
00055     if (pbt != NULL)
00056         oBrokenTime = *pbt;
00057     oMutex.Unlock ();
00058 }
00059 
00060 
00061 CdcHttpTime::CdcHttpTime (int year, int month, int day,
00062                           int hour, int min, int sec)
00063 {
00064     oBrokenTime.tm_sec = sec;
00065     oBrokenTime.tm_min = min;
00066     oBrokenTime.tm_hour = hour;
00067     oBrokenTime.tm_mday = day;
00068     oBrokenTime.tm_mon = month - 1;
00069     oBrokenTime.tm_year = year - 1900;
00070     oBrokenTime.tm_isdst = 0;
00071 
00072     nTheTime = mktime (&oBrokenTime);
00073 }
00074 
00075 
00076 CdcHttpTime::CdcHttpTime (const CdcString& strtime) throw (CdcException)
00077 {
00078     if (strtime.Find ("GMT") == -1)
00079         *this = ParseAnsiCTime (strtime);
00080     else
00081         *this = ParseHttpTime (strtime);
00082 }
00083 
00084 CdcHttpTime CdcHttpTime::ToGmt ()
00085 {
00086     AddHours (5);
00087 }
00088 
00089 CdcHttpTime CdcHttpTime::ToLocal ()
00090 {
00091 
00092 }
00093 
00094 CdcString CdcHttpTime::Format (const CdcString& format_str) const
00095 {
00096     char buff [BUFF_LEN];
00097     int len =  strftime (buff, BUFF_LEN, format_str.GetBuffer (),
00098         &oBrokenTime);
00099     if (len == 0)
00100         return EMPTY_STR;
00101     return CdcString (buff, len);
00102 }
00103 
00104 
00105 CdcString CdcHttpTime::FormatGmt (const CdcString& format_str) const
00106 {
00107     char buff [BUFF_LEN];
00108     oMutex.Lock ();
00109     struct tm* pbt = gmtime (&nTheTime);
00110     int len =  strftime (buff, BUFF_LEN, format_str.GetBuffer (), pbt);
00111     oMutex.Unlock ();
00112     if (len == 0)
00113         return EMPTY_STR;
00114     return CdcString (buff, len);
00115 }
00116 
00117 
00118 CdcHttpTime& CdcHttpTime::AddSeconds (int secs)
00119 {
00120     oBrokenTime.tm_sec += secs;
00121     nTheTime = mktime (&oBrokenTime);
00122     return *this;
00123 }
00124 
00125 CdcHttpTime& CdcHttpTime::AddMinutes (int mins)
00126 {
00127     oBrokenTime.tm_min += mins;
00128     nTheTime = mktime (&oBrokenTime);
00129     return *this;
00130 }
00131 
00132 CdcHttpTime& CdcHttpTime::AddHours (int hrs)
00133 {
00134     oBrokenTime.tm_hour += hrs;
00135     nTheTime = mktime (&oBrokenTime);
00136     return *this;
00137 }
00138 
00139 CdcHttpTime& CdcHttpTime::AddDays (int days)
00140 {
00141     oBrokenTime.tm_mday += days;
00142     nTheTime = mktime (&oBrokenTime);
00143     return *this;
00144 }
00145 
00146 CdcHttpTime& CdcHttpTime::AddMonths (int months)
00147 {
00148     oBrokenTime.tm_mon += months;
00149     nTheTime = mktime (&oBrokenTime);
00150     return *this;
00151 }
00152 
00153 CdcHttpTime& CdcHttpTime::AddYears (int years)
00154 {
00155     oBrokenTime.tm_year += years;
00156     nTheTime = mktime (&oBrokenTime);
00157     return *this;
00158 }
00159 
00160 
00161 CdcHttpTime CdcHttpTime::ParseHttpTime (const CdcString& strtime) throw (CdcException)
00162 {
00163     int day=-1, month=-1, year=-1, hour=-1, min=-1, sec=-1;
00164     CdcString time = strtime;
00165     time.Replace ("-", " ");
00166 
00167     int place = time.Find (',') + 2;
00168     if (place == 1) throw (CdcException ("Bad time format."));
00169     day = atoi (time.Mid (place, 2).GetBuffer ());
00170 
00171     place = time.Find (' ', place) + 1;
00172     if (place == 0) throw (CdcException ("Bad time format."));
00173     month = GetMonthNumber (time.Mid (place, 3));
00174 
00175     place = time.Find (' ', place) + 1;
00176     if (place == 0) throw (CdcException ("Bad time format."));
00177     year = atoi (time.Mid (place, 4).GetBuffer ());
00178     if (year < 100)
00179         if (year < 50)
00180             year += 2000;
00181         else
00182             year += 1999;
00183 
00184     place = time.Find (' ', place) + 1;
00185     if (place == 0) throw (CdcException ("Bad time format."));
00186     hour = atoi (time.Mid (place, 2).GetBuffer ());
00187 
00188     place = time.Find (':', place) + 1;
00189     if (place == 0) throw (CdcException ("Bad time format."));
00190     min = atoi (time.Mid (place, 2).GetBuffer ());
00191 
00192     place = time.Find (':', place) + 1;
00193     if (place == 0) throw (CdcException ("Bad time format."));
00194     sec = atoi (time.Mid (place, 2).GetBuffer ());
00195 
00196     if (day < 1 || day > 31 ||
00197         month < 1 || month > 12 ||
00198         year < 1970 || year > 2038 ||
00199         hour < 0 || hour > 23 ||
00200         min < 0 || min > 59 ||
00201         sec < 0 || sec > 59)
00202         throw (CdcException ("Bad time format."));
00203 
00204     return CdcHttpTime (year, month, day, hour, min, sec);
00205 }
00206 
00207 
00208 CdcHttpTime CdcHttpTime::ParseAnsiCTime (const CdcString& strtime) throw (CdcException)
00209 {
00210     int day, month, year, hour, min, sec;
00211     CdcString time = strtime;
00212 
00213     int place = time.Find (' ') + 1;
00214     if (place == 0) throw (CdcException ("Bad time format."));
00215     month = GetMonthNumber (time.Mid (place, 3));
00216 
00217     place = time.Find (' ', place) + 1;
00218     if (place == 0) throw (CdcException ("Bad time format."));
00219     day = atoi (time.Mid (place, 2).GetBuffer ());
00220 
00221     place = time.Find (' ', place) + 1;
00222     if (place == 0) throw (CdcException ("Bad time format."));
00223     hour = atoi (time.Mid (place, 2).GetBuffer ());
00224 
00225     place = time.Find (':', place) + 1;
00226     if (place == 0) throw (CdcException ("Bad time format."));
00227     min = atoi (time.Mid (place, 2).GetBuffer ());
00228 
00229     place = time.Find (':', place) + 1;
00230     if (place == 0) throw (CdcException ("Bad time format."));
00231     sec = atoi (time.Mid (place, 2).GetBuffer ());
00232 
00233     place = time.Find (' ', place) + 1;
00234     if (place == 0) throw (CdcException ("Bad time format."));
00235     year = atoi (time.Mid (place, 4).GetBuffer ());
00236 
00237     if (day < 1 || day > 31 ||
00238         month < 1 || month > 12 ||
00239         year < 1970 || year > 2038 ||
00240         hour < 0 || hour > 23 ||
00241         min < 0 || min > 59 ||
00242         sec < 0 || sec > 59)
00243         throw (CdcException ("Bad time format."));
00244 
00245     return CdcHttpTime (year, month, day, hour, min, sec);
00246 }
00247 
00248 
00249 int CdcHttpTime::GetMonthNumber (const CdcString& str)
00250 {
00251     int num = 0;
00252     num = oMap [str];
00253     return num;
00254 }
00255 
00256 
00257 void CdcHttpTime::Init ()
00258 {
00259     oMap ["Jan"] = 1;
00260     oMap ["Feb"] = 2;
00261     oMap ["Mar"] = 3;
00262     oMap ["Apr"] = 4;
00263     oMap ["May"] = 5;
00264     oMap ["Jun"] = 6;
00265     oMap ["Jul"] = 7;
00266     oMap ["Aug"] = 8;
00267     oMap ["Sep"] = 9;
00268     oMap ["Oct"] = 10;
00269     oMap ["Nov"] = 11;
00270     oMap ["Dec"] = 12;
00271 
00272     /* Set time zone and day light saving. */
00273     //tzset ();
00274 }
00275 

Generated on Mon Oct 11 2010 16:23:24 for Beesnest by  doxygen 1.7.2