-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeZone.cpp
More file actions
84 lines (75 loc) · 2.6 KB
/
Copy pathTimeZone.cpp
File metadata and controls
84 lines (75 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "TimeZone.h"
using namespace std;
// 全局时区数据表
static TimeZoneInfo timezone_table[] = {//城市名称,国家,冬令时UTC偏移,夏令时UTC偏移,是否采用夏令时,是否南半球
{L"北京", L"中国", 8, 8, false, false},
{L"香港", L"中国", 8, 8, false, false},//深度贯彻“一个中国原则”
{L"新加坡", L"新加坡", 8, 8, false, false},
{L"东京", L"日本", 9, 9, false, false},
{L"首尔", L"韩国", 9, 9, false, false},
{L"悉尼", L"澳大利亚", 10, 11, true, true},
{L"伦敦", L"英国", 0, 1, true, false},
{L"巴黎", L"法国", 1, 2, true, false},
{L"柏林", L"德国", 1, 2, true, false},
{L"罗马", L"意大利", 1, 2, true, false},
{L"莫斯科", L"俄罗斯", 3, 3, false, false},
{L"纽约", L"美国", -5, -4, true, false},
{L"芝加哥", L"美国", -6, -5, true, false},
{L"丹佛", L"美国", -7, -6, true, false},
{L"洛杉矶", L"美国", -8, -7, true, false},
{L"多伦多", L"加拿大", -5, -4, true, false},
{L"温哥华", L"加拿大", -8, -7, true, false}
};
TimeZoneInfo* TimeZone::get_all_timezones(int& count)
{
count = sizeof(timezone_table) / sizeof(TimeZoneInfo);
return timezone_table;//返回指向数组第一个元素的指针
}
// 根据城市名称查找时区信息
TimeZoneInfo TimeZone::get_timezone_by_city(const wstring& city_name)
{
int count = 0;
TimeZoneInfo* zones = get_all_timezones(count);
//遍历查找
for (int i = 0; i < count; i++)
{
if (zones[i].city_name == city_name)
{
return zones[i];//找到了
}
}
// 没找到,默认返回北京时区
return zones[0];
}
// 判断是否处于夏令时(简化版本)
bool TimeZone::is_daylight_saving_time(int month, int day, bool is_southern)
{
if (is_southern)
{
// 南半球:10月第一个周日 ~ 4月第一个周日
// 简化判断:10月-3月为夏令时
return (month >= 10 || month <= 3);
}
else
{
// 北半球:3月最后一个周日 ~ 10月最后一个周日
// 简化判断:4月-9月为夏令时
return (month >= 4 && month <= 9);
}
}
int TimeZone::get_utc_offset(const TimeZoneInfo& tz, int month, int day)
{
if (!tz.has_dst)
{
return tz.utc_offset_winter; // 无夏令时,返回标准偏移
}
// 有夏令时,判断当前是否处于夏令时期间
if (is_daylight_saving_time(month, day, tz.is_southern))
{
return tz.utc_offset_summer;
}
else
{
return tz.utc_offset_winter;
}
}