DateTime.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 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 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 } // end namespace BLOCXX_NAMESPACE
00615 
00616 #endif

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6