00001 #ifndef IONFLUX_TOOLS_DATETIME
00002 #define IONFLUX_TOOLS_DATETIME
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <time.h>
00030 #ifdef HAVE_INTTYPES
00031 #include <inttypes.h>
00032 #else
00033 #include <stdint.h>
00034 #endif
00035 #include <cstdlib>
00036 #include <string>
00037 #include <iostream>
00038 #include <sstream>
00039 #include <vector>
00040 #include "ionflux/tools.hpp"
00041 #include "ionflux/TimeZone.hpp"
00042
00043 namespace Ionflux
00044 {
00045
00046 namespace Tools
00047 {
00048
00049 class TimeZone;
00050
00058
00059 typedef int64_t TimeTicks;
00061 typedef int64_t Year;
00062
00067 class DateTime
00068 {
00069 protected:
00071 TimeTicks ticks;
00073 std::string timestamp;
00075 const TimeZone *timeZone;
00077 Year year;
00079 int month;
00081 int day;
00083 int hour;
00085 int minute;
00087 int second;
00089 int weekDay;
00091 int yearDay;
00093 bool dstState;
00095 int utcOffset;
00097 bool manageTimeZone;
00099 std::vector<const TimeZone *> defaultTimeZones;
00100
00107 virtual void parseTimestamp(const std::string &source);
00108
00117 virtual std::string createTimestamp();
00118
00120 virtual void updateTicks();
00121
00129 virtual void updateBrokenDownTime(int tickOffset);
00130
00132 virtual void initTimeZones();
00133
00134 public:
00136 static const std::string TIMESTAMP_DATE_SEP;
00138 static const std::string TIMESTAMP_TIME_SEP;
00140 static const std::string TIMESTAMP_DATETIME_SEP;
00144 static const int NUM_DAYS[];
00146 static const int LEAP_MONTH;
00148 static const TimeTicks POSIX_EPOCH;
00150 static const TimeTicks TICKS_400Y;
00152 static const TimeTicks TICKS_100Y;
00154 static const TimeTicks TICKS_4Y;
00156 static const TimeTicks TICKS_3Y;
00158 static const TimeTicks TICKS_1Y;
00160 static const TimeTicks TICKS_1D;
00162 static const TimeTicks REF_SUNDAY;
00164 static const char *WEEKDAY_NAME[];
00166 static const char *WEEKDAY_NAME_SHORT[];
00168 static const char *MONTH_NAME[];
00170 static const char *MONTH_NAME_SHORT[];
00172 static const TimeZone TZ_UTC;
00174 static const TimeZone TZ_UTC_P0100;
00176 static const TimeZone TZ_UTC_P0200;
00178 static const TimeZone TZ_UTC_P0300;
00180 static const TimeZone TZ_UTC_P0330;
00182 static const TimeZone TZ_UTC_P0400;
00184 static const TimeZone TZ_UTC_P0430;
00186 static const TimeZone TZ_UTC_P0500;
00188 static const TimeZone TZ_UTC_P0530;
00190 static const TimeZone TZ_UTC_P0545;
00192 static const TimeZone TZ_UTC_P0600;
00194 static const TimeZone TZ_UTC_P0630;
00196 static const TimeZone TZ_UTC_P0700;
00198 static const TimeZone TZ_UTC_P0800;
00200 static const TimeZone TZ_UTC_P0900;
00202 static const TimeZone TZ_UTC_P0930;
00204 static const TimeZone TZ_UTC_P1000;
00206 static const TimeZone TZ_UTC_P1100;
00208 static const TimeZone TZ_UTC_P1200;
00210 static const TimeZone TZ_UTC_M0100;
00212 static const TimeZone TZ_UTC_M0200;
00214 static const TimeZone TZ_UTC_M0300;
00216 static const TimeZone TZ_UTC_M0400;
00218 static const TimeZone TZ_UTC_M0500;
00220 static const TimeZone TZ_UTC_M0600;
00222 static const TimeZone TZ_UTC_M0700;
00224 static const TimeZone TZ_UTC_M0800;
00226 static const TimeZone TZ_UTC_M0900;
00228 static const TimeZone TZ_UTC_M1000;
00230 static const TimeZone TZ_UTC_M1100;
00232 static const TimeZone TZ_UTC_M1200;
00234 static const TimeZone TZ_GMT;
00236 static const TimeZone TZ_EUROPE_WESTERN;
00238 static const TimeZone TZ_EUROPE_CENTRAL;
00240 static const TimeZone TZ_EUROPE_EASTERN;
00242 static const TimeZone TZ_UK;
00244 static const TimeZone TZ_USA_EASTERN;
00246 static const TimeZone TZ_USA_CENTRAL;
00248 static const TimeZone TZ_USA_MOUNTAIN;
00250 static const TimeZone TZ_USA_PACIFIC;
00252 static const TimeZone TZ_USA_ALASKA;
00254 static const TimeZone TZ_RUSSIA_WESTERN;
00256 static const TimeZone TZ_EGYPT;
00257
00263 DateTime();
00264
00272 DateTime(time_t initTime);
00273
00281 DateTime(TimeTicks initTime);
00282
00290 DateTime(struct tm initTime);
00291
00300 DateTime(const std::string &initTime);
00301
00309 DateTime(const DateTime &initTime);
00310
00323 DateTime(int initYear, int initMonth, int initDay, int initHour,
00324 int initMinute, int initSecond);
00325
00330 virtual ~DateTime();
00331
00340 virtual void setTime(time_t newTime);
00341
00350 virtual void setTime(TimeTicks newTime);
00351
00360 virtual void setTime(tm newTime);
00361
00370 virtual void setTime(const std::string &newTime);
00371
00380 virtual void setTime(const DateTime &newTime);
00381
00395 virtual void setTime(int newYear, int newMonth, int newDay,
00396 int newHour, int newMinute, int newSecond);
00397
00414 virtual void setLocalTime(const DateTime &newTime, bool newDSTState);
00415
00426 virtual void setLocalTime(const std::string &newTime, bool newDSTState);
00427
00443 virtual void setLocalTime(int newYear, int newMonth, int newDay,
00444 int newHour, int newMinute, int newSecond, bool newDSTState);
00445
00463 virtual void setLocalTime(const DateTime &newTime);
00464
00478 virtual void setLocalTime(const std::string &newTime);
00479
00498 virtual void setLocalTime(int newYear, int newMonth, int newDay,
00499 int newHour, int newMinute, int newSecond);
00500
00509 virtual void setHMS(int newHour, int newMinute, int newSecond);
00510
00517 virtual void setHMS(const std::string &newTime);
00518
00525 virtual void setHMS(const DateTime &sourceTime);
00526
00539 virtual void setLocalHMS(int newHour, int newMinute, int newSecond,
00540 bool newDSTState);
00541
00552 virtual void setLocalHMS(const std::string &newTime, bool newDSTState);
00553
00564 virtual void setLocalHMS(const DateTime &sourceTime, bool newDSTState);
00565
00581 virtual void setLocalHMS(int newHour, int newMinute, int newSecond);
00582
00596 virtual void setLocalHMS(const std::string &newTime);
00597
00611 virtual void setLocalHMS(const DateTime &sourceTime);
00612
00625 static void makeBrokenTime(time_t *source, tm *target);
00626
00636 static Year countLeapYears(Year limit);
00637
00647 static bool isLeapYear(Year checkYear);
00648
00665 static DateTime getNth(Year targetYear, int targetMonth,
00666 int targetWeekDay, int n);
00667
00677 virtual DateTime &shift(int shiftSeconds);
00678
00687 virtual TimeTicks diff(const DateTime &diffTime) const;
00688
00697 virtual void setTimeZone(const TimeZone *newTimeZone, bool newManage);
00698
00706 virtual void setTimeZone(const std::string &newTimeZone);
00707
00721 virtual int compareHMS(int otherHour, int otherMinute, int otherSecond);
00722
00734 virtual int compareHMS(const DateTime &otherTime);
00735
00748 virtual int compareHMS(const std::string &otherTime);
00749
00759 virtual int compareDay(const DateTime &otherTime);
00760
00772 static bool isTime(const std::string &checkTime);
00773
00786 static bool isDate(const std::string &checkDate);
00787
00801 static bool isDatetime(const std::string &checkDatetime);
00802
00809 virtual DateTime getUTC();
00810
00818 virtual TimeTicks getTicks() const;
00819
00831 virtual time_t getTime() const;
00832
00845 virtual tm getBrokenTime() const;
00846
00853 virtual std::string getTimestamp();
00854
00861 virtual std::string getHMS();
00862
00867 virtual Year getYear() const;
00868
00873 virtual int getMonth() const;
00874
00879 virtual int getDay() const;
00880
00885 virtual int getHour() const;
00886
00891 virtual int getMinute() const;
00892
00897 virtual int getSecond() const;
00898
00903 virtual int getDayOfWeek() const;
00904
00909 virtual int getDayOfYear() const;
00910
00915 virtual std::string getDayOfWeekName() const;
00916
00921 virtual std::string getDayOfWeekShortName() const;
00922
00927 virtual std::string getMonthName() const;
00928
00933 virtual std::string getMonthShortName() const;
00934
00940 virtual const TimeZone *getTimeZone() const;
00941
00947 virtual std::string getTimeZoneName() const;
00948
00957 virtual std::string getTimeZoneAliases() const;
00958
00964 virtual bool isDST() const;
00965
00970 virtual int getUTCOffset() const;
00971
00976 virtual std::string getRFCTimestamp() const;
00977
00979 virtual DateTime& operator=(const DateTime &newTime);
00980
00982 virtual DateTime& operator+=(TimeTicks seconds);
00983
00985 virtual DateTime& operator-=(TimeTicks seconds);
00986
00988 virtual TimeTicks operator-(const DateTime &diffTime) const;
00989
00991 virtual bool operator==(const DateTime &compTime) const;
00992
00994 virtual bool operator>(const DateTime &compTime) const;
00995
00997 virtual bool operator<(const DateTime &compTime) const;
00998
01000 virtual bool operator>=(const DateTime &compTime) const;
01001
01003 virtual bool operator<=(const DateTime &compTime) const;
01004 };
01005
01007
01008 }
01009
01010 }
01011
01015 #endif