winix/core/timezone.cpp

290 lines
4.3 KiB
C++

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2012, Tomasz Sowa
* All rights reserved.
*
*/
#include "timezone.h"
#include "misc.h"
TimeZone::Dst::Dst()
{
Clear();
}
void TimeZone::Dst::Clear()
{
has_dst = false;
start.Clear();
end.Clear();
offset = 0;
}
bool TimeZone::Dst::IsDstUsed(const PT::Date & date) const
{
if( !has_dst )
return false;
if( Compare(start, date) <= 0 ) // !! CHECK ME <= or < ? (what about the one second?)
if( Compare(date, end) < 0 )
return true;
return false;
}
int TimeZone::Dst::Compare(const PT::Date & date1, const PT::Date & date2) const
{
// year is ignored
if( date1.month != date2.month )
return date1.month - date2.month;
if( date1.day != date2.day )
return date1.day - date2.day;
if( date1.hour != date2.hour )
return date1.hour - date2.hour;
if( date1.min != date2.min )
return date1.min - date2.min;
if( date1.sec != date2.sec )
return date1.sec - date2.sec;
// dates are equal
return 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;
}
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.GetValue(L"offset_str");
if( offset_str )
return ParseStrOffset(offset_str->c_str());
return space.Long(L"offset");
}
bool TimeZone::SetTzDst(PT::Space & year)
{
time_t h24 = 60 * 60 * 24; // 24 hours
bool result = true;
Dst dst;
int year_int = Toi(year.name);
if( year_int < 1970 && year_int > 10000 )
return false;
dst.has_dst = year.Bool(L"has_dst", false);
if( dst.has_dst )
{
dst.start.year = year_int;
dst.end.year = year_int;
if( !dst.start.ParseMonthDayTime(year.Text(L"start")) )
result = false;
if( !dst.end.ParseMonthDayTime(year.Text(L"end")) )
result = false;
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 = space.name;
id = space.Int(L"id", -1);
offset = GetOffset(space);
time_t h24 = 60 * 60 * 24; // 24 hours
if( offset < -h24 || offset > h24 )
result = false;
PT::Space & dst = space.FindAddSpace(L"dst");
for(size_t i=0 ; i<dst.spaces.size() ; ++i)
{
PT::Space & year = *dst.spaces[i];
if( !SetTzDst(year) )
{
result = false;
break;
}
}
return result;
}