]> git.tdb.fi Git - libs/core.git/blobdiff - source/time/datetime.cpp
Drop copyright and license notices from source files
[libs/core.git] / source / time / datetime.cpp
index 4c109758eb0e95d8b191d50152866856bbe48b46..8feeff2f0d21a798f153607ea3f3d02e93b36fd2 100644 (file)
@@ -1,18 +1,18 @@
-/* $Id$ */
-#include <sstream>
-#include <iomanip>
-#include "../core/except.h"
+#include <cstdlib>
+#include <stdexcept>
+#include <msp/strings/format.h>
 #include "datetime.h"
 #include "timestamp.h"
+#include "units.h"
 
 using namespace std;
 
 namespace {
 
-inline bool is_leap_year(int32_t y)
+inline bool is_leap_year(int y)
 { return y%4==0 && (y%100 || y%400==0); }
 
-inline uint8_t month_days(int32_t y, uint8_t m)
+inline unsigned char month_days(int y, unsigned char m)
 {
        switch(m)
        {
@@ -43,118 +43,144 @@ inline int cmp_(T a, T b)
 namespace Msp {
 namespace Time {
 
-DateTime::DateTime(const TimeStamp &ts):
-       year(1970),
-       month(1),
-       mday(1),
-       hour(0),
-       minute(0),
-       second(0),
-       usec(0)
+DateTime::DateTime(const TimeStamp &ts)
 {
-       add_raw(ts.raw());
+       init(ts);
+}
+
+DateTime::DateTime(const TimeStamp &ts, const TimeZone &tz)
+{
+       init(ts);
+       convert_timezone(tz);
+}
+
+DateTime::DateTime(int y, unsigned char m, unsigned char d)
+{
+       init(y, m, d, 0, 0, 0, 0);
+}
+
+DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s)
+{
+       init(y, m, d, h, n, s, 0);
 }
 
-DateTime::DateTime(int32_t y, uint8_t m, uint8_t d):
-       year(y),
-       month(m),
-       mday(d),
-       hour(0),
-       minute(0),
-       second(0),
-       usec(0)
+DateTime::DateTime(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
 {
-       validate();
+       init(y, m, d, h, n, s, u);
 }
 
-DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s):
-       year(y),
-       month(m),
-       mday(d),
-       hour(h),
-       minute(n),
-       second(s),
-       usec(0)
+void DateTime::init(const TimeStamp &ts)
 {
-       validate();
+       year = 1970;
+       month = 1;
+       mday = 1;
+       hour = 0;
+       minute = 0;
+       second = 0;
+       usec = 0;
+       add_raw(ts.raw());
 }
 
-DateTime::DateTime(int32_t y, uint8_t m, uint8_t d, uint8_t h, uint8_t n, uint8_t s, uint32_t u):
-       year(y),
-       month(m),
-       mday(d),
-       hour(h),
-       minute(n),
-       second(s),
-       usec(u)
+void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, unsigned char n, unsigned char s, unsigned u)
 {
-       validate();
+       year = y;
+       month = m;
+       mday = d;
+       hour = h;
+       minute = n;
+       second = s;
+       usec = u;
+
+       if(usec>=1000000)
+               throw out_of_range("DateTime::DateTime usec");
+       if(second>=60)
+               throw out_of_range("DateTime::DateTime second");
+       if(minute>=60)
+               throw out_of_range("DateTime::DateTime minute");
+       if(hour>=24)
+               throw out_of_range("DateTime::DateTime hour");
+       if(month<1 || month>12)
+               throw out_of_range("DateTime::DateTime month");
+       if(mday<1 || mday>month_days(year, month))
+               throw out_of_range("DateTime::DateTime mday");
 }
 
-void DateTime::add_days(int32_t days)
+void DateTime::add_days(int days)
 {
-       unsigned new_year=year;
+       int new_year = year;
 
        /* Leap years have a 400 year cycle, so any 400 consecutive years have a
-       constant number of days (400*365+97=146097) */
-       new_year+=days/146097*400;
-       days%=146097;
+       constant number of days (400*365+97 = 146097) */
+       new_year += days/146097*400;
+       days %= 146097;
 
        if(days<0)
        {
-               new_year-=400;
-               days+=146097;
+               new_year -= 400;
+               days += 146097;
        }
 
        // Fudge factor for leap day
-       int fudge=(month<=2)?1:0;
+       int fudge = (month<=2)?1:0;
 
        // (Almost) every 4 year cycle has 1 leap year and 3 normal years
-       unsigned cycles=days/1461;
-       days%=1461;
+       unsigned cycles = days/1461;
+       days %= 1461;
 
-       new_year+=cycles*4;
+       new_year += cycles*4;
 
        // See how many non-leap-years we counted as leap years and reclaim the lost days
-       unsigned missed_leap_days=((year-fudge)%100+cycles*4)/100;
+       // XXX This breaks with negative years
+       unsigned missed_leap_days = ((year-fudge)%100+cycles*4)/100;
        if((year-fudge)%400+cycles*4>=400)
                --missed_leap_days;
 
-       days+=missed_leap_days;
+       days += missed_leap_days;
 
        // Count single years from the 4 year cycle
-       cycles=days/365;
-       days%=365;
+       cycles = days/365;
+       days %= 365;
 
-       new_year+=cycles;
+       new_year += cycles;
 
-       if((year-fudge)%4+cycles>=4)
+       if((year-fudge)%4+cycles>=4 && (new_year%100>=4 || new_year%400<4))
        {
                // We passed a leap year - decrement days
                if(days==0)
                {
+                       days = is_leap_year(new_year-fudge)?365:364;
                        --new_year;
-                       days=is_leap_year(new_year)?365:364;
                }
                else
                        --days;
        }
 
-       year=new_year;
+       year = new_year;
 
        // Step months
        while(mday+days>month_days(year, month))
        {
-               days-=month_days(year, month);
+               days -= month_days(year, month);
                ++month;
                if(month>12)
                {
                        ++year;
-                       month=1;
+                       month = 1;
                }
        }
 
-       mday+=days;
+       mday += days;
+}
+
+void DateTime::set_timezone(const TimeZone &tz)
+{
+       zone = tz;
+}
+
+void DateTime::convert_timezone(const TimeZone &tz)
+{
+       add_raw((tz.get_offset()-zone.get_offset()).raw());
+       zone = tz;
 }
 
 DateTime DateTime::operator+(const TimeDelta &td) const
@@ -170,21 +196,34 @@ DateTime &DateTime::operator+=(const TimeDelta &td)
        return *this;
 }
 
+DateTime DateTime::operator-(const TimeDelta &td) const
+{
+       DateTime dt(*this);
+       dt.add_raw(-td.raw());
+       return dt;
+}
+
+DateTime &DateTime::operator-=(const TimeDelta &td)
+{
+       add_raw(-td.raw());
+       return *this;
+}
+
 int DateTime::cmp(const DateTime &dt) const
 {
-       if(int c=cmp_(year, dt.year))
+       if(int c = cmp_(year, dt.year))
                return c;
-       if(int c=cmp_(month, dt.month))
+       if(int c = cmp_(month, dt.month))
                return c;
-       if(int c=cmp_(mday, dt.mday))
+       if(int c = cmp_(mday, dt.mday))
                return c;
-       if(int c=cmp_(hour, dt.hour))
+       if(int c = cmp_(hour, dt.hour))
                return c;
-       if(int c=cmp_(minute, dt.minute))
+       if(int c = cmp_(minute, dt.minute))
                return c;
-       if(int c=cmp_(second, dt.second))
+       if(int c = cmp_(second, dt.second))
                return c;
-       if(int c=cmp_(usec, dt.usec))
+       if(int c = cmp_(usec, dt.usec))
                return c;
        return 0;
 }
@@ -192,24 +231,23 @@ int DateTime::cmp(const DateTime &dt) const
 TimeStamp DateTime::get_timestamp() const
 {
        if(year<-289701 || year>293641)
-               throw Exception("DateTime is not representable as a TimeStamp");
+               throw range_error("DateTime::get_timestamp");
 
-       int64_t raw=(((hour*60LL)+minute)*60+second)*1000000+usec;
-       int days=(year-1970)*365;
-       days+=(year-1)/4-(year-1)/100+(year-1)/400-477;
+       RawTime raw = (((hour*60LL)+minute)*60+second)*1000000+usec;
+       int days = (year-1970)*365;
+       days += (year-1)/4-(year-1)/100+(year-1)/400-477;
        for(unsigned i=1; i<month; ++i)
-               days+=month_days(year, i);
-       days+=mday-1;
+               days += month_days(year, i);
+       days += mday-1;
 
-       raw+=days*86400000000LL;
+       raw += days*86400000000LL;
 
        return TimeStamp(raw);
 }
 
 string DateTime::format(const string &fmt) const
 {
-       ostringstream ss;
-       ss.fill('0');
+       string result;
        for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
        {
                if(*i=='%')
@@ -218,47 +256,60 @@ string DateTime::format(const string &fmt) const
                        if(i==fmt.end())
                                break;
                        else if(*i=='d')
-                               ss<<setw(2)<<int(mday);
+                               result += Msp::format("%02d", mday);
                        else if(*i=='H')
-                               ss<<setw(2)<<int(hour);
+                               result += Msp::format("%02d", hour);
                        else if(*i=='I')
-                               ss<<setw(2)<<hour%12;
+                               result += Msp::format("%02d", hour%12);
                        else if(*i=='m')
-                               ss<<setw(2)<<int(month);
+                               result += Msp::format("%02d", month);
                        else if(*i=='M')
-                               ss<<setw(2)<<int(minute);
+                               result += Msp::format("%02d", minute);
                        else if(*i=='p')
-                               ss<<((hour>=12) ? "PM" : "AM");
+                               result += ((hour>=12) ? "PM" : "AM");
                        else if(*i=='S')
-                               ss<<setw(2)<<int(second);
+                               result += Msp::format("%02d", second);
                        else if(*i=='y')
-                               ss<<setw(2)<<year%100;
+                               result += Msp::format("%02d", year%100);
                        else if(*i=='Y')
-                               ss<<year;
+                               result += Msp::format("%04d", year);
                        else if(*i=='%')
-                               ss<<'%';
+                               result += '%';
                }
                else
-                       ss<<*i;
+                       result += *i;
        }
 
-       return ss.str();
+       return result;
 }
 
-void DateTime::add_raw(int64_t raw)
+string DateTime::format_rfc3339() const
 {
-       int32_t days=raw/86400000000LL;
-       raw%=86400000000LL;
+       string result = format("%Y-%m-%dT%H:%M:%S");
+       if(const TimeDelta &offs = zone.get_offset())
+       {
+               int m = abs(static_cast<int>(offs/Time::min));
+               result += Msp::format("%c%02d:%02d", (offs<zero ? '-' : '+'), m/60, m%60);
+       }
+       else
+               result += 'Z';
+       return result;
+}
+
+void DateTime::add_raw(RawTime raw)
+{
+       int days = static_cast<int>(raw/86400000000LL);
+       raw %= 86400000000LL;
        if(raw<0)
        {
-               ++days;
-               raw+=86400000000LL;
+               --days;
+               raw += 86400000000LL;
        }
 
-       usec+=raw%1000000; raw/=1000000;
-       second+=raw%60;    raw/=60;
-       minute+=raw%60;    raw/=60;
-       hour+=raw%24;      raw/=24;
+       usec+=raw%1000000; raw /= 1000000;
+       second+=raw%60;    raw /= 60;
+       minute+=raw%60;    raw /= 60;
+       hour+=raw%24;      raw /= 24;
 
        add_days(days);
        normalize();
@@ -266,41 +317,25 @@ void DateTime::add_raw(int64_t raw)
 
 void DateTime::normalize()
 {
-       second+=usec/1000000;
-       usec%=1000000;
-       minute+=second/60;
-       second%=60;
-       hour+=minute/60;
-       minute%=60;
-       mday+=hour/24;
-       hour%=24;
+       second += usec/1000000;
+       usec %= 1000000;
+       minute += second/60;
+       second %= 60;
+       hour += minute/60;
+       minute %= 60;
+       mday += hour/24;
+       hour %= 24;
        while(mday>month_days(year, month))
        {
-               mday-=month_days(year, month);
+               mday -= month_days(year, month);
                ++month;
                if(month>12)
                {
                        ++year;
-                       month=1;
+                       month = 1;
                }
        }
 }
 
-void DateTime::validate() const
-{
-       if(usec>=1000000)
-               throw InvalidParameterValue("Microseconds out of range");
-       if(second>=60)
-               throw InvalidParameterValue("Seconds out of range");
-       if(minute>=60)
-               throw InvalidParameterValue("Minutes out of range");
-       if(hour>=24)
-               throw InvalidParameterValue("Hours out of range");
-       if(month<1 || month>12)
-               throw InvalidParameterValue("Month out of range");
-       if(mday<1 || mday>month_days(year, month))
-               throw InvalidParameterValue("Day of month out of range");
-}
-
 } // namespace Time
 } // namespace Msp