added: a new directory "etc"
there'll be some generic config files for winix added: a new file in etc directory: time_zones_file list of time zones (not finished yet -- daylight saving time is needed) added: option to config: etc_dir a directory in which there are some config files used mainly when winix starts default: empty (means not for using) added: option to config: time_zones_file a file in etc_dir with time zones info default: time_zones.conf this is a Space structure with all time zones added: to system: TimeZones struct list of time zones read from etc/time_zones.conf git-svn-id: svn://ttmath.org/publicrep/winix/trunk@849 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
163
core/timezones.cpp
Normal file
163
core/timezones.cpp
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user