C++ strptime 和strptime函数的使用方式

本文介绍如何使用strptime和strftime函数进行时间格式转换,包括从字符串到tm结构体,再到时间戳的转换过程。同时,展示了mktime和localtime函数在时间管理中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间标签(timestamp)的格式为RFC3339格式。

strptime  和strptime函数

 

#include <stdio.h>      /* puts, printf */
#include <time.h>       /* time_t, struct tm, time, localtime */
#include <string>
using namespace std;
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
char buffer [80];
  time (&rawtime);
  timeinfo = localtime (&rawtime);
 int length= strftime (buffer,80,"%Y-%m-%dT%H:%M:%SZ\n",timeinfo);
 
  
  printf ("timestamp: %.*s",length,buffer);

  return 0;
}

strptime 函数将字符串转换成 tm 结构

#include <stdio.h>
       #include <stdlib.h>
       #include <string.h>
       #include <time.h>
using namespace std;
       int
       main(void)
       {
           struct tm *timeinfo;
           char buffer[80];
           memset( timeinfo, 0, sizeof(struct tm));
           strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S",  timeinfo);
            int length= strftime (buffer,80,"%Y-%m-%dT%H:%M:%SZ\n",timeinfo);
            printf ("timestamp: %.*s",length,buffer);
           exit(EXIT_SUCCESS);
       }

localtime 和mktime 函数

mktime 将tm 结构转换成 time_t

localtime 将time_t 转换成 tm结构时间

gmtime 函数 Convert time_t to tm as UTC time

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值