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

Beesnest/DcHttpTime.h

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 /* DcHttpTime.h: interface for the CdcHttpTime class. */
00021 
00022 /**
00023  * This class makes a time and date object. it can handle HTTP time strings,
00024  * and do some basic time and date operations.
00025  *
00026  * <p>
00027  * There are three types of HTTP date / time strings: <br>
00028  * Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123 <br>
00029  * Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 <br>
00030  * Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format <br>
00031  *
00032  * <p>
00033  * @author Erez Bibi
00034  * @version 1.0
00035  */
00036 
00037 #ifndef __CDC_HTTP_TIME
00038 #define __CDC_HTTP_TIME
00039 
00040 #include "DcGlobals.h"
00041 #include "DcException.h"
00042 #include _GET_INCLUDE_PATH (DcMutex.h)
00043 
00044 
00045 class CdcHttpTime
00046 {
00047 private:
00048 
00049     /** The time this object represents. */
00050     time_t nTheTime;
00051 
00052     struct tm oBrokenTime;
00053 
00054     typedef map <CdcString, int> t_num_map;
00055 
00056     /** Month names to numbers map. */
00057     static t_num_map oMap;
00058 
00059     /** Mutex needed for some tm return functions. */
00060     static CdcMutex oMutex;
00061 
00062     /**
00063      * Return the month number by the month short name.
00064      * <p>
00065      * @param str The month 3 letter name.
00066      * @return (int) The month number, or 0 if the string is not a valid month
00067      * name.
00068      */
00069     int GetMonthNumber (const CdcString& str);
00070 
00071     /**
00072      * Parse an HTTP time string.
00073      * <p>
00074      * @param strtime The string to parse.
00075      * @return (CdcHttpTime) The time as a CdcHttpTime object.
00076      * @throws (CdcException) If the string cannot be parsed.
00077      */
00078     CdcHttpTime ParseHttpTime (const CdcString& strtime) throw (CdcException);
00079 
00080     /**
00081      * Parse an ANSI C time string.
00082      * <p>
00083      * @param strtime The string to parse.
00084      * @return (CdcHttpTime) The time as a CdcHttpTime object.
00085      * @throws (CdcException) If the string cannot be parsed.
00086      */
00087     CdcHttpTime ParseAnsiCTime (const CdcString& strtime) throw (CdcException);
00088 
00089 public:
00090 
00091     /**
00092      * Default constructor - makes CdcHttpTime object, that holds the current
00093      * time.
00094      * <p>
00095      * @param gmt If True, the time will be GMT (UTC) time.
00096      */
00097     CdcHttpTime (bool gmt = false);
00098 
00099 
00100     /**
00101      * Construct a CdcHttpTime object from "C" time_t.
00102      * <p>
00103      * @param time The time as an ANSI C time_t value.
00104      * @param gmt If True, the time will be GMT (UTC) time.
00105      */
00106     CdcHttpTime (time_t time, bool gmt = false);
00107 
00108 
00109     /**
00110      * Constructor that builds a CdcHttpTime object from YYYY, MM, DD, HH, MM
00111      * and SS.
00112      */
00113     CdcHttpTime (int year, int month, int day,
00114                  int hour, int min, int sec);
00115 
00116 
00117     /**
00118      * Constructor that gets one of the 3 HTTP formats time string.
00119      * <p>
00120      * @param timestr The time as string.
00121      * @throws (CdcException) If the string cannot be parsed.
00122      */
00123     CdcHttpTime (const CdcString& timestr) throw (CdcException);
00124 
00125 
00126     /**
00127      * Create the same object but in GMT time.
00128      */
00129     CdcHttpTime ToGmt ();
00130 
00131     /**
00132      * Create the same object but in local time.
00133      */
00134     CdcHttpTime ToLocal ();
00135 
00136 
00137     /**
00138      * Create a string from the date and time. see the ANSI C <b>strftime</b>
00139      * for the format details.
00140      */
00141     CdcString Format (const CdcString& format_str) const;
00142 
00143     /**
00144      * Create a string from the GMT date and time. see the ANSI C <b>strftime</b>\
00145      * for the format details.
00146      */
00147     CdcString FormatGmt (const CdcString& format_str) const;
00148 
00149 
00150 
00151     /** Return the seconds of this time object. */
00152     int GetSeconds () const
00153     { return oBrokenTime.tm_sec; }
00154 
00155     /** Return the minutes of this time object. */
00156     int GetMinutes () const
00157     { return oBrokenTime.tm_min; }
00158 
00159     /** Return the hours of this time object. */
00160     int GetHours () const
00161     { return oBrokenTime.tm_hour; }
00162 
00163     /** Return the days of this time object. */
00164     int GetDays () const
00165     { return oBrokenTime.tm_mday; }
00166 
00167     /** Return the week in the year of this time object (0..50). */
00168     int GetWeeks () const
00169     { return (int)(oBrokenTime.tm_yday / 7); }
00170 
00171     /** Return the months of this time object. */
00172     int GetMonths () const
00173     { return oBrokenTime.tm_mon; }
00174 
00175     /** Return the years of this time object. */
00176     int GetYears () const
00177     { return oBrokenTime.tm_year; }
00178 
00179 
00180     /** Add seconds to this time object. */
00181     CdcHttpTime& AddSeconds (int secs);
00182 
00183     /** Add minutes to this time object. */
00184     CdcHttpTime& AddMinutes (int mins);
00185 
00186     /** Add hours to this time object. */
00187     CdcHttpTime& AddHours (int hrs);
00188 
00189     /** Add days to this time object. */
00190     CdcHttpTime& AddDays (int days);
00191 
00192     /** Add months to this time object. */
00193     CdcHttpTime& AddMonths (int months);
00194 
00195     /** Add years to this time object. */
00196     CdcHttpTime& AddYears (int years);
00197 
00198 
00199     /** CdcHttpTime comparisons. */
00200 
00201     bool operator == (const CdcHttpTime& time) const
00202     { return nTheTime == time.nTheTime; }
00203 
00204     bool operator != (const CdcHttpTime& time) const
00205     { return nTheTime != time.nTheTime; }
00206 
00207     bool operator > (const CdcHttpTime& time) const
00208     { return nTheTime > time.nTheTime; }
00209 
00210     bool operator < (const CdcHttpTime& time) const
00211     { return nTheTime < time.nTheTime; }
00212 
00213     bool operator >= (const CdcHttpTime& time) const
00214     { return nTheTime <= time.nTheTime; }
00215 
00216     bool operator <= (const CdcHttpTime& time) const
00217     { return nTheTime <= time.nTheTime; }
00218 
00219 
00220     /**
00221      * Get the different between two times in seconds.
00222      */
00223     double operator - (const CdcHttpTime& time) const
00224     { return difftime (nTheTime, time.nTheTime); }
00225 
00226 
00227     /**
00228      * Static function to get an object that set to the current time.
00229      */
00230     static CdcHttpTime GetCurrentTime ()
00231     { return CdcHttpTime (); }
00232 
00233 
00234     /**
00235      * Static function to get an object that set to the GMT current
00236      * time.
00237      */
00238     static CdcHttpTime GetCurrentTimeGMT ()
00239     { return CdcHttpTime (true); }
00240 
00241     /**
00242      * Initialize the month name to number, map. Static Function.
00243      */
00244     static void Init ();
00245 
00246 };
00247 
00248 #endif /* __CDC_HTTP_TIME */

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