Date and Time (C / C ++)

To refer to the program <ctime> C ++ header file inherits the C language structures and functions for the date and time of the operation, prior to use

There are four time-related types: clock_t, time_t, size_t, and tm. Type clock_t, size_t, time_t and the system time and date can be represented as a certain integer.

Tm structure to save the time and date in the form of C structure, tm structure defined as follows:

struct (TM) 
{ 
    int tm_sec; // seconds, the normal range of 0 to 59, but to allow 61 is 
    int tm_min; // min 59 0 ~ 
    int tm_hour; // h 23 is 0 ~ 
    int tm_mday;     // first few of the month day 
    int tm_mon;     // May 11 ~ 0 
    int tm_year;     // since several years beginning in 1900, 
    int tm_wday;     // the first few days of the week 
    int tm_yday;     // first day of the year 
    int tm_isdst; // Daylight Saving Time when     
}

related functions:

function

description

time_t time(time_t *time);

This function returns the current system of calendar time. Since January 1, 1970 after a number of seconds, if the system did not have time, return -1

char *ctime(const time_t *time);

This function returns a pointer to a string that represents the local time, string day month year hours: minutes: seconds year \ n \ 0

struct tm *localtime(const time_t *time);

This function returns a pointer to a pointer indicating local time tm structure.

clock_t clock(void);

The program execution returns from the function, the time processor time used, if time is not available, or -1

char *asctime(const struct tm *time);

This function returns a pointer to the string, a string containing information structure stored time points, returned in the form of: day month year hours: minutes: seconds year \ n \ 0

struct tm *gmtime(const time_t *time);

This function returns a pointer pointing to time, time is tm structure, with Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT) representation

time_t mktime(struct tm *time);

This function returns the calendar time, time corresponding to the time points stored in the configuration

double difftime(time_t time2,time_t time1);

This function returns the number of seconds of the phase difference between time1 and time2

size_t strftime();

This function can be used to format the date and time specified format

Example:

#include <the iostream> 
#include <the ctime>
 the using  namespace STD; 

int main () 
{ 
    // based on the current system date and time initialization 0 
    time_t Time now = ( 0 );

     / convert now to string
     char * dt = the ctime (& now); 

    COUT << " local DATE and Time:   " << dt << endl; 

    // the structure now converted to tm 
    tm = gmtm gmtime, * (& now); 
    dt = the asctime (gmtm); 
    COUT << " UTC DATE and Time:   " << << dt endl;
    return 0;
}

operation result:

exbot@ubuntu:~/wangqinghe/C++/time$ ./time

local date and time:  Mon Aug  5 14:54:25 2019

 

UTC date and time :  Mon Aug  5 06:54:25 2019

 

Formatting structure using time tm

#include<iostream>
#include<ctime>
using namespace std;

int main()
{
    time_t now = time(0);
    
    cout << "from 1970  then the seconds passed : " << now << endl;

    tm* ltm = localtime(&now);

    cout << "year : " << 1900 + ltm->tm_year << endl;
    cout << "month : " << 1 + ltm->tm_mon << endl;
    cout << "day : " << ltm->tm_mday << endl;
    cout << "hour : " << ltm->tm_hour << ":";
    cout << ltm->tm_min << ":";
    cout << ltm->tm_sec << endl;
    return 0;
}

operation result:

exbot@ubuntu:~/wangqinghe/C++/time$ ./time1

from 1970  then the seconds passed : 1564988067

year : 2019

month : 8

day : 5

hour : 14:54:27

 

In 20xx-xx-xx xx: xx: xx output format:

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>

using namespace std;

string Get_Current_Date();

int main()
{
    cout << Get_Current_Date().c_str() << endl;
    return 0;
}

string Get_Current_Date()
{
    time_t nowtime;
    nowtime = time(NULL);
    char tmp[64];
    strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",localtime(&nowtime));
    return tmp;
}

operation result:

exbot@ubuntu:~/wangqinghe/C++/time$ ./time2

2019-08-05 15:00:14

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11305023.html