C++时间日期相关函数

C++时间日期相关函数

头文件#include <ctime>

time()

time_t time(time_t * time)

返回系统当前日历时间,自1970.1.1以来经过的秒数。如果系统没有时间,返回-1

ctime()

char *ctime(const time_t *timer)

返回的是一个表示当地时间的字符串,当地时间是基于参数timer:
timer是指向time_t对象的指针,该对象包含了一个日历时间

#include <stdio.h>
#include <time.h>

int main()
{
	time_t curtime;
	time(&curtime);
	printf("current time = %s", ctime(&curtime));
	return 0;
}

运行结果:

current time = Wed Dec 21 11:38:03 2022

localtime()

struct tm * localtime(const time_t * time)

返回一个指向表示本地时间的tm结构的指针

mktime()

time_t mktime(struct tm *timeptr)

原型表示指向日历时间的time_t值的指针
返回自1970.1.1以来持续的秒数。如果发生错误,则返回-1

#include <stdio.h>
#include <time.h>

int main()
{
    int retl
    struct tm info;
    char buffer[80];
    
    info.tm_year = 2021 -1900;
    info.tm_mo = 7 - 1;
    info.tm_mday = 4;
    info.tm_hour = 0;
    info.tm_min = 0;
    info.tm_sec = 1;
    info.tm_isdst = -1;

    ret = mktime(&info);
    if( ret == -1 ) {
        printf("Error: unable to make time using mktime\n");
    } else {
        strftime(buffer, sizeof(buffer), "%c", &info );
        printf(buffer);
    }

    return(0);
}
Sun Jul  4 00:00:01 2021

strptime()

将字符串转换为分解的时间struct tm
原型:

/* Parse S according to FORMAT and store binary time information in TP.
   The return value is a pointer to the first unparsed character in S.  */
extern char *strptime (__const char *__restrict __s,
                       __const char *__restrict __fmt, 
					 struct tm *__tp);

param 1: 输入一个char 的指针,可通过c_str()兼容
param 2: 统一为一个char
的指针, 用于格式控制的字符串指针,可通过c_str()兼容
param 3: 分解时间的存储,struct tm类型的指针,可定义一个struct tm类型,然后&实现

#include <iostream>
#include <string>
#include <time.h>
using namespace std;
 
int main()
{
    string mystr="2018-07-27 23:50";
    string myformat="%Y-%m-%d %H:%M";
    struct tm mytm;

    strptime(mystr.c_str(),myformat.c_str(),&mytm);
 
    cout<<"print mytm:"<<endl;
    cout<<"tm_min:"<<mytm.tm_min<<endl;
    cout<<"tm_hour:"<<mytm.tm_hour<<endl;
    cout<<"tm_mday:"<<mytm.tm_mday<<endl;
    cout<<"tm_mon:"<<mytm.tm_mon<<endl;
    cout<<"tm_year:"<<mytm.tm_year<<endl;
 
}

strftime()

与strptime()函数相对应的是,strftime()将结构化时间转换到字符串中
原型:

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm * timeptr)

根据format中定义的格式化规则,格式化结构timeptr表示的时间,并存储在str中
如果产生的字符小于size个字符(包含空结束字符),则会返回复制到str中的字符总数(不包括空结束字符),否则返回0
param 1: 指向目标数组的指针,产生的字符串
param 2: 复制到str的最大字符数
param 3: 包含了普通字符和特殊格式说明符的任何组合
param 4: 指向tm结构的指针

   time_t rawtime;
   struct tm *info;
   char buffer[80];
 
   time( &rawtime );
 
   info = localtime( &rawtime );
 
   strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
   printf("格式化的日期 & 时间 : |%s|\n", buffer );
  
   return(0);
格式化的日期 & 时间 : |2022-12-16 11:36:42|

[1]: c++ 日期&时间:https://www.runoob.com/cplusplus/cpp-date-time.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值