1. 简介
TimeZone用于GMT时间和本地时区时间转换。
2. 类与接口
detail
// 记录GMT时间和本地时间的转化信息基本单元
struct Transition {
time_t gmttime; // GMT时间
time_t localtime; // 本地时区时间
int localtimeIdx; // Data.localtimes里对应的本地时区时间索引
...
};
// 比较算子,用于Transition的比较
struct Comp {...}
// 记录本地时区时间信息基本单元
struct Localtime {
time_t gmtOffset; // GMT时间转化为本地时间的差值
bool isDst; // 夏令时标记
int arrbIdx; // Data.names对应的时区名名索引
...
};
// 将秒数seconds转化为 struct tm格式时间,参数utc来存储转换后的结果
inline void fillHMS(unsigned seconds, struct tm* utc)
// 提供文件读取工具函数,方便读取指定字节的文本信息,主要用于读取特定格式的时区文本信息
class File : noncopyable{...}
// 读取时区信息文本zonefile中信息来初始化时区信息对象data
// 只会返回true,除非异常
bool readTimeZoneFile(const char* zonefile, struct TimeZone::Data* data)
// 从data里找到与sentry对应的本地时间
// 返回找到的合适的Localtime
const Localtime* findLocaltime(const TimeZone::Data& data, Transition sentry, Comp comp)
TimeZone
// 时区数据管理类,主要用于管理GMT时间和本地时区时间的转化
// 协调世界时UTC不与任何地区位置相关,也不代表此刻某地的时间,所以在说明某地时间时要加上时区
// 也就是说GMT并不等于UTC,而是等于UTC+0,只是格林尼治刚好在0时区上。
// GMT = UTC+0
struct TimeZone::Data {
vector<detail::Transition> transitions;
vector<detail::Localtime> localtimes;
vector<string> names;
string abbreviation;
};
// 根据自定义格式的时区信息文件内信息初始化
TimeZone::TimeZone(const char* zonefile)
: data_(new TimeZone::Data)
// 根据eastOfUtc时间(秒数)和name(时区名)初始化
TimeZone::TimeZone(int eastOfUtc, const char* name)
: data_(new TimeZone::Data)
// 将1970年1月1日0时0分0秒到此时的秒数seconds转化为struct tm格式时间
// 返回转换后的struct tm时间
struct tm TimeZone::toLocalTime(time_t seconds) const
// 将struct tm格式时间转化为1970年1月1日0时0分0秒到此时的秒数
// 返回转化后的秒数
time_t TimeZone::fromLocalTime(const struct tm& localTm) const
// 将secondsSinceEpoch秒数转化为struct tm,参数yday为处理tm.tm_yday标记
// 返回转化后的struct tm
struct tm TimeZone::toUtcTime(time_t secondsSinceEpoch, bool yday)
// 将struct tm形式的utc时间转化为time_t形式的utc时间
// 返回转化后的time_t形式utc时间
time_t TimeZone::fromUtcTime(const struct tm& utc)
// 将年月日时分秒转化为time_t形式的utc时间
// 返回转化后的time_t形式utc时间
time_t TimeZone::fromUtcTime(int year, int month, int day,
int hour, int minute, int seconds)
3. test_timezone.cc
//
// test_timezone.cc
// test_timezone
//
// Created by blueBling on 22-04-08.
// Copyright (c) 2022年blueBling. All rights reserved.
//
#include "TimeZone.h"
#include "Date.h"
#include "TimeStamp.h"
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
void PrintTm(struct tm& T) {
cout << T.tm_year << "-" << T.tm_mon << "-" << T.tm_mday << endl;
cout << T.tm_hour << "-" << T.tm_min << "-" << T.tm_sec << endl;
cout << T.tm_wday << endl;
}
// https://blog.csdn.net/qq_39898877/article/details/107884806
int test_timezone() {
Timestamp timeStamp = Timestamp::now();
struct tm T = TimeZone::toUtcTime(timeStamp.secondsSinceEpoch());//通过类调用静态成员函数
PrintTm(T);//这个打印的月份要+1才是正确的月份时间,见Date.cc中的构造函数。
Date dt(T);
cout << dt.toIsoString() << endl;//显示正确的年月日
//TimeZone timeZone(8, "China");//作者希望传入的是偏移的秒数,而不是偏移的时区数
TimeZone timeZone(8*60*60, "China");
struct tm T2 = timeZone.toLocalTime(timeStamp.secondsSinceEpoch());
PrintTm(T2);//这个能显示正确的时分秒
return 0;
}
int main() {
test_timezone();
return 0;
}
输出结果:
122-3-8
7-58-35
5
2022-04-08
122-3-8
15-58-35
5