You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
winix/winixd/core/timezone.h

171 lines
4.2 KiB

/*
* This file is a part of Winix
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2012-2014, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#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