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
00031
00032
00038 #ifndef BLOCXX_DATETIME_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_DATETIME_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/Exception.hpp"
00042 #include "blocxx/Types.hpp"
00043 #include "blocxx/CommonFwd.hpp"
00044
00045 extern "C"
00046 {
00047 #include <time.h>
00048 }
00049
00050 namespace BLOCXX_NAMESPACE
00051 {
00052
00053 BLOCXX_DECLARE_APIEXCEPTION(DateTime, BLOCXX_COMMON_API)
00054
00055
00079 class BLOCXX_COMMON_API DateTime
00080 {
00081 public:
00086 enum ETimeOffset
00087 {
00092 E_LOCAL_TIME,
00096 E_UTC_TIME
00097 };
00102 DateTime();
00180 explicit DateTime(const String& str);
00190 explicit DateTime(time_t t, UInt32 microseconds=0);
00207 DateTime(
00208 int year,
00209 int month,
00210 int day,
00211 int hour=0,
00212 int minute=0,
00213 int second=0,
00214 UInt32 microsecond=0,
00215 ETimeOffset timeOffset = E_LOCAL_TIME);
00219 ~DateTime();
00225 int getHour(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00231 int getMinute(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00239 int getSecond(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00247 UInt32 getMicrosecond() const;
00254 int getDay(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00261 int getDow(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00267 int getMonth(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00273 int getYear(ETimeOffset timeOffset = E_LOCAL_TIME) const;
00278 time_t get() const;
00286 void setHour(int hour, ETimeOffset timeOffset = E_LOCAL_TIME);
00294 void setMinute(int minute, ETimeOffset timeOffset = E_LOCAL_TIME);
00302 void setSecond(
00303 int second,
00304 ETimeOffset timeOffset = E_LOCAL_TIME);
00311 void setMicrosecond(
00312 UInt32 microsecond);
00322 void setTime(
00323 int hour,
00324 int minute,
00325 int second,
00326 ETimeOffset timeOffset = E_LOCAL_TIME);
00334 void setDay(
00335 int day,
00336 ETimeOffset timeOffset = E_LOCAL_TIME);
00345 void setMonth(
00346 int month,
00347 ETimeOffset timeOffset = E_LOCAL_TIME);
00356 void setYear(
00357 int year,
00358 ETimeOffset timeOffset = E_LOCAL_TIME);
00368 void set(time_t t, UInt32 microseconds=0);
00384 void set(int year, int month, int day, int hour, int minute, int second, UInt32 microseconds, ETimeOffset timeOffset = E_LOCAL_TIME);
00388 void setToCurrent();
00395 void addDays(int days);
00402 void addWeeks(int weeks)
00403 {
00404 addDays(weeks * 7);
00405 }
00412 void addMonths(int months);
00419 void addYears(int years);
00426 void addSeconds(long seconds)
00427 {
00428 m_time += seconds;
00429 }
00434 void addMinutes(long minutes)
00435 {
00436 m_time += minutes * 60;
00437 }
00441 void addMicroseconds(long microseconds)
00442 {
00443 m_microseconds += microseconds;
00444 m_time += m_microseconds / 1000000;
00445 m_microseconds %= 1000000;
00446 }
00450 void addMilliseconds(long milliseconds)
00451 {
00452 this->addMicroseconds(milliseconds * 1000);
00453 }
00458 void addHours(long hours) { m_time += hours * 60 * 60; }
00464 bool operator< ( const DateTime& tm ) const
00465 {
00466 if (m_time == tm.m_time)
00467 {
00468 return m_microseconds < tm.m_microseconds;
00469 }
00470 return m_time < tm.m_time;
00471 }
00477 bool operator> ( const DateTime& tm ) const
00478 {
00479 return tm < *this;
00480 }
00486 bool operator== ( const DateTime& tm ) const
00487 {
00488 return m_time == tm.m_time && m_microseconds == tm.m_microseconds;
00489 }
00495 bool operator!= ( const DateTime& tm ) const
00496 {
00497 return !(*this == tm);
00498 }
00505 bool operator<= ( const DateTime& tm ) const
00506 {
00507 return !(tm < *this);
00508 }
00515 bool operator>= ( const DateTime& tm ) const
00516 {
00517 return !(*this < tm);
00518 }
00524 DateTime& operator+= (long seconds)
00525 {
00526 addSeconds(seconds);
00527 return *this;
00528 }
00534 DateTime& operator-= (long seconds)
00535 {
00536 addSeconds(-seconds);
00537 return *this;
00538 }
00539
00546 String toString(
00547 ETimeOffset timeOffset = E_LOCAL_TIME) const;
00548
00559 String toString(
00560 char const * format,
00561 ETimeOffset timeOffset = E_LOCAL_TIME) const;
00562
00568 static char const DEFAULT_FORMAT[];
00569
00576 static Int16 getGMTOffsetMinutesNow()
00577 {
00578 time_t t = time(0);
00579 struct tm tt;
00580 return DateTime::localTimeAndOffset(t, tt);
00581 }
00582
00588 Int16 toLocal(struct tm & tt) const
00589 {
00590 return DateTime::localTimeAndOffset(m_time, tt);
00591 }
00592
00596 static DateTime getCurrent();
00597
00598 private:
00599 time_t m_time;
00600 UInt32 m_microseconds;
00601
00602 tm getTm(ETimeOffset timeOffset) const;
00603 void setTime(tm& tmarg, ETimeOffset timeOffset);
00604 static Int16 localTimeAndOffset(time_t t, struct tm & tt);
00605 };
00606
00612 DateTime operator-(DateTime const & x, DateTime const & y);
00613
00614 }
00615
00616 #endif