winix/core/timezones.cpp

164 lines
2.6 KiB
C++

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2012, Tomasz Sowa
* All rights reserved.
*
*/
#include "timezones.h"
#include "misc.h"
#include "log.h"
TimeZones::TimeZones()
{
Clear();
}
time_t TimeZones::ParseOffset(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;
if( is_sign )
offset = -offset;
}
return offset;
}
void TimeZones::ParseZones()
{
for(size_t i=0 ; i<temp_space.spaces.size() ; ++i)
{
PT::Space & zone = *temp_space.spaces[i];
zone.Add(L"tz_offset", ParseOffset(zone.Text(L"tz_offset_str").c_str()));
zone.Add(L"tz_dst_offset", ParseOffset(zone.Text(L"tz_dst_offset_str").c_str()));
temp_zone.Clear();
if( temp_zone.time_zone.SetTz(zone) )
{
temp_zone.name_key = zone.name;
if( !FindZone(temp_zone.time_zone.tz_id) )
{
tab.push_back(temp_zone);
}
else
{
log << log1 << "Tz: zone with id: " << temp_zone.time_zone.tz_id
<< " already exists (skipping)" << logend;
}
}
else
{
log << log1 << "System: problem with reading info from time zone: "
<< zone.name << " (skipping) " << logend;
}
}
}
// !! IMPROVE ME
// in the future we do not have to read the whole file
// just space by space (not implemented in Space at the moment)
bool TimeZones::ReadTimeZones(const wchar_t * path)
{
parser.UTF8(true);
parser.SetSpace(temp_space);
tab.clear();
PT::SpaceParser::Status status = parser.Parse(path);
if( status == PT::SpaceParser::ok )
{
ParseZones();
log << log2 << "Tz: time zones loaded, there are " << tab.size() << " zones" << logend;
}
else
if( status == PT::SpaceParser::syntax_error )
{
log << log1 << "TZ: error in time zone file, line: " << parser.line << logend;
}
else
if( status == PT::SpaceParser::cant_open_file )
{
log << log1 << "TZ: I cannot open the time zone file: " << path << logend;
}
temp_space.Clear();
return status == PT::SpaceParser::ok;
}
bool TimeZones::ReadTimeZones(const std::wstring & path)
{
return ReadTimeZones(path.c_str());
}
TimeZones::Zone * TimeZones::FindZone(int tz_id)
{
for(size_t i=0 ; i<tab.size() ; ++i)
{
if( tab[i].time_zone.tz_id == tz_id )
return &tab[i];
}
return 0;
}
TimeZones::Zone & TimeZones::operator[](size_t index)
{
return tab[index];
}
size_t TimeZones::Size() const
{
return tab.size();
}
bool TimeZones::Empty() const
{
return tab.empty();
}
void TimeZones::Clear()
{
tab.clear();
}