linux 结构体tm,C++ strftime和std::get_time对linux struct tm的作用-Go语言中文社区

代码说明一切。

strftime给 struct tm结构体的日期的年+1900,月份+1,并按格式转成字符串

std::get_time则对字符串获取struct tm结构体,年份-1900,月份-1

代码如下:

#include

#include

#include

#include

#include

#include

#include

int main()

{

time_t time_T;

time_T = time(NULL);

struct tm *tmTime;

// tm对象格式的时间

tmTime = localtime(&time_T);

printf("nlocaltime %d-%d-%d %d:%d:%dn",

(*tmTime).tm_year ,

(*tmTime).tm_mon,

(*tmTime).tm_mday,

(*tmTime).tm_hour,

(*tmTime).tm_min,

(*tmTime).tm_sec);

const char* format = "%Y-%m-%d %H:%M:%S";

char strTime[100];

strftime(strTime, sizeof(strTime), format, tmTime);

printf("nlocaltime atfter strftime string %s n",strTime );

struct tm time_struct;

std::istringstream ss(strTime);

ss>> std::get_time(&time_struct, "%Y-%m-%d %H:%M:%S");

printf("nstd::get_time %d-%d-%d %d:%d:%dn", time_struct.tm_year ,

time_struct.tm_mon ,

time_struct.tm_mday,

time_struct.tm_hour,

time_struct.tm_min ,

time_struct.tm_sec);

return 0;

}

编译,需要C++11以上

g++ New0001.cpp -std=c++11

运行结果

bc0e12ec10c8815309f2cb3cb19e9a56.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值