文章预览
上篇文章: Linux-C++获取当前时间与计算时间间隔 ,介绍了ISO8601格式时间的生成,但未包含时区部分。 今天这篇,再来介绍一下时区部分的获取。 1、Linux获取时区 1.1 编程实现 Linux系统中,可以通过localtime来获取时间,并通过tm_gmtoff来获取时区偏移量,进而得到时区。 std :: string GetTimeZone () { // 获取当前时间结构体 time_t currentTime; time( ); // 将时间结构体转换为本地时间结构体 struct tm * localTime = localtime ( & currentTime ); // 获取时区偏移量(以秒为单位) int timezoneOffset = localTime->tm_gmtoff; // 根据偏移量计算时区差值(以小时为单位) int timezoneDiffHours = timezoneOffset / 3600 ; int timezoneDiffMinutes = (timezoneOffset % 3600 ) / 60 ; // 输出时区信息 char tmpBuff[ 32 ] = { 0 }; sprintf (tmpBuff, "%1s%02d:%02d" , timezoneOffset>= 0
………………………………