C/C++时间函数(Linux)

获取当前时间最简单的函数方法

#include 

time_t time(time_t* timer);

注:time_t是长整形类型(long int),返回值为返回距计算机元年的秒数,一般timer置为NULL。

使用C++标准库函数

参考:C++时间函数标准库

函数原型

#include 

// time_t是长整形类型(long int)

struct tm
{
    int tm_sec;    // 秒 – 取值区间为[0,59]
    int tm_min;    // 分 - 取值区间为[0,59]        
    int tm_hour;   // 时 - 取值区间为[0,23]        
    int tm_mday;   // 一个月中的日期 - 取值区间为[1,31]      
    int tm_mon;    // 月份(从一月开始,0代表一月) - 取值区间为[0,11]       
    int tm_year;   // 年份,其值等于实际年份减去1900
    int tm_wday;   // 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 
    int tm_yday;   // 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日以此类推
    int tm_isdst;  // 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0
};

// 获取当前时间,返回值为距计算机元年的秒数
time_t time (time_t* timer);
// 将tm结构体的时间转换为time_t
time_t mktime (struct tm * timeptr);
// 将tm结构体的时间转换为时间字符串
char* asctime (const struct tm * timeptr);
// 将time_t时间转换为时间字符串
char* ctime (const time_t * timer);
// 将time_t时间转换为世界统一tm结构体时间
struct tm * gmtime (const time_t * timer);
// 将time_t时间转换位tm结构体时间
struct tm * localtime (const time_t * timer);
// 将tm结构体字符串转化为格式化时间字符串
size_t strftime (char* ptr, size_t maxsize, const char* format,  const struct tm* timeptr);

注:strftime格式化函数的format格式
%a 星期几的简写  
%A 星期几的全称  
%b 月分的简写  
%B 月份的全称  
%c 标准的日期的时间串  
%C 年份的后两位数字
%d 十进制表示的每月的第几天  
%D 月/天/年  
%e 在两字符域中,十进制表示的每月的第几天  
%F 年-月-日  
%g 年份的后两位数字,使用基于周的年  
%G 年分,使用基于周的年  
%h 简写的月份名  
%H 24小时制的小时  
%I 12小时制的小时  
%j 十进制表示的每年的第几天  
%m 十进制表示的月份  
%M 十时制表示的分钟数  
%n 新行符  
%p 本地的AM或PM的等价显示  
%r 12小时的时间  
%R 显示小时和分钟:hh:mm  
%S 十进制的秒数  
%t 水平制表符  
%T 显示时分秒:hh:mm:ss  
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)  
%U 第年的第几周,把星期日做为第一天(值从0到53)  
%V 每年的第几周,使用基于周的年  
%w 十进制表示的星期几(值从0到6,星期天为0)  
%W 每年的第几周,把星期一做为第一天(值从0到53)  
%x 标准的日期串  
%X 标准的时间串  
%y 不带世纪的十进制年份(值从0到99)  
%Y 带世纪部分的十制年份  
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。  
%% 百分号

C++时间标准库函数之间的关系

C/C++时间函数(Linux)_第1张图片
标准库函数之间的关系

示例代码

#include 
#include 
#include 

using namespace std;

int main()
{
    time_t      curTime;
    time_t      tmToCurTime;
    struct tm   *utcTm;
    struct tm   *curTm;
    char        szTime[40];

    // 获取当前时间
    curTime = time(NULL);
    cout << "Cur Time: " << curTime << endl;

    // 将time_t时间转换为世界统一struct tm结构体时间
    utcTm = gmtime(&curTime);

    // 将time_t时间转换为struct tm结构体时间
    curTm = localtime(&curTime);

    // 将struct tm结构体时间转换为时间字符串
    cout << "struct tm to time string: " << asctime(curTm) << endl;

    // 将struct tm结构体时间转换为time_t
    cout << "struct tm to time_t: " << mktime(curTm) << endl;

    // 将time_t字符串转换为时间字符串
    cout << "time_t to time string: " << ctime(&curTime) << endl;

    // 将struct tm结构体时间转为格式化时间字符串
    memset(szTime, 0, sizeof(szTime));
    strftime(szTime, sizeof(szTime) - 1, "%F %T", curTm);
    cout << "Format time string: " << szTime << endl;

    return 0;
}

执行结果

Cur Time: 1474986855
struct tm to time string: Tue Sep 27 22:34:15 2016

struct tm to time_t: 1474986855
time_t to time string: Tue Sep 27 22:34:15 2016

Format time string: 2016-09-27 22:34:15

使用linux系统函数

参考:Linux系统函数标准库

函数原型

#include

// timeval 结构体
struct timeval
{    
    long tv_sec;   //秒    
    long tv_usec;  //微秒
};

// timezone 结构体
struct timezone
{
    int tz_minuteswest;  // 和Greenwich 时间差了多少分钟    
    int tz_dsttime;      // DST时间的修正方式
};

// 获取当前时间
int gettimeofday(struct timeval*tv, struct timezone *tz);

示例代码

#include 
#include 

using namespace std;

int main()
{
    struct timeval  tv;
    struct timezone tz;

    gettimeofday(&tv, &tz);

    // 返回秒
    cout << "seconds: " << tv.tv_sec << endl;
    // 返回毫秒
    cout << "microseconds: " << tv.tv_usec << endl;

    // 返回和Greenwich 时间差了多少分钟
    cout << tz.tz_minuteswest << endl;
    // 返回DST时间的修正方式
    cout << tz.tz_dsttime << endl;

    return 0;
}

执行结果

seconds: 1474989732
microseconds: 105807
0
0

综合使用C++标准函数库和Linux系统函数

格式化输出微秒级当前的时间(可在C++项目日志打印中使用)

#include 
#include 
#include 
#include 

using namespace std;

string GetCurrentFormatTimeString()
{
    struct timeval  tv;
    char            timeArray[40];
    stringstream    ss;

    gettimeofday(&tv, NULL);

    memset(timeArray, 0, sizeof(timeArray));

    strftime(timeArray, sizeof(timeArray) - 1, "%F %T", localtime(&tv.tv_sec));

    ss << string(timeArray) << "." << tv.tv_usec;

    return ss.str();
}

int main()
{
    cout << GetCurrentFormatTimeString() << endl;

    return 0;
}

执行结果

2016-09-27 23:52:35.225960

你可能感兴趣的:(C/C++时间函数(Linux))