|
PLearn 0.1
|
#include <PDate.h>

Public Member Functions | |
| PDate () | |
| Create a missing date by default. | |
| PDate (short the_year, unsigned char the_month, unsigned char the_day) | |
| PDate (int the_year, int the_month, int the_day) | |
| PDate (int julian_day) | |
| convert a Julian day into a PDate. | |
| PDate (string date, bool invalid_value_as_missing=false) | |
| Initialize a date from a string. | |
| bool | isMissing () const |
| Missing date handling. | |
| void | setMissing () |
| bool | isValid () const |
| Returns true if the date is valid; manages leap years. | |
| PDate | lastDateOfMonth () const |
| unsigned char | lastDayOfMonth () const |
| string | info () const |
| returns the date in the format //!< yyyy/mm/dd | |
| bool | operator== (const PDate &rhs) const |
| Equality comparison. | |
| bool | operator!= (const PDate &rhs) const |
| bool | operator< (const PDate &rhs) const |
| bool | operator<= (const PDate &rhs) const |
| bool | operator> (const PDate &rhs) const |
| bool | operator>= (const PDate &rhs) const |
| int | toJulianDay () const |
| int | dayOfWeek () const |
| int | dayOfYear () const |
| int | weekNumber () const |
| Returns week number in the year between 0 and 52 incl. (ISO 8601 minus 1) | |
Static Public Member Functions | |
| static PDate | lastDateOfMonth (int year, int month) |
| static unsigned char | lastDayOfMonth (int year, int month) |
| static PDate | today () |
Public Attributes | |
| short | year |
| ex: 1983, 2001, -350, ... | |
| unsigned char | month |
| 1..12 | |
| unsigned char | day |
| 1..31 | |
PDate must be a concrete class with no virtual function. Hence it MUST NOT UNDER ANY CONSIDERATION UNDER HEAVY PRISON PENALTY derive from Object.
| PLearn::PDate::PDate | ( | ) |
| PLearn::PDate::PDate | ( | short | the_year, |
| unsigned char | the_month, | ||
| unsigned char | the_day | ||
| ) | [inline] |
| PLearn::PDate::PDate | ( | int | julian_day | ) |
convert a Julian day into a PDate.
Day 0 of the Julian calendar is about 6000 years ago...
Definition at line 98 of file PDate.cc.
References PLASSERT.
{
int jw = (int)((julian_day - 1867216.25)/36524.25);
int jx = (int)(jw/4);
int ja = 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;
PLASSERT( isValid() );
}
| PLearn::PDate::PDate | ( | string | date, |
| bool | invalid_value_as_missing = false |
||
| ) |
Initialize a date from a string.
Currently recognized formats are: "2003/01/27", "27JAN2003" if invalid_value_as_missing is true and the string is an invalid date, we set the date as missing
Definition at line 115 of file PDate.cc.
References PLearn::pl_isnumber(), PLERROR, PLWARNING, PLearn::removeblanks(), PLearn::search_replace(), PLearn::toint(), and PLearn::tostring().
{
date = removeblanks(date);
search_replace(date, "-", ""); // remove dashes
// Format "2003/01/27"
if (date.size() == 10 && date[4] == '/' && date[7] == '/')
{
year = (short)toint(date.substr(0,4));
month = (unsigned char)toint(date.substr(5,2));
day = (unsigned char)toint(date.substr(8,2));
}
// Format "27JAN2003" or "27-jan-2003" or "27-jan-03"
else if((date.size()==9 || date.size()==7)
&& isalpha(date[2]) && isalpha(date[3]) && isalpha(date[4]))
{
// Convert to YYYY; assume cutoff point of 39 for years in 2000
if (date.size() == 7) {
int yy = toint(date.substr(5));
int yyyy = -1;
if (yy <= 40)
yyyy = 2000 + yy;
else
yyyy = 1900 + yy;
date = date.substr(0,5) + tostring(yyyy);
}
year = toint(date.substr(5,4));
day = toint(date.substr(0,2));
string mo = date.substr(2,3);
// Make uppercase
std::transform(mo.begin(), mo.end(), mo.begin(), (int(*)(int)) toupper);
if(mo=="JAN")
month = 1;
else if(mo=="FEB")
month = 2;
else if(mo=="MAR")
month = 3;
else if(mo=="APR")
month = 4;
else if(mo=="MAY")
month = 5;
else if(mo=="JUN")
month = 6;
else if(mo=="JUL")
month = 7;
else if(mo=="AUG")
month = 8;
else if(mo=="SEP")
month = 9;
else if(mo=="OCT")
month = 10;
else if(mo=="NOV")
month = 11;
else if(mo=="DEC")
month = 12;
else if(invalid_value_as_missing)
setMissing();
else
PLERROR("Invalid month string: '%s'",mo.c_str());
}
// Format "20020917"
else if (date.size() == 8 && pl_isnumber(date))
{
year = toint(date.substr(0,4));
month = toint(date.substr(4,2));
day = toint(date.substr(6,2));
}
// Format 1020917 (102 stands for 2002)
else if (date.size()==7 && pl_isnumber(date))
{
year = 1900+toint(date.substr(0,3));
month = toint(date.substr(3,2));
day = toint(date.substr(5,2));
}
// Format 980917 (99 stands for 1998)
else if (date.size()==6 && pl_isnumber(date))
{
year = 1900+toint(date.substr(0,2));
month = toint(date.substr(2,2));
day = toint(date.substr(4,2));
}
else if(invalid_value_as_missing)
setMissing();
else
PLERROR("PDate::PDate: the passed date string is not in a known format: %s", date.c_str());
if( !isValid() ){
if(invalid_value_as_missing){
setMissing();
PLWARNING("Invalid date string: %s",date.c_str());
}else
PLERROR("Invalid date string: %s",date.c_str());
}
}

