c/c++时间类型

1、time_t

time_t 类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。

time_t cur = time(NULL);

2、struct timeval

在time.h中的定义为:

struct timeval
{
	__time_t tv_sec;        /* Seconds. */
	__suseconds_t tv_usec;    /* Microseconds. */
};

其中,tv_sec为Epoch(1970-1-1零点零分)到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。毫秒(ms): tv.tv_usec/1000。

gettimeofday(&tv, NULL);//获取当前时间

3、struct tm

在标准库wchar.h内,我们可以看到结构体tm的声明如下:

#ifndef _TM_DEFINED
struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };
#define _TM_DEFINED
#endif  /* _TM_DEFINED */
  1. 注意月份是0-11,而不是1-12,所以在tm结构体与string转换的时候,要相应的做减1加1处理。
  2. tm_isdst为夏令时设置,0为非夏令时,1为夏令时。由于21世纪的中国并没有实行夏令时制度,所以编写国内程序我们可以忽略这个变量。

利用这个结构体,我们就可以完成日期时间与string字符串的转换了,由于计算的方便,我们一般选择将日期时间的string转换成time_t类型。

4、常用的时间函数

 //取得从1970年1月1日至今的秒数
time_t time(time_t *t);

 //将结构中的信息转换为真实世界的时间,以字符串的形式显示 // Www Mmm dd hh:mm:ss yyyy 其中,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份。
char *asctime(const struct tm *tm);

//将timep转换为真实世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样 //  Mon Jan 11 08:23:14 2021
char *ctime(const time_t *timep); 

//将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针
struct tm *gmtime(const time_t *timep); 

//和gmtime类似,但是它是经过时区转换的时间。
struct tm *localtime(const time_t *timep); 
struct tm *localtime_r(const time_t *timep, struct tm *result); //线程安全的

 //将struct tm 结构的时间转换为从1970年至今的秒数
time_t mktime(struct tm *tm);

//返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用
int gettimeofday(struct timeval *tv, struct timezone *tz); 

//返回两个时间相差的秒数
double difftime(time_t time1, time_t time2); 
asctime_r(), ctime_r(), gmtime_r() // 时间函数的 _r 版本都是线程安全的。

//时间转字符串
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr); 

//字符串转时间

char *strptime(const char *str, const char *format, struct tm *timeptr); 

5、日期时间处理与字符串string转换

c/c++日期时间处理与字符串string转换

  1. 日期时间string转换为time_t

    time_t StringToDatetime(string str)
    {//2021-08-25 12:00:00
        tm tm_;                                    // 定义tm结构体。
        sscanf(str.c_str(), "%d-%d-%d %d:%d:%d", &tm_.tm_year, &tm_.tm_mon, &tm_.tm_mday, &tm_.tm_hour, &tm_.tm_min, &tm_.tm_sec);// 将string存储的日期时间,转换为int临时变量。
        tm_.tm_year -=  1900;                 // 年,由于tm结构体存储的是从1900年开始的时间,所以tm_year减去1900。
        tm_.tm_mon -= 1;                    // 月,由于tm结构体的月份存储范围为0-11,所以tm_mon减去1。
        tm_.tm_isdst = 0;                          // 非夏令时。
        time_t t_ = mktime(&tm_);                  // 将tm结构体转换成time_t格式。
        return t_;                                 // 返回值。
    }
    
  2. 日期时间time_t转换为string

    string DatetimeToString(time_t time)
    {
        tm *tm_ = localtime(&time);                // 将time_t格式转换为tm结构体
        char s[64]={0};      // 定义总日期时间char*变量。
        sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d", tm_->tm_year+1900, tm_->tm_mon + 1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);// 将年月日时分秒合并。
        return string(s);  // 返回转换日期时间后的string变量。
    }
    
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值