PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes
PLearn::PDateTime Class Reference

#include <PDateTime.h>

Collaboration diagram for PLearn::PDateTime:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 PDateTime ()
 Create a missing date by default.
 PDateTime (short the_year, unsigned char the_month, unsigned char the_day)
 Create a date only, with time assumed to be midnight.
 PDateTime (int the_year, int the_month, int the_day)
 PDateTime (short the_year, unsigned char the_month, unsigned char the_day, unsigned char the_hour, unsigned char the_min, unsigned char the_sec)
 Create a full date/time.
 PDateTime (int the_year, int the_month, int the_day, int the_hour, int the_min, int the_sec)
 PDateTime (double julian_day)
 convert a Julian day into a PDateTime.
 PDateTime (string datetime)
 Convert a string in "YYYY/MM/DD" or "YYYY/MM/DD hh:mm:ss" into a PDateTime.
bool isMissing () const
 Missing date/time handling.
void setMissing ()
 returns the date in the format yyyy/mm/dd hh:mm:ss
string info () const
bool operator== (const PDateTime &rhs) const
 Equality comparison.
bool operator!= (const PDateTime &rhs) const
bool operator< (const PDateTime &rhs) const
bool operator<= (const PDateTime &rhs) const
bool operator> (const PDateTime &rhs) const
bool operator>= (const PDateTime &rhs) const
bool sameDay (const PDateTime &rhs) const
double toJulianDay () const
void incSecond (int sec_inc)
 Increment the current date by a given number of seconds.
void incMinute (int min_inc)
 Increment the current date by a given number of minutes.
void incHour (int hour_inc)
 Increment the current date by a given number of hours.
void incDay (int day_inc)
 Increment the current date by a given number of days.

Static Public Member Functions

static PDateTime currentLocalTime ()
 Return a PDateTime object corresponding to the current local date and time.

Public Attributes

short year
 ex: 1983, 2001, -350, ...
unsigned char month
 1..12
unsigned char day
 1..31
unsigned char hour
 00..23
unsigned char min
 00..59
unsigned char sec
 00..61, allowing for leap seconds (!)

Detailed Description

PDateTime must be a concrete class with no virtual function. Hence it MUST NOT UNDER ANY CONSIDERATION UNDER HEAVY PRISON PENALTY derive from Object. The reason that PDateTime is different from the related PDate (which stores only a date) is that some clients (e.g. SimpleDB and friends) make fairly stringent assumptions on the memory layout of a PDate, and leave no room for time. Also, a PDateTime should really be considered a "time-point", and for added precision converts itself to a double.

Definition at line 62 of file PDateTime.h.


Constructor & Destructor Documentation

PLearn::PDateTime::PDateTime ( )

Create a missing date by default.

Definition at line 49 of file PDateTime.cc.

{
    setMissing();
}
PLearn::PDateTime::PDateTime ( short  the_year,
unsigned char  the_month,
unsigned char  the_day 
) [inline]

Create a date only, with time assumed to be midnight.

Definition at line 76 of file PDateTime.h.

        : year(the_year), month(the_month), day(the_day),
          hour(0), min(0), sec(0) {}
PLearn::PDateTime::PDateTime ( int  the_year,
int  the_month,
int  the_day 
) [inline]

Definition at line 80 of file PDateTime.h.

        : year(the_year), month(int(the_month)), day(int(the_day)),
          hour(0), min(0), sec(0) {}
PLearn::PDateTime::PDateTime ( short  the_year,
unsigned char  the_month,
unsigned char  the_day,
unsigned char  the_hour,
unsigned char  the_min,
unsigned char  the_sec 
) [inline]

Create a full date/time.

Definition at line 85 of file PDateTime.h.

        : year(the_year), month(the_month), day(the_day),
          hour(the_hour), min(the_min), sec(the_sec) {}
PLearn::PDateTime::PDateTime ( int  the_year,
int  the_month,
int  the_day,
int  the_hour,
int  the_min,
int  the_sec 
) [inline]

Definition at line 91 of file PDateTime.h.

        : year(the_year), month(the_month), day(the_day),
          hour(the_hour), min(the_min), sec(the_sec) {}
PLearn::PDateTime::PDateTime ( double  julian_day)

convert a Julian day into a PDateTime.

Day 0 of the Julian calendar is about 6000 years ago... The julian day is passed as a DOUBLE, to allow specifying hours/minutes/seconds as a FRACTION of days.

Definition at line 54 of file PDateTime.cc.

References PLearn::double_to_hhmmss(), and PLearn::min().