| int PLearn::PDate::dayOfWeek | ( | ) | const [inline] |
Definition at line 138 of file PDate.h.
{ return toJulianDay()%7; }
| int PLearn::PDate::dayOfYear | ( | ) | const |
| string PLearn::PDate::info | ( | ) | const |
returns the date in the format //!< yyyy/mm/dd
Definition at line 243 of file PDate.cc.
References PLearn::right(), slash, and PLearn::tostring().
Referenced by PLearn::operator<<(), and PLearn::FieldValue::toString().
{
return tostring(year)+slash+
right(tostring(int(month)), 2, '0') +slash+
right(tostring(int(day)), 2, '0');
}


| bool PLearn::PDate::isMissing | ( | ) | const |
Missing date handling.
Definition at line 214 of file PDate.cc.
Referenced by PLearn::SDBVMFieldDateDiff::convertField(), PLearn::date_to_double(), and PLearn::date_to_float().

| bool PLearn::PDate::isValid | ( | ) | const |
Returns true if the date is valid; manages leap years.
Definition at line 226 of file PDate.cc.
Referenced by lastDateOfMonth().
{
// Managing leap years
if (month==2 && day==29)
{
bool valid = false;
if ( year % 4 == 0 ) valid = true;
if ( year % 100 == 0 ) valid = false;
if ( year % 400 == 0 ) valid = true;
return valid;
}
return year >= 1582 && year <= 3000 &&
month >= 1 && month <= 12 &&
day >= 1 && day <= lastDayOfMonth();
}

Definition at line 57 of file PDate.cc.
References isValid(), and PLASSERT.
Referenced by PLearn::Calendar::getLastDayOfMonth().
{
PDate date = PDate(year, month, int(lastDayOfMonth(year, month)));
PLASSERT( date.isValid() );
return date;
}


