OW_DateTime.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
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; // in 3.0.0
00521 
00522 #if 0
00523 
00529    static Int16 getGMTOffset();
00530    // Removed due to the above problems.  Use toLocal() or
00531    // getGMTOffsetMinutesNow() instead.
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 } // end namespace OW_NAMESPACE
00577 
00578 #endif

Generated on Thu Feb 9 08:47:58 2006 for openwbem by  doxygen 1.4.6