PLearn 0.1
PDateTime.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PDateTime
00004 // Copyright (c) 2002 by Nicolas Chapados
00005 
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 // 
00009 //  1. Redistributions of source code must retain the above copyright
00010 //     notice, this list of conditions and the following disclaimer.
00011 // 
00012 //  2. Redistributions in binary form must reproduce the above copyright
00013 //     notice, this list of conditions and the following disclaimer in the
00014 //     documentation and/or other materials provided with the distribution.
00015 // 
00016 //  3. The name of the authors may not be used to endorse or promote
00017 //     products derived from this software without specific prior written
00018 //     permission.
00019 // 
00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 // 
00031 // This file is part of the PLearn library. For more information on the PLearn
00032 // library, go to the PLearn Web site at www.plearn.org
00033 
00034 /* *******************************************************      
00035  * $Id: PDateTime.h 8243 2007-11-12 18:11:30Z nouiz $
00036  * This file is part of the PLearn library.
00037  ******************************************************* */
00038 
00039 
00042 #ifndef PDateTime_INC
00043 #define PDateTime_INC
00044 
00045 #include <iostream>
00046 #include <string>
00047 
00048 namespace PLearn {
00049 using namespace std;
00050 
00051 #define SECONDS_PER_DAY 86400 // 24*60*60
00052 
00062 class PDateTime
00063 {
00064 public:
00065     short year; 
00066     unsigned char month; 
00067     unsigned char day; 
00068     unsigned char hour; 
00069     unsigned char min;  
00070     unsigned char sec;  
00071 
00073     PDateTime();
00074 
00076     PDateTime(short the_year, unsigned char the_month, unsigned char the_day)
00077         : year(the_year), month(the_month), day(the_day),
00078           hour(0), min(0), sec(0) {}
00079 
00080     PDateTime(int the_year, int the_month, int the_day)
00081         : year(the_year), month(int(the_month)), day(int(the_day)),
00082           hour(0), min(0), sec(0) {}
00083 
00085     PDateTime(short the_year, unsigned char the_month, unsigned char the_day,
00086               unsigned char the_hour, unsigned char the_min,
00087               unsigned char the_sec)
00088         : year(the_year), month(the_month), day(the_day),
00089           hour(the_hour), min(the_min), sec(the_sec) {}
00090   
00091     PDateTime(int the_year, int the_month, int the_day,
00092               int the_hour, int the_min, int the_sec)
00093         : year(the_year), month(the_month), day(the_day),
00094           hour(the_hour), min(the_min), sec(the_sec) {}
00095   
00099     PDateTime(double julian_day);
00100 
00103     PDateTime(string datetime);
00104   
00106     bool isMissing() const;
00107     void setMissing();
00108 
00110     string info() const; 
00111 
00113     bool operator==(const PDateTime& rhs) const {
00114         return year == rhs.year && month == rhs.month && day == rhs.day
00115             && hour == rhs.hour && min == rhs.min && sec == rhs.sec;
00116     }
00117   
00118     bool operator!=(const PDateTime& rhs) const {
00119         return ! (*this == rhs);
00120     }
00121   
00122     bool operator<(const PDateTime& rhs) const {
00123         double datetime_to_double(const PDateTime& t); // declare the function
00124         return datetime_to_double(*this) < datetime_to_double(rhs);
00125     }
00126   
00127     bool operator<=(const PDateTime& rhs) const {
00128         return *this == rhs || *this < rhs;
00129     }
00130   
00131     bool operator>(const PDateTime& rhs) const {
00132         return ! (*this <= rhs);
00133     }
00134   
00135     bool operator>=(const PDateTime& rhs) const {
00136         return ! (*this < rhs);
00137     }
00138 
00139     bool sameDay(const PDateTime& rhs) const {
00140         return day   == rhs.day && month == rhs.month && year  == rhs.year;
00141     }
00142     
00147     double toJulianDay() const;
00148 
00150     void incSecond  (int sec_inc);
00152     void incMinute  (int min_inc);
00154     void incHour    (int hour_inc);
00156     void incDay     (int day_inc);
00157 
00160     static PDateTime currentLocalTime();
00161 };
00162 
00164 inline double operator-(const PDateTime& to_date, const PDateTime& from_date)
00165 {
00166     return to_date.toJulianDay() - from_date.toJulianDay();
00167 }
00168 
00169 inline ostream& operator<<(ostream& os, const PDateTime& date)
00170 {
00171     os << date.info();
00172     return os;
00173 }
00174   
00179 double datetime_to_double(const PDateTime& t);
00180 PDateTime double_to_datetime(double f);
00181 
00183 double hhmmss_to_double(int hh, int mm, int ss);
00184 
00186 void double_to_hhmmss(double fraction, int& hh, int& mm, int& ss);
00187 
00190 int delta_seconds(const PDateTime& current, const PDateTime& past);
00191 
00192 } // end of namespace PLearn
00193 
00194 #endif
00195 
00196 
00197 /*
00198   Local Variables:
00199   mode:c++
00200   c-basic-offset:4
00201   c-file-style:"stroustrup"
00202   c-file-offsets:((innamespace . 0)(inline-open . 0))
00203   indent-tabs-mode:nil
00204   fill-column:79
00205   End:
00206 */
00207 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines