pikotools/src/date/date.cpp

497 lines
7.6 KiB
C++

/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "date.h"
// for memset
#include <string.h>
namespace pt
{
Date::Date()
{
Clear();
}
Date::Date(const Date & d)
{
operator=(d);
}
Date::Date(time_t t)
{
FromTime(t);
}
Date::Date(const tm & t)
{
FromTm(t);
}
Date::Date(const char * str)
{
// parsing can be break in the middle of the string (if errors)
// and some values would not be initialized
Clear();
Parse(str);
}
Date::Date(const wchar_t * str)
{
Clear();
Parse(str);
}
Date::Date(const std::string & str)
{
Clear();
Parse(str);
}
Date::Date(const std::wstring & str)
{
Clear();
Parse(str);
}
Date & Date::operator=(const Date & d)
{
year = d.year;
month = d.month;
day = d.day;
hour = d.hour;
min = d.min;
sec = d.sec;
return *this;
}
Date & Date::operator=(time_t t)
{
FromTime(t);
return *this;
}
Date & Date::operator=(const tm & t)
{
FromTm(t);
return *this;
}
Date & Date::operator=(const char * str)
{
Parse(str);
return *this;
}
Date & Date::operator=(const wchar_t * str)
{
Parse(str);
return *this;
}
Date & Date::operator=(const std::string & str)
{
Parse(str);
return *this;
}
Date & Date::operator=(const std::wstring & str)
{
Parse(str);
return *this;
}
Date Date::operator+(time_t t) const
{
time_t t0 = ToTime();
Date d(t0 + t);
return d;
}
Date Date::operator-(time_t t) const
{
time_t t0 = ToTime();
Date d(t0 - t);
return d;
}
Date & Date::operator+=(time_t t)
{
time_t t0 = ToTime();
FromTime(t0 + t);
return *this;
}
Date & Date::operator-=(time_t t)
{
time_t t0 = ToTime();
FromTime(t0 - t);
return *this;
}
void Date::Swap(Date & date)
{
Date temp(*this);
*this = date;
date = temp;
}
time_t Date::operator-(const Date & d) const
{
time_t t0 = ToTime();
time_t t1 = d.ToTime();
time_t res;
if( t1 >= t0 )
res = t1 - t0;
else
res = t0 - t1;
return res;
}
bool Date::IsTheSameDay(const Date & d) const
{
return year == d.year && month == d.month && day == d.day;
}
bool Date::IsTheSameHour(const Date & d) const
{
return hour == d.hour && min == d.min && sec == d.sec;
}
bool Date::operator==(const Date & d) const
{
return IsTheSameDay(d) && IsTheSameHour(d);
}
bool Date::operator!=(const Date & d) const
{
return !operator==(d);
}
int Date::Compare(const Date & d) const
{
if( year != d.year )
return year - d.year;
if( month != d.month )
return month - d.month;
if( day != d.day )
return day - d.day;
if( hour != d.hour )
return hour - d.hour;
if( min != d.min )
return min - d.min;
if( sec != d.sec )
return sec - d.sec;
// dates are equal
return 0;
}
bool Date::operator>(const Date & d) const
{
return Compare(d) > 0;
}
bool Date::operator>=(const Date & d) const
{
return Compare(d) >= 0;
}
bool Date::operator<(const Date & d) const
{
return Compare(d) < 0;
}
bool Date::operator<=(const Date & d) const
{
return Compare(d) <= 0;
}
void Date::Clear()
{
year = 1970;
month = 1;
day = 1;
hour = 0;
min = 0;
sec = 0;
}
void Date::AssertRange(int & val, int val_min, int val_max)
{
if( val < val_min )
val = val_min;
if( val > val_max )
val = val_max;
}
void Date::AssertCorrectDate()
{
// 10000 is only a 'cosmetic' limit
// we can make calculations with greater values
AssertRange(year, 1970, 10000);
AssertRange(month, 1, 12);
AssertRange(day, 1, MonthLen(year, month));
AssertRange(hour, 0, 23);
AssertRange(min, 0, 59);
AssertRange(sec, 0, 59);
}
bool Date::IsCorrectDate()
{
// 10000 is only a 'cosmetic' limit
// we can make calculations with greater values
if( year < 1970 || year > 10000 )
return false;
if( month < 1 || month > 12 )
return false;
if( day < 1 || day > MonthLen(year, month) )
return false;
if( hour < 0 || hour > 23 )
return false;
if( min < 0 || min > 59 )
return false;
if( sec < 0 || sec > 59 )
return false;
return true;
}
int Date::MonthLen(int y, int m)
{
if( m == 2 && IsYearLeap(y) )
return 29;
const int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if( m>=1 && m<=12 )
return days[m-1];
return 0;
}
bool Date::IsYearLeap(int y)
{
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
bool Date::IsYearLeap() const
{
return IsYearLeap(year);
}
/*
return 'days' starts from 0000:03:01 (year 0, month 3 - March, day 1)
*/
long long Date::ToDays() const
{
long long m = (month + 9) % 12;
long long y = year - m/10;
return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (day - 1);
}
time_t Date::ToTime() const
{
time_t res = time_t((ToDays() - 719468) * 60*60*24);
res += hour * 60 * 60;
res += min * 60;
res += sec;
return res;
}
tm Date::ToTm() const
{
tm t;
memset(&t, 0, sizeof(tm));
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
t.tm_hour = hour;
t.tm_min = min;
t.tm_sec = sec;
t.tm_wday = WeekDay();
// t.tm_yday is not set
return t;
}
/*
this method calculates year, month and a day from given 'days'
'days' starts from 0000:03:01 (year 0, month 3 - March, day 1)
*/
void Date::FromDays(long long days)
{
//static_assert( sizeof(long long) >= 8 , "operation here should be used at least with 64 bits precision");
year = int(((long long)(10000)*days + 14780)/3652425);
int delta = int(days - (365*year + year/4 - year/100 + year/400));
if( delta < 0 )
{
year -= 1;
delta = int(days - (365*year + year/4 - year/100 + year/400));
}
int mi = (100*delta + 52)/3060;
month = (mi + 2)%12 + 1;
year = year + (mi + 2)/12;
day = delta - (mi*306 + 5)/10 + 1;
}
void Date::FromTime(time_t t)
{
time_t days = t / 60 / 60 / 24;
time_t diff = t - days * 60 * 60 * 24;
hour = int(diff / 60 / 60);
min = int((diff - hour * 60 * 60) / 60);
sec = int((diff - hour * 60 * 60 - min * 60));
FromDays((long long)(days) + 719468);
}
void Date::FromTm(const tm & t)
{
year = t.tm_year + 1900;
month = t.tm_mon + 1;
day = t.tm_mday;
hour = t.tm_hour;
min = t.tm_min;
sec = t.tm_sec;
}
int Date::WeekDay() const
{
long long d = ToDays();
return (int)((d+3) % 7);
}
} // namespace