/* * This file is a part of Winix * and is not publicly distributed * * Copyright (c) 2012, Tomasz Sowa * All rights reserved. * */ #include "timezone.h" TimeZone::TimeZone() { Clear(); } void TimeZone::Clear() { tz_id = -1; tz_offset = 0; tz_has_dst = false; tz_dst_offset = 0; tz_dst_start.Clear(); tz_dst_end.Clear(); } time_t TimeZone::CalcLocalOffset(const PT::Date & utc_date) { time_t offset; offset = tz_offset; // !! CHECK ME need to be tested if( tz_has_dst ) { PT::Date local(utc_date); local += tz_offset; local.year = tz_dst_start.year; tz_dst_end.year = tz_dst_start.year; if( tz_dst_start <= local && local < tz_dst_end ) offset += tz_dst_offset; } return 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 offset; offset = tz_offset; if( tz_has_dst ) { // !! CHECK ME need to be tested PT::Date local(local_date); local -= tz_dst_offset; local.year = tz_dst_start.year; tz_dst_end.year = tz_dst_start.year; if( tz_dst_start <= local && local < tz_dst_end ) offset += tz_dst_offset; } return 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; } bool TimeZone::SetTz(PT::Space & space) { bool result = true; tz_id = space.Int(L"tz_id", -1); tz_offset = space.Long(L"tz_offset", 0); tz_has_dst = space.Bool(L"tz_has_dst", false); time_t h13 = 60 * 60 * 13; // 13 hours time_t h15 = 60 * 60 * 15; // 15 hours time_t h24 = 60 * 60 * 24; // 24 hours if( tz_offset < -h13 || tz_offset > h15 ) result = false; if( tz_has_dst ) { tz_dst_start.year = 1970; tz_dst_end.year = 1970; if( !tz_dst_start.ParseMonthDayTime(space.Text(L"tz_dst_start")) ) result = false; if( !tz_dst_end.ParseMonthDayTime(space.Text(L"tz_dst_end")) ) result = false; tz_dst_offset = space.Long(L"tz_dst_offset"); if( tz_dst_offset < -h24 || tz_dst_offset > h24 ) result = false; } else { tz_dst_start.Clear(); tz_dst_end.Clear(); tz_dst_offset = 0; } if( !result ) Clear(); return result; }