{
    int jw = (int)((julian_day - 1867216.25)/36524.25);
    int jx = (int)(jw/4);
    int ja = (int)(julian_day + 1 + jw - jx);
    int jb = ja + 1524;
    int jc = (int)((jb - 122.1)/365.25);
    int jd = (int)(365.25*jc);
    int je = (int)((jb - jd)/30.6001);
    int jf = (int)(30.6001*je);

    day = jb - jd - jf;
    month = (je>13) ? je-13 : je-1;
    year = (month>2) ? jc-4716 : jc-4715;

    double fraction = julian_day - floor(julian_day);
    int hh,mm,ss;
    double_to_hhmmss(fraction,hh,mm,ss);
    hour = hh;
    min = mm;
    sec = ss;
}

Here is the call graph for this function:

PLearn::PDateTime::PDateTime ( string  datetime)

Convert a string in "YYYY/MM/DD" or "YYYY/MM/DD hh:mm:ss" into a PDateTime.

The format has to match exactly.

Definition at line 77 of file PDateTime.cc.

References PLearn::min(), PLERROR, and PLearn::toint().

{
    // Parse either "YYYY/MM/DD" or "YYYY/MM/DD hh:mm:ss" format
    if ((date.size() == 10 || date.size() == 19) &&
        date[4] == '/' && date[7] == '/' &&
        (date.size() == 19? date[13] == ':' && date[16] == ':' : true)) {
        year = toint(date.substr(0,4));
        month = toint(date.substr(5,2));
        day = toint(date.substr(8,2));
        if (date.size() == 10)
            hour = min = sec = 0;
        else {
            hour = toint(date.substr(11,2));
            min  = toint(date.substr(14,2));
            sec  = toint(date.substr(17,2));
        }
    }
    else
        PLERROR("PDateTime::PDateTime: the passed date-time string is not in "
                "\"YYYY/MM/DD\" or \"YYYY/MM/DD hh:mm:ss\" format");
}

Here is the call graph for this function:


Member Function Documentation

PDateTime PLearn::PDateTime::currentLocalTime ( ) [static]

Return a PDateTime object corresponding to the current local date and time.

Definition at line 198 of file PDateTime.cc.

Referenced by PLearn::TestClientCommand::run().

                                      {
    time_t current;
    tm* ptr;
    time(&current);
    ptr = ::localtime(&current);
    return PDateTime(ptr->tm_year + 1900, ptr->tm_mon + 1, ptr->tm_mday,
                     ptr->tm_hour, ptr->tm_min, ptr->tm_sec);
}

Here is the caller graph for this function:

void PLearn::PDateTime::incDay ( int  day_inc)

Increment the current date by a given number of days.

Definition at line 153 of file PDateTime.cc.

References PLASSERT, and PLERROR.

{
    PLASSERT( day_inc >= 0 );
    int new_day = int(day) + day_inc;
    int max_day = 31;
    if (month == 2) {
        if (year < 1900 || year > 2100)
            PLERROR("In PDateTime::incDay - This code is probably not going to"
                    " work for year %d", int(year));
        max_day = year % 4 == 0 ? 29 : 28;
    } else if (month == 4 || month == 6 || month == 9 || month == 11)
        max_day = 30;
    if (new_day <= max_day) {
        day = (unsigned char) new_day;
        return;
    }
    // We need to switch to next month.
    PLASSERT( max_day >= int(day) );
    int days_to_next_month = max_day - int(day) + 1;
    month++;
    if (month == 13) {
        // We need to switch to next year.
        year++;
        month = 1;
    }
    day = 1;
    incDay(day_inc - days_to_next_month);
}
void PLearn::PDateTime::incHour ( int  hour_inc)

Increment the current date by a given number of hours.

Definition at line 142 of file PDateTime.cc.

{
    int new_hour = int(hour) + hour_inc;
    int day_inc = new_hour / 24;
    hour = (unsigned char) (new_hour % 24);
    incDay(day_inc);
}
void PLearn::PDateTime::incMinute ( int  min_inc)

Increment the current date by a given number of minutes.

Definition at line 131 of file PDateTime.cc.

References PLearn::min().

{
    int new_min = int(min) + min_inc;
    int hour_inc = new_min / 60;
    min = (unsigned char) (new_min % 60);
    incHour(hour_inc);
}

Here is the call graph for this function:

void PLearn::PDateTime::incSecond ( int  sec_inc)

Increment the current date by a given number of seconds.

Definition at line 120 of file PDateTime.cc.

{
    int new_sec = int(sec) + sec_inc;
    int min_inc = new_sec / 60;
    sec = (unsigned char) (new_sec % 60);
    incMinute(min_inc);
}
string PLearn::PDateTime::info ( ) const

Definition at line 185 of file PDateTime.cc.

References PLearn::min(), PLearn::right(), slash, and PLearn::tostring().

Referenced by PLearn::operator<<().

{
    return tostring(year)+ slash+
        right(tostring(int(month)), 2, '0') + slash+
        right(tostring(int(day)),   2, '0') + " "+
        right(tostring(int(hour)),  2, '0') + ":"+
        right(tostring(int(min)),   2, '0') + ":"+
        right(tostring(int(sec)),   2, '0');
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool PLearn::PDateTime::isMissing ( ) const

Missing date/time handling.

Definition at line 102 of file PDateTime.cc.

Referenced by PLearn::datetime_to_double().

{
    return year == SHRT_MIN && month == 0 && day == 0;
}

Here is the caller graph for this function:

bool PLearn::PDateTime::operator!= ( const PDateTime rhs) const [inline]

Definition at line 118 of file PDateTime.h.

                                                {
        return ! (*this == rhs);
    }
bool PLearn::PDateTime::operator< ( const PDateTime rhs) const [inline]

Definition at line 122 of file PDateTime.h.

References PLearn::datetime_to_double().

                                               {
        double datetime_to_double(const PDateTime& t); // declare the function
        return datetime_to_double(*this) < datetime_to_double(rhs);
    }

Here is the call graph for this function:

bool PLearn::PDateTime::operator<= ( const PDateTime rhs) const [inline]

Definition at line 127 of file PDateTime.h.

                                                {
        return *this == rhs || *this < rhs;
    }
bool PLearn::PDateTime::operator== ( const PDateTime rhs) const [inline]

Equality comparison.

Definition at line 113 of file PDateTime.h.

References day, hour, min, PLearn::min(), month, sec, and year.

                                                {
        return year == rhs.year && month == rhs.month && day == rhs.day
            && hour == rhs.hour && min == rhs.min && sec == rhs.sec;
    }

Here is the call graph for this function:

bool PLearn::PDateTime::operator> ( const PDateTime rhs) const [inline]

Definition at line 131 of file PDateTime.h.

                                               {
        return ! (*this <= rhs);
    }
bool PLearn::PDateTime::operator>= ( const PDateTime rhs) const [inline]

Definition at line 135 of file PDateTime.h.

                                                {
        return ! (*this < rhs);
    }
bool PLearn::PDateTime::sameDay ( const PDateTime rhs) const [inline]

Definition at line 139 of file PDateTime.h.

References day, month, and year.

                                             {
        return day   == rhs.day && month == rhs.month && year  == rhs.year;
    }
void PLearn::PDateTime::setMissing ( )

returns the date in the format yyyy/mm/dd hh:mm:ss

Definition at line 110 of file PDateTime.cc.

{
    year = SHRT_MIN;
    month = 0;
    day = 0;
}  
double PLearn::PDateTime::toJulianDay ( ) const

Convert a PDateTime into a Julian day. See http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html (if still existing...) for details.

Definition at line 210 of file PDateTime.cc.

References PLearn::hhmmss_to_double(), PLearn::min(), and PLERROR.

Referenced by PLearn::delta_seconds(), PLearn::JulianizeVMatrix::getNewRow(), and PLearn::operator-().

{
    if (year < 1582)
        PLERROR("toJulianDay works safely only for year > 1581 (%d)", year);

    double fraction = hhmmss_to_double(hour,min,sec);
  
    int jy = (month>2) ? year : year-1;
    int jm = (month>2) ? month : month+12;
    int ja = (int)(jy/100);
    int jb = (int)(ja/4);
    int jc = 2 - ja + jb;
    int je = (int)(365.25*(jy + 4716));
    int jf = (int)(30.6001*(jm + 1));

    return jc + day + je + jf - 1524 + fraction;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

unsigned char PLearn::PDateTime::day

1..31

Definition at line 67 of file PDateTime.h.

Referenced by PLearn::datetime_to_double(), PLearn::double_to_datetime(), operator==(), and sameDay().

unsigned char PLearn::PDateTime::hour

00..23

Definition at line 68 of file PDateTime.h.

Referenced by PLearn::datetime_to_double(), PLearn::double_to_datetime(), and operator==().

unsigned char PLearn::PDateTime::min

00..59

Definition at line 69 of file PDateTime.h.

Referenced by PLearn::datetime_to_double(), PLearn::double_to_datetime(), and operator==().

unsigned char PLearn::PDateTime::month

1..12

Definition at line 66 of file PDateTime.h.

Referenced by PLearn::datetime_to_double(), PLearn::double_to_datetime(), operator==(), and sameDay().

unsigned char PLearn::PDateTime::sec

00..61, allowing for leap seconds (!)

Definition at line 70 of file PDateTime.h.

Referenced by PLearn::datetime_to_double(), PLearn::double_to_datetime(), and operator==().

ex: 1983, 2001, -350, ...

Definition at line 65 of file PDateTime.h.

Referenced by PLearn::datetime_to_double(), PLearn::double_to_datetime(), operator==(), and sameDay().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines