2009-12-15 21:18:56 +00:00
|
|
|
#include <Pascal/Date.h>
|
2009-12-10 01:41:37 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace Pascal;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-12 04:51:53 +00:00
|
|
|
Date::Date(unsigned val)
|
2009-12-10 01:41:37 +00:00
|
|
|
{
|
|
|
|
// yyyy yyym mmmm dddd
|
2009-12-11 04:03:21 +00:00
|
|
|
_month = val & 0xf;
|
|
|
|
_day = (val >> 4) & 0x1f;
|
2009-12-10 01:41:37 +00:00
|
|
|
_year = (val >> 9) & 0x7f;
|
|
|
|
}
|
|
|
|
|
2009-12-12 04:51:53 +00:00
|
|
|
Date::operator std::time_t() const {
|
2009-12-10 01:41:37 +00:00
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
if (_day == 0 || _month == 0) return (std::time_t)-1;
|
|
|
|
|
|
|
|
std::memset(&tm, 0, sizeof(tm));
|
|
|
|
tm.tm_hour = 12;
|
|
|
|
tm.tm_mday = _day;
|
|
|
|
tm.tm_mon = _month;
|
|
|
|
tm.tm_year = _year;
|
|
|
|
tm.tm_isdst = -1;
|
|
|
|
|
2009-12-15 21:18:56 +00:00
|
|
|
// ProDOS standard for dealing w/ y2k.
|
|
|
|
if (_year <= 39) tm.tm_year += 100;
|
|
|
|
|
2009-12-10 01:41:37 +00:00
|
|
|
return std::mktime(&tm);
|
2009-12-12 04:51:53 +00:00
|
|
|
}
|
|
|
|
|
2009-12-12 15:38:12 +00:00
|
|
|
Date::operator uint16_t() const {
|
2009-12-12 04:51:53 +00:00
|
|
|
// year must be 0 .. 127
|
|
|
|
return (_year << 9) | (_day << 4) | _month;
|
|
|
|
}
|
|
|
|
|
|
|
|
Date Date::Today()
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
std::time_t t = std::time(NULL);
|
|
|
|
|
|
|
|
::localtime_r(&t, &tm);
|
|
|
|
|
2009-12-15 21:18:56 +00:00
|
|
|
return Date(tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday);
|
2009-12-10 01:41:37 +00:00
|
|
|
}
|