2012-05-30 21:04:18 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2012-2014, Tomasz Sowa
|
2012-05-30 21:04:18 +02:00
|
|
|
* All rights reserved.
|
2014-10-04 20:04:03 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2012-05-30 21:04:18 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef headerfile_winix_core_timezone
|
|
|
|
#define headerfile_winix_core_timezone
|
|
|
|
|
|
|
|
#include <ctime>
|
2012-06-27 01:19:19 +02:00
|
|
|
#include <map>
|
2012-05-30 21:04:18 +02:00
|
|
|
#include "date/date.h"
|
|
|
|
#include "space/space.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-30 21:04:18 +02:00
|
|
|
class TimeZone
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
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
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::Date start, end;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
// 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
|
2021-05-20 20:59:12 +02:00
|
|
|
bool IsDstUsed(const pt::Date & date) const;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-05-20 20:59:12 +02:00
|
|
|
int Compare(const pt::Date & date1, const pt::Date & date2) const;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-30 21:04:18 +02:00
|
|
|
TimeZone();
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*/
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-06-23 01:34:33 +02:00
|
|
|
reading zime zone values from Space struct (tz_id is skipped)
|
|
|
|
the space struct should have:
|
2012-05-30 21:04:18 +02:00
|
|
|
"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)
|
|
|
|
*/
|
2021-05-20 20:59:12 +02:00
|
|
|
bool SetTz(pt::Space & space);
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
converting from UTC to local time
|
|
|
|
*/
|
2021-05-20 20:59:12 +02:00
|
|
|
time_t CalcLocalOffset(const pt::Date & utc_date);
|
2012-05-30 21:04:18 +02:00
|
|
|
time_t ToLocal(time_t utc_time);
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::Date ToLocal(const pt::Date & utc_date);
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
converting from local time to UTC
|
|
|
|
*/
|
2021-05-20 20:59:12 +02:00
|
|
|
time_t CalcUTCOffset(const pt::Date & local_date);
|
2012-05-30 21:04:18 +02:00
|
|
|
time_t ToUTC(time_t local_time);
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::Date ToUTC(const pt::Date & local_date);
|
2012-05-30 21:04:18 +02:00
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// 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;
|
2012-05-30 21:04:18 +02:00
|
|
|
|
2012-06-23 01:34:33 +02:00
|
|
|
// each locale has its own identifier
|
2012-06-27 01:19:19 +02:00
|
|
|
size_t id;
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
// time zone offset (in seconds)
|
2012-06-27 01:19:19 +02:00
|
|
|
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;
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
private:
|
2012-05-30 21:04:18 +02:00
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
time_t ParseStrOffset(const wchar_t * str);
|
2021-05-20 20:59:12 +02:00
|
|
|
time_t GetOffset(pt::Space & space);
|
|
|
|
bool SetTzDst(pt::Space & year);
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
2012-05-30 21:04:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|