00001
00002
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
00030
00036 #ifndef OW_DATETIME_HPP_INCLUDE_GUARD_
00037 #define OW_DATETIME_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_Exception.hpp"
00040 #include "OW_Types.hpp"
00041 #include "OW_CommonFwd.hpp"
00042
00043 extern "C"
00044 {
00045 #include <time.h>
00046 }
00047
00048 namespace OW_NAMESPACE
00049 {
00050
00051 OW_DECLARE_APIEXCEPTION(DateTime, OW_COMMON_API)
00052
00053
00077 class OW_COMMON_API DateTime
00078 {
00079 public:
00087 enum ETimeOffset
00088 {
00089 E_LOCAL_TIME,
00090 E_UTC_TIME
00091 };
00092
00097 DateTime();
00175 explicit DateTime(const String& str);
00185 explicit DateTime(time_t t, UInt32 microseconds=0);
00198 DateTime(int year, int month, int day, int hour=0, int minute=0,
00199 int second=0, UInt32 microsecond=0, ETimeOffset timeOffset = E_LOCAL_TIME);
00203 ~DateTime();
00210 int getHour(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00217 int getMinute(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00225 int getSecond(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00233 UInt32 getMicrosecond() const;
00240 int getDay(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00246 int getDow(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00251 int getMonth(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00256 int getYear(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00260 time_t get() const;
00267 void setHour(int hour, ETimeOffset timeOffset = E_LOCAL_TIME);
00274 void setMinute(int minute, ETimeOffset timeOffset = E_LOCAL_TIME);
00281 void setSecond(int second, ETimeOffset timeOffset = E_LOCAL_TIME);
00288 void setMicrosecond(UInt32 microsecond);
00297 void setTime(int hour, int minute, int second, ETimeOffset timeOffset = E_LOCAL_TIME);
00304 void setDay(int day, ETimeOffset timeOffset = E_LOCAL_TIME);
00311 void setMonth(int month, ETimeOffset timeOffset = E_LOCAL_TIME);
00318 void setYear(int year, ETimeOffset timeOffset = E_LOCAL_TIME);
00326 void set(time_t t, UInt32 microseconds=0);
00339 void set(int year, int month, int day, int hour, int minute, int second, UInt32 microseconds, ETimeOffset timeOffset = E_LOCAL_TIME);
00343 void setToCurrent();
00349 void addDays(int days);
00355 void addWeeks(int weeks)
00356 {
00357 addDays(weeks * 7);
00358 }
00364 void addMonths(int months);
00370 void addYears(int years);
00376 void addSeconds(long seconds)
00377 {
00378 m_time += seconds;
00379 }
00384 void addMinutes(long minutes)
00385 {
00386 m_time += minutes * 60;
00387 }
00391 void addMicroseconds(long microseconds)
00392 {
00393 m_microseconds += microseconds;
00394 m_time += m_microseconds / 1000000;
00395 m_microseconds %= 1000000;
00396 }
00400 void addMilliseconds(long milliseconds)
00401 {
00402 this->addMicroseconds(milliseconds * 1000);
00403 }
00408 void addHours(long hours) { m_time += hours * 60 * 60; }
00414 bool operator< ( const DateTime& tm ) const
00415 {
00416 if (m_time == tm.m_time)
00417 {
00418 return m_microseconds < tm.m_microseconds;
00419 }
00420 return m_time < tm.m_time;
00421 }
00427 bool operator> ( const DateTime& tm ) const
00428 {
00429 return tm < *this;
00430 }
00436 bool operator== ( const DateTime& tm ) const
00437 {
00438 return m_time == tm.m_time && m_microseconds == tm.m_microseconds;
00439 }
00445 bool operator!= ( const DateTime& tm ) const
00446 {
00447 return !(*this == tm);
00448 }
00455 bool operator<= ( const DateTime& tm ) const
00456 {
00457 return !(tm < *this);
00458 }
00465 bool operator>= ( const DateTime& tm ) const
00466 {
00467 return !(*this < tm);
00468 }
00474 DateTime& operator+= (long seconds)
00475 {
00476 addSeconds(seconds);
00477 return *this;
00478 }
00484 DateTime& operator-= (long seconds)
00485 {
00486 addSeconds(-seconds);
00487 return *this;
00488 }
00489
00495 String toString(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00496
00506 String toString(
00507 char const * format, ETimeOffset timeOffset = E_LOCAL_TIME) const;
00508
00514 static char const DEFAULT_FORMAT[];
00515
00520 String toStringGMT() const OW_DEPRECATED;
00521
00522 #if 0
00523
00529 static Int16 getGMTOffset();
00530
00531
00532 #endif
00533
00539 static Int16 getGMTOffsetMinutesNow()
00540 {
00541 time_t t = time(0);
00542 struct tm tt;
00543 return DateTime::localTimeAndOffset(t, tt);
00544 }
00545
00551 Int16 toLocal(struct tm & tt) const
00552 {
00553 return DateTime::localTimeAndOffset(m_time, tt);
00554 }
00555
00559 static DateTime getCurrent();
00560
00561 private:
00562 time_t m_time;
00563 UInt32 m_microseconds;
00564 tm getTm(ETimeOffset timeOffset) const;
00565 void setTime(tm& tmarg, ETimeOffset timeOffset);
00566 static Int16 localTimeAndOffset(time_t t, struct tm & tt);
00567 };
00568
00574 DateTime operator-(DateTime const & x, DateTime const & y);
00575
00576 }
00577
00578 #endif