| PDate PLearn::PDate::lastDateOfMonth | ( | ) | const [inline] |
Definition at line 99 of file PDate.h.
{ return PDate::lastDateOfMonth(year, month); }
Definition at line 64 of file PDate.cc.
References PLERROR.
{
unsigned char day = 28;
switch ( month ) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day = 31;
break;
case 4: case 6: case 9: case 11:
day = 30;
break;
case 2:
day = 28;
if ( year % 4 == 0 ) day = 29;
if ( year % 100 == 0 ) day = 28;
if ( year % 400 == 0 ) day = 29;
break;
default:
PLERROR("%s: Invalid month argument %d", __FUNCTION__, month);
}
return day;
}
| unsigned char PLearn::PDate::lastDayOfMonth | ( | ) | const [inline] |
Definition at line 100 of file PDate.h.
{ return PDate::lastDayOfMonth(year, month); }
| void PLearn::PDate::setMissing | ( | ) |
| static PDate PLearn::PDate::today | ( | ) | [inline, static] |
Definition at line 147 of file PDate.h.
Referenced by PLearn::VMatLanguage::run().
{
time_t now(time(0));
tm* snow = localtime(&now);
return PDate(1900+snow->tm_year, 1+snow->tm_mon, snow->tm_mday);
}

| int PLearn::PDate::toJulianDay | ( | ) | const |
Convert a PDate into a Julian day. See http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html (if still existing...) for details.
Definition at line 250 of file PDate.cc.
References PLERROR.
Referenced by PLearn::SDBVMFieldDateDiff::convertField(), PLearn::Calendar::getLastDayOfMonth(), PLearn::JulianizeVMatrix::getNewRow(), PLearn::Calendar::makeCalendar(), PLearn::operator+(), PLearn::operator-(), PLearn::VMatLanguage::run(), and PLearn::TextFilesVMatrix::transformStringToValue().
{
if (year < 1582)
PLERROR("toJulianDay works safely only for year > 1581 (%d)", year);
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;
}

| int PLearn::PDate::weekNumber | ( | ) | const |
| unsigned char PLearn::PDate::day |
1..31
Definition at line 73 of file PDate.h.
Referenced by PLearn::SDBVMFieldDay::convertField(), PLearn::SDBVMFieldDate::convertField(), PLearn::date_to_double(), PLearn::date_to_float(), PLearn::double_to_date(), PLearn::FieldValue::FieldValue(), PLearn::float_to_date(), PLearn::FieldValue::isMissing(), operator<(), PLearn::operator<<(), operator==(), PLearn::operator>>(), PLearn::VMatLanguage::run(), PLearn::JulianDateCommand::run(), and PLearn::FieldValue::setMissing().
| unsigned char PLearn::PDate::month |
1..12
Definition at line 72 of file PDate.h.
Referenced by PLearn::SDBVMFieldICBCTargets::convertField(), PLearn::SDBVMFieldDateDiff::convertField(), PLearn::SDBVMFieldMonths::convertField(), PLearn::SDBVMFieldDay::convertField(), PLearn::SDBVMFieldDate::convertField(), PLearn::date_to_double(), PLearn::date_to_float(), PLearn::double_to_date(), PLearn::FieldValue::FieldValue(), PLearn::float_to_date(), PLearn::Calendar::getLastDayOfMonth(), PLearn::FieldValue::isMissing(), operator<(), PLearn::operator<<(), operator==(), PLearn::operator>>(), PLearn::VMatLanguage::run(), PLearn::JulianDateCommand::run(), and PLearn::FieldValue::setMissing().
| short PLearn::PDate::year |
ex: 1983, 2001, -350, ...
Definition at line 71 of file PDate.h.
Referenced by PLearn::SDBVMFieldICBCTargets::convertField(), PLearn::SDBVMFieldDateDiff::convertField(), PLearn::SDBVMFieldMonths::convertField(), PLearn::SDBVMFieldDay::convertField(), PLearn::SDBVMFieldDate::convertField(), PLearn::date_to_double(), PLearn::date_to_float(), PLearn::double_to_date(), PLearn::FieldValue::FieldValue(), PLearn::float_to_date(), PLearn::FieldValue::isMissing(), operator<(), PLearn::operator<<(), operator==(), PLearn::operator>>(), PLearn::VMatLanguage::run(), PLearn::JulianDateCommand::run(), and PLearn::FieldValue::setMissing().
1.7.4