blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 00031 *******************************************************************************/ 00032 00033 00039 #ifndef BLOCXX_DATETIME_HPP_INCLUDE_GUARD_ 00040 #define BLOCXX_DATETIME_HPP_INCLUDE_GUARD_ 00041 #include "blocxx/BLOCXX_config.h" 00042 #include "blocxx/Exception.hpp" 00043 #include "blocxx/Types.hpp" 00044 #include "blocxx/CommonFwd.hpp" 00045 00046 extern "C" 00047 { 00048 #include <time.h> 00049 } 00050 00051 namespace BLOCXX_NAMESPACE 00052 { 00053 00054 BLOCXX_DECLARE_APIEXCEPTION(DateTime, BLOCXX_COMMON_API) 00055 00056 00080 class BLOCXX_COMMON_API DateTime 00081 { 00082 public: 00090 enum ETimeOffset 00091 { 00092 E_LOCAL_TIME, 00093 E_UTC_TIME 00094 }; 00095 00100 DateTime(); 00178 explicit DateTime(const String& str); 00189 explicit DateTime(time_t t, UInt32 microseconds=0); 00205 DateTime( 00206 int year, 00207 int month, 00208 int day, 00209 int hour=0, 00210 int minute=0, 00211 int second=0, 00212 UInt32 microsecond=0, 00213 ETimeOffset timeOffset = E_LOCAL_TIME); 00217 ~DateTime(); 00224 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; 00260 int getDow(ETimeOffset timeOffset = E_LOCAL_TIME) const; 00265 int getMonth(ETimeOffset timeOffset = E_LOCAL_TIME) const; 00270 int getYear(ETimeOffset timeOffset = E_LOCAL_TIME) const; 00274 time_t get() const; 00282 void setHour(int hour, ETimeOffset timeOffset = E_LOCAL_TIME); 00290 void setMinute(int minute, ETimeOffset timeOffset = E_LOCAL_TIME); 00298 void setSecond(int second, ETimeOffset timeOffset = E_LOCAL_TIME); 00305 void setMicrosecond(UInt32 microsecond); 00315 void setTime( 00316 int hour, 00317 int minute, 00318 int second, 00319 ETimeOffset timeOffset = E_LOCAL_TIME); 00327 void setDay(int day, ETimeOffset timeOffset = E_LOCAL_TIME); 00336 void setMonth(int month, ETimeOffset timeOffset = E_LOCAL_TIME); 00345 void setYear(int year, ETimeOffset timeOffset = E_LOCAL_TIME); 00355 void set(time_t t, UInt32 microseconds=0); 00371 void set( 00372 int year, 00373 int month, 00374 int day, 00375 int hour, 00376 int minute, 00377 int second, 00378 UInt32 microseconds, 00379 ETimeOffset timeOffset = E_LOCAL_TIME); 00383 void setToCurrent(); 00390 void addDays(int days); 00397 void addWeeks(int weeks) 00398 { 00399 addDays(weeks * 7); 00400 } 00407 void addMonths(int months); 00414 void addYears(int years); 00421 void addSeconds(long seconds) 00422 { 00423 m_time += seconds; 00424 } 00429 void addMinutes(long minutes) 00430 { 00431 m_time += minutes * 60; 00432 } 00437 void addMicroseconds(long microseconds) 00438 { 00439 m_microseconds += microseconds; 00440 m_time += m_microseconds / 1000000; 00441 m_microseconds %= 1000000; 00442 } 00447 void addMilliseconds(long milliseconds) 00448 { 00449 this->addMicroseconds(milliseconds * 1000); 00450 } 00455 void addHours(long hours) { m_time += hours * 60 * 60; } 00461 bool operator< ( const DateTime& tm ) const 00462 { 00463 if (m_time == tm.m_time) 00464 { 00465 return m_microseconds < tm.m_microseconds; 00466 } 00467 return m_time < tm.m_time; 00468 } 00475 bool operator> ( const DateTime& tm ) const 00476 { 00477 return tm < *this; 00478 } 00484 bool operator== ( const DateTime& tm ) const 00485 { 00486 return m_time == tm.m_time && m_microseconds == tm.m_microseconds; 00487 } 00493 bool operator!= ( const DateTime& tm ) const 00494 { 00495 return !(*this == tm); 00496 } 00503 bool operator<= ( const DateTime& tm ) const 00504 { 00505 return !(tm < *this); 00506 } 00513 bool operator>= ( const DateTime& tm ) const 00514 { 00515 return !(*this < tm); 00516 } 00522 DateTime& operator+= (long seconds) 00523 { 00524 addSeconds(seconds); 00525 return *this; 00526 } 00532 DateTime& operator-= (long seconds) 00533 { 00534 addSeconds(-seconds); 00535 return *this; 00536 } 00537 00543 String toString(ETimeOffset timeOffset = E_LOCAL_TIME) const; 00544 00555 String toString( 00556 char const * format, ETimeOffset timeOffset = E_LOCAL_TIME) const; 00557 00563 static char const DEFAULT_FORMAT[]; 00564 00569 String toStringGMT() const BLOCXX_DEPRECATED; // in 3.0.0 00570 00571 #if 0 00572 00578 static Int16 getGMTOffset(); 00579 // Removed due to the above problems. Use toLocal() or 00580 // getGMTOffsetMinutesNow() instead. 00581 #endif 00582 00588 static Int16 getGMTOffsetMinutesNow() 00589 { 00590 time_t t = time(0); 00591 struct tm tt; 00592 return DateTime::localTimeAndOffset(t, tt); 00593 } 00594 00601 Int16 toLocal(struct tm & tt) const 00602 { 00603 return DateTime::localTimeAndOffset(m_time, tt); 00604 } 00605 00609 static DateTime getCurrent(); 00610 00611 private: 00612 time_t m_time; 00613 UInt32 m_microseconds; 00614 tm getTm(ETimeOffset timeOffset) const; 00615 void setTime(tm& tmarg, ETimeOffset timeOffset); 00616 static Int16 localTimeAndOffset(time_t t, struct tm & tt); 00617 }; 00618 00624 BLOCXX_COMMON_API DateTime operator-(DateTime const & x, DateTime const & y); 00625 00626 } // end namespace BLOCXX_NAMESPACE 00627 00628 #endif 00629