C language to get the current system time (2nd Edition)

C language to get the current system time

Program: localtime ()

Advantage: only the C standard library; disadvantages: only accurate to the second stage

time_t is defined in time.h of a type that represents a calendar time, that is, from at 0:00:00 on January 1, 1970 to the number of seconds this time, the prototype is:

 typedef long time_t;        /* time value */

It can be seen time_t is actually a long integer, limited value due to the long integer can be expressed, and therefore it represents the latest time at 19:18 on January 2038 at 14 minutes 07 seconds.

A function of time can obtain the current calendar time time, the definition of time:
time_t time (time_t *)

time_t (typedef __int64 time_t) is just a long integer, does not meet our habits, you need to convert local time, it is necessary to use tm structure, the structure tm time.h prototype is:

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 */  
       };  

As can be seen, the agency defines the year, month, day, hour, minute, second, week, one day in the year, daylight saving time. This structure can easily display the time.

Get current system time with localtime, the function is a converting time_t time to time tm structure represented by the function prototype:
struct tm * localtime (const time_t *)
using gmtime function to get GMT, the function prototype:
struct tm * gmtime (const time_t *)

Output 1:

#include <iostream>  
#include <time.h>  
using namespace std;  
void dsptime(const struct tm *); //输出时间。  
  
int main(void)  
{  
 time_t nowtime;  
 nowtime = time(NULL); //获取日历时间  
 cout << nowtime << endl;  //输出nowtime  
  
 struct tm *local,*gm;  
 local=localtime(&nowtime);  //获取当前系统时间  
 dsptime(local);   
 gm=gmtime(&nowtime);  //获取格林尼治时间  
 dsptime(gm);  
    
 return 0;  
}  
void dsptime(const struct tm * ptm)  
{  
 char *pxq[]={"日","一","二","三","四","五","六"};  
 cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;  
 cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;  
 cout << " 星期" <<pxq[ptm->tm_wday] << " 当年的第" << ptm->tm_yday << "天 " << endl;  
}  

Output 2:

#include <time.h>   
#include <stdio.h>   
int main( void )   
{   
    time_t t = time(0);   
    char tmp[64];   
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );   
    puts( tmp );   
    return 0;   
}  

C / C ++ in the time.h provided in a custom function strftime time format, the function prototype:
size_t strftime (strDest char *, size_t MAXSIZE, the format const char *, const struct * timeptr (TM));
usable formatting characters string:

%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 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号
···
Published 14 original articles · won praise 8 · views 1160

Guess you like

Origin blog.csdn.net/Horse_Lake/article/details/103935800