464 lines
8.4 KiB
C++
464 lines
8.4 KiB
C++
/*
|
|
* This file is a part of Winix
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2012-2023, 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:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. 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.
|
|
*
|
|
* 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 HOLDER 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 "timezone.h"
|
|
#include "misc.h"
|
|
|
|
|
|
namespace Winix
|
|
{
|
|
|
|
|
|
|
|
|
|
TimeZone::Dst::Dst()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
|
|
void TimeZone::Dst::Clear()
|
|
{
|
|
has_dst = false;
|
|
start.Clear();
|
|
end.Clear();
|
|
offset = 0;
|
|
}
|
|
|
|
|
|
bool TimeZone::Dst::IsDstUsed(const pt::Date & utc_date) const
|
|
{
|
|
return has_dst && start.Compare(utc_date, true) <= 0 && utc_date.Compare(end) < 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
TimeZone::TimeZone()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
|
|
|
|
|
|
void TimeZone::Clear()
|
|
{
|
|
name.clear();
|
|
id = 0;
|
|
offset = 0;
|
|
dst_map.clear();
|
|
}
|
|
|
|
|
|
|
|
TimeZone::Dst * TimeZone::FindDst(int year)
|
|
{
|
|
if( dst_map.empty() )
|
|
return 0;
|
|
|
|
DstMap::iterator i = dst_map.lower_bound(year);
|
|
|
|
if( i == dst_map.begin() && i->first > year )
|
|
return 0;
|
|
|
|
if( i == dst_map.end() )
|
|
return &(--i)->second;
|
|
|
|
if( i != dst_map.begin() && i->first > year )
|
|
return &(--i)->second;
|
|
|
|
return &i->second;
|
|
}
|
|
|
|
|
|
|
|
time_t TimeZone::CalcLocalOffset(const pt::Date & utc_date)
|
|
{
|
|
time_t dst_offset = 0;
|
|
|
|
Dst * dst = FindDst(utc_date.year);
|
|
|
|
if( dst && dst->IsDstUsed(utc_date) )
|
|
dst_offset = dst->offset;
|
|
|
|
return offset + dst_offset;
|
|
}
|
|
|
|
|
|
|
|
time_t TimeZone::ToLocal(time_t utc_time)
|
|
{
|
|
time_t offset = CalcLocalOffset(pt::Date(utc_time));
|
|
|
|
return utc_time + offset;
|
|
}
|
|
|
|
|
|
pt::Date TimeZone::ToLocal(const pt::Date & utc_date)
|
|
{
|
|
pt::Date local(utc_date);
|
|
local += CalcLocalOffset(utc_date);
|
|
|
|
return local;
|
|
}
|
|
|
|
|
|
void TimeZone::PrintOffsetPart(long val, pt::Stream & str)
|
|
{
|
|
if( val < 10 )
|
|
{
|
|
str << '0';
|
|
}
|
|
|
|
str << val;
|
|
}
|
|
|
|
|
|
void TimeZone::PrintOffset(time_t offset, pt::Stream & str)
|
|
{
|
|
bool has_sign = false;
|
|
|
|
if( offset < 0 )
|
|
{
|
|
has_sign = true;
|
|
offset = 0 - offset;
|
|
}
|
|
|
|
time_t days = offset / 60 / 60 / 24;
|
|
|
|
if( days == 0 )
|
|
{
|
|
str << ((has_sign) ? '-' : '+');
|
|
time_t diff = offset - days * 60 * 60 * 24;
|
|
|
|
long hour = int(diff / 60 / 60);
|
|
long min = int((diff - hour * 60 * 60) / 60);
|
|
|
|
PrintOffsetPart(hour, str);
|
|
str << ':';
|
|
PrintOffsetPart(min, str);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
time_t TimeZone::CalcUTCOffset(const pt::Date & local_date)
|
|
{
|
|
time_t dst_offset = 0;
|
|
|
|
Dst * dst = FindDst(local_date.year);
|
|
|
|
if( dst && dst->has_dst )
|
|
{
|
|
// dst date ranges we have in UTC
|
|
pt::Date utc(local_date);
|
|
utc -= (offset + dst->offset);
|
|
|
|
if( dst->IsDstUsed(utc) )
|
|
dst_offset = dst->offset;
|
|
}
|
|
|
|
return offset + dst_offset;
|
|
}
|
|
|
|
|
|
|
|
time_t TimeZone::ToUTC(time_t local_time)
|
|
{
|
|
time_t offset = CalcUTCOffset(pt::Date(local_time));
|
|
|
|
return local_time - offset;
|
|
}
|
|
|
|
|
|
|
|
pt::Date TimeZone::ToUTC(const pt::Date & local_date)
|
|
{
|
|
time_t offset;
|
|
pt::Date utc(local_date);
|
|
|
|
offset = CalcUTCOffset(local_date);
|
|
utc -= offset;
|
|
|
|
return utc;
|
|
}
|
|
|
|
|
|
|
|
time_t TimeZone::ParseStrOffset(const wchar_t * str)
|
|
{
|
|
pt::Date date;
|
|
bool is_sign = false;
|
|
time_t offset = 0;
|
|
|
|
str = SkipWhite(str);
|
|
|
|
if( *str == '-' )
|
|
{
|
|
is_sign = true;
|
|
str += 1;
|
|
}
|
|
else
|
|
if( *str == '+' )
|
|
{
|
|
str += 1;
|
|
}
|
|
|
|
if( date.ParseTime(str) )
|
|
{
|
|
offset = date.hour * 60 * 60 + date.min * 60 + date.sec;
|
|
|
|
if( is_sign )
|
|
offset = -offset;
|
|
}
|
|
|
|
return offset;
|
|
}
|
|
|
|
|
|
time_t TimeZone::GetOffset(pt::Space & space)
|
|
{
|
|
std::wstring * offset_str = space.get_wstr(L"offset_str");
|
|
|
|
if( offset_str )
|
|
return ParseStrOffset(offset_str->c_str());
|
|
|
|
return space.to_long(L"offset");
|
|
}
|
|
|
|
|
|
const wchar_t * TimeZone::SkipDstDateSeparator(const wchar_t * date_str)
|
|
{
|
|
date_str = pt::skip_white(date_str, false, false);
|
|
|
|
// the same separators as in the pikotools library
|
|
if( *date_str == '-' || *date_str == '/' || *date_str == '.' )
|
|
{
|
|
++date_str;
|
|
}
|
|
|
|
return pt::skip_white(date_str, false, false);
|
|
}
|
|
|
|
|
|
|
|
int TimeZone::ParseDstDateWeekday(const wchar_t * date_str, const wchar_t ** after_date_str)
|
|
{
|
|
date_str = SkipDstDateSeparator(date_str);
|
|
int weekday_int = -1;
|
|
|
|
struct WeekDay {
|
|
const wchar_t * name;
|
|
int weekday;
|
|
};
|
|
|
|
static const WeekDay week_days[] = {
|
|
{L"sunday", 0},
|
|
{L"monday", 1},
|
|
{L"tuesday", 2},
|
|
{L"wednesday", 3},
|
|
{L"thursday", 4},
|
|
{L"friday", 5},
|
|
{L"saturday", 6},
|
|
};
|
|
|
|
for(const WeekDay & week_day : week_days)
|
|
{
|
|
if( pt::is_substr(week_day.name, date_str) )
|
|
{
|
|
date_str += wcslen(week_day.name);
|
|
weekday_int = week_day.weekday;
|
|
break;
|
|
}
|
|
}
|
|
|
|
*after_date_str = date_str;
|
|
return weekday_int;
|
|
}
|
|
|
|
|
|
void TimeZone::ParseDstDateDay(bool is_first, int weekday, pt::Date & date)
|
|
{
|
|
int len = 1;
|
|
int adder = 1;
|
|
|
|
if( is_first )
|
|
{
|
|
len = pt::Date::MonthLen(date.year, date.month);
|
|
date.day = 1;
|
|
adder = 1;
|
|
}
|
|
else
|
|
{
|
|
len = pt::Date::MonthLen(date.year, date.month);
|
|
date.day = len;
|
|
adder = -1;
|
|
}
|
|
|
|
for(int i=0 ; i < len && date.WeekDay() != weekday ; ++i)
|
|
{
|
|
date.day += adder;
|
|
}
|
|
}
|
|
|
|
|
|
bool TimeZone::ParseDstDate(int year, const wchar_t * date_str, pt::Date & date)
|
|
{
|
|
bool was_overflow = false;
|
|
bool is_ok = false;
|
|
const wchar_t * after_date_str;
|
|
|
|
date.Clear();
|
|
date.year = year;
|
|
|
|
date_str = pt::skip_white(date_str, false, false);
|
|
date.month = pt::to_i(date_str, 10, &after_date_str, &was_overflow, true);
|
|
|
|
if( !was_overflow && after_date_str != date_str )
|
|
{
|
|
date_str = SkipDstDateSeparator(after_date_str);
|
|
|
|
if( pt::is_substr(L"first", date_str) )
|
|
{
|
|
date_str += 5; // length of "first"
|
|
int weekday = ParseDstDateWeekday(date_str, &date_str);
|
|
|
|
if( weekday >= 0 )
|
|
{
|
|
ParseDstDateDay(true, weekday, date);
|
|
is_ok = true;
|
|
}
|
|
}
|
|
else
|
|
if( pt::is_substr(L"last", date_str) )
|
|
{
|
|
date_str += 4; // length of "last"
|
|
int weekday = ParseDstDateWeekday(date_str, &date_str);
|
|
|
|
if( weekday >= 0 )
|
|
{
|
|
ParseDstDateDay(false, weekday, date);
|
|
is_ok = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
date.day = pt::to_i(date_str, 10, &after_date_str, &was_overflow, true);
|
|
is_ok = !was_overflow && after_date_str != date_str;
|
|
date_str = after_date_str;
|
|
}
|
|
}
|
|
|
|
return is_ok && date.ParseTime(date_str) && date.IsCorrectDate();
|
|
}
|
|
|
|
|
|
bool TimeZone::SetTzDst(pt::Space & year)
|
|
{
|
|
time_t h24 = pt::Date::ONE_DAY;
|
|
bool result = true;
|
|
Dst dst;
|
|
|
|
int year_int = 0;
|
|
std::wstring * year_name = year.get_wstr(L"year");
|
|
|
|
if( year_name )
|
|
year_int = Toi(*year_name);
|
|
|
|
if( year_int < 1970 || year_int > 10000 )
|
|
return false;
|
|
|
|
bool has_dst = year.to_bool(L"has_dst", false);
|
|
std::wstring * dst_start = year.get_wstr(L"start");
|
|
std::wstring * dst_end = year.get_wstr(L"end");
|
|
|
|
if( has_dst && dst_start && dst_end )
|
|
{
|
|
dst.has_dst = true;
|
|
result = result && ParseDstDate(year_int, dst_start->c_str(), dst.start);
|
|
result = result && ParseDstDate(year_int, dst_end->c_str(), dst.end);
|
|
dst.offset = GetOffset(year);
|
|
|
|
if( dst.offset < -h24 || dst.offset > h24 )
|
|
result = false;
|
|
}
|
|
|
|
if( result )
|
|
dst_map[year_int] = dst;
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
bool TimeZone::SetTz(pt::Space & space)
|
|
{
|
|
bool result = true;
|
|
name.clear();
|
|
|
|
std::wstring * space_name = space.get_wstr(L"name");
|
|
|
|
if( space_name )
|
|
name = *space_name;
|
|
|
|
id = space.to_int(L"id", -1);
|
|
offset = GetOffset(space);
|
|
time_t h24 = pt::Date::ONE_DAY;
|
|
|
|
if( offset < -h24 || offset > h24 )
|
|
result = false;
|
|
|
|
pt::Space::TableType * dst = space.get_table(L"dst");
|
|
|
|
if( dst )
|
|
{
|
|
for(pt::Space * dst_space : *dst)
|
|
{
|
|
if( !SetTzDst(*dst_space) )
|
|
{
|
|
result = false;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|