winix/core/timezone.h

146 lines
2.9 KiB
C++

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2012-2014, Tomasz Sowa
* All rights reserved.
*
*/
#ifndef headerfile_winix_core_timezone
#define headerfile_winix_core_timezone
#include <ctime>
#include <map>
#include "date/date.h"
#include "space/space.h"
namespace Winix
{
class TimeZone
{
public:
struct Dst
{
// true if a time zone has daylight saving time
bool has_dst;
// time zone daylight saving time (used if has_dst is true)
// the 'year' field is the same in 'start' and 'end'
// start and end are represented in UTC time
PT::Date start, end;
// time zone daylight saving time offset
// used when has_dst is true and the date is whithin start and end
// this offset should be added to time zone offset
time_t offset;
Dst();
void Clear();
// checking whether specified 'date' is in the range of <start, end>
// the year field in date, start and end is ignored
// has_dst must be true
bool IsDstUsed(const PT::Date & date) const;
private:
// Compare returns zero if date1 and date2 are equal
// return value less than zero if date1 is lower than date2
// and a value greater than zero if date1 is greater than date2
// the year field is ignored
int Compare(const PT::Date & date1, const PT::Date & date2) const;
};
TimeZone();
/*
*/
void Clear();
/*
reading zime zone values from Space struct (tz_id is skipped)
the space struct should have:
"tz_offset" (long)
"tz_has_dst" (bool)
if tz_has_dst is true then also:
"tz_dst_start" date in the following format: MM:DD HH[:MM[:SS]]
"tz_dst_end" the same as above
"tz_dst_offset" (long)
*/
bool SetTz(PT::Space & space);
/*
converting from UTC to local time
*/
time_t CalcLocalOffset(const PT::Date & utc_date);
time_t ToLocal(time_t utc_time);
PT::Date ToLocal(const PT::Date & utc_date);
/*
converting from local time to UTC
*/
time_t CalcUTCOffset(const PT::Date & local_date);
time_t ToUTC(time_t local_time);
PT::Date ToUTC(const PT::Date & local_date);
// return a Dst structure for the specified year
// or null if it not exists
// this method can return a Dst structure for earlier year than 'year'
// if 'year' doesn't exist
Dst * FindDst(int year);
// a time zone name
// this is a key to locale
std::wstring name;
// each locale has its own identifier
size_t id;
// time zone offset (in seconds)
time_t offset;
// daylight saving time map
// year -> Dst
// if there is not a specified year we are taking the lower year, e.g.
// if we are looking for 2010 and there is no such a year then we take 2009
// (or 2008 if 2009 not exists etc)
typedef std::map<int, Dst> DstMap;
DstMap dst_map;
private:
time_t ParseStrOffset(const wchar_t * str);
time_t GetOffset(PT::Space & space);
bool SetTzDst(PT::Space & year);
};
} // namespace Winix
#endif