《C++--UTC时间ISO8601格式与本地时间转换》

做c/s开发,肯定要与时间打交道。
很多时候,获取本地的时间,也就是大陆境内的东八区时间。但是为了保证国际化,服务器往往采用的是0区的时间。

之前博客介绍了C++中关于时间的函数:
http://blog.csdn.net/wangshubo1989/article/details/50500515

首先明确几个概念:
UTC时间与GMT时间
Greenwich Mean Time (GMT) is a term originally referring to mean solar time at the Royal Observatory, Greenwich where a system was first developed around 1850 for tracking time based on the rotation of the Earth. It is now often used to refer to Coordinated Universal Time (UTC) when this is viewed as a time zone.

UTC与GMT的区别:
这里写图片描述

时间戳
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数

本地时间
UTC + (+0800) = 本地(北京)时间

ISO8601
很多人问ISO8601与UTC的区别,其实两者完全是两个东西,通俗的说,iso8601是一种utc时间的表示方式而已。
例如:

2016-12-23T08:53:02.418Z

下面介绍几个结构体及函数:

std::tm
Structure holding a calendar date and time broken down into its components.
别忘了加入头文件:

 <ctime>

std::get_time
When used in an expression in >> get_time(tmb, fmt), parses the character input as a date/time value according to format string fmt according to the std::time_get facet of the locale currently imbued in the input stream in. The resultant value is stored in a std::tm object pointed to by tmb.

别忘了加入头文件:

 <iomanip>

_mkgmtime
Converts a UTC time represented by a tm struct to a UTC time represented by a time_t type.

strftime
Copies into ptr the content of format, expanding its format specifiers into the corresponding values that represent the time described in timeptr, with a limit of maxsize characters.
在之前的博客也有介绍,这里就不赘述了。

ISO8601格式转本地时间

std::string ISO8601ToLocaltime(const std::string& time)
{
    struct std::tm time_struct;
    std::istringstream ss(time);
    ss >> std::get_time(&time_struct, "%Y-%m-%dT%H:%M:%SZ");
    std::time_t time_unix = _mkgmtime(&time_struct);
    if (time_unix == -1)
    {
        return "";
    }
    char current_time[32];
    strftime(current_time, sizeof(current_time), "%Y-%m-%d %H:%M:%S", localtime(&time_unix));
    std::string local_time_str(current_time);

    return local_time_str;
}

本地时间转ISO8601格式
这里有两种写法,一种是最新的,适用于VS2015:

std::time_t course_start_time1 = 1483232400;
  char buf[32];
  strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&course_start_time1));
  std::cout << buf << std::endl;

另一种是适用于VS2013的:

std::string Win32Helper::LocaltimeToISO8601(std::time_t time)
{
    char buf[32];
    strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&time));
    std::string iso8601_time_str(buf);

    return iso8601_time_str;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 C++ 函数,可以将 hhmm.ss utc 格式时间转换为本地时间: ```cpp #include <iostream> #include <iomanip> #include <ctime> using namespace std; time_t utc_to_local_time(const string& utc_time) { int hh, mm; double ss; char sign; sscanf(utc_time.c_str(), "%2d%2d.%lf %c", &hh, &mm, &ss, &sign); int offset = (sign == '-' ? -1 : 1) * (hh * 3600 + mm * 60 + (int)ss); time_t t = time(NULL); tm* local_time = localtime(&t); time_t local_t = mktime(local_time); local_t -= offset; return local_t; } int main() { string utc_time = "0800.00 +"; time_t local_t = utc_to_local_time(utc_time); tm* local_time = localtime(&local_t); cout << "Local Time: " << put_time(local_time, "%Y-%m-%d %H:%M:%S") << endl; return 0; } ``` 函数 `utc_to_local_time` 接受一个字符串,其中包含 hhmm.ss utc 格式时间。该函数首先使用 `sscanf` 函数从字符串中提取出小时、分钟、秒和时区符号,并将其转换为整数和浮点数表示。然后,该函数计算出本地时间UTC 时间之间的偏移量,并使用 `mktime` 函数将本地时间转换时间戳。最后,该函数从时间戳中减去偏移量,以获得 UTC 时间对应的本地时间。函数返回本地时间时间戳。 在主函数中,我们调用 `utc_to_local_time` 函数,并使用 `localtime` 函数将返回的时间转换为本地时间。然后,我们使用 `put_time` 函数将本地时间格式化为字符串,并输出到控制台上。 注意,该函数假设 hhmm.ss utc 格式时间中的小时、分钟和秒数都是有效的,因此没有进行任何错误检查。在实际的应用程序中,您可能需要添加更多的错误处理代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一苇渡江694

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值