C + + Time acquisition

Source: Internet
Author: User
Tags local time string format time and date

Http://net.pku.edu.cn/~yhf/linux_c/function/04.html

 

Asctime (the time and date are represented in string format)
Related functions Time,ctime,gmtime,localtime
Table header File #include <time.h>
Defining functions char * asctime (const struct TM * timeptr);
Function description Asctime () Converts the information in the TM structure referred to by the parameter timeptr to the time-date representation used by the real world, and then returns the result in string form. This function has been converted from time zone to local time, and the string format is: "Wed June 21:49:08 1993\n"
return value If the associated time-date function is called again, the string may be corrupted. This function differs from CTime in that the parameters passed in are different structures.
Additional Instructions Returns a string representing the current local time date.
Example #include <time.h>
Main ()
{
time_t TIMEP;
Time (&TIMEP);
printf ("%s", Asctime (Gmtime (&TIMEP)));
}
Perform Sat Oct 28 02:10:06 2000
 

CTime (the time and date are represented in string format)
Related functions Time,asctime,gmtime,localtime
Table header File #include <time.h>
Defining functions Char *ctime (const time_t *TIMEP);
Function description CTime () Converts the information in the time_t structure referred to by the parameter TIMEP to the time-date representation used by the real world, and then returns the result in string form. This function has been converted from time zone to local time, and the string format is "Wed June 21:49:08 1993\n". If the associated time-date function is called again, the string may be corrupted.
return value Returns a string representing the current local time date.
Example #include <time.h>
Main ()
{
time_t TIMEP;
Time (&TIMEP);
printf ("%s", CTime (&TIMEP));
}
Perform Sat Oct 28 10:12:05 2000
 

Gettimeofday (get current time)
Related functions Time,ctime,ftime,settimeofday
Table header File #include <sys/time.h>
#include <unistd.h>
Defining functions int gettimeofday (struct timeval * TV, struct timezone * tz)
Function description Gettimeofday () returns the current time with a structure referred to by TV, and the local time zone information is placed in the structure that TZ refers to.
The TIMEVAL structure is defined as:
struct timeval{
Long tv_sec; /* sec */
Long tv_usec; /* microseconds */
};
The timezone structure is defined as:
struct timezone{
int tz_minuteswest; /* and Greenwich TIME DIFFERENCE how many minutes */
int tz_dsttime; /* Daylight Saving Time status */
};
Both of these structures are defined in/usr/include/sys/time.h. The status represented by Tz_dsttime is as follows
Dst_none/* Do not use */
DST_USA/* USA */
Dst_aust/* Australia */
Dst_wet/* Western Europe *
Dst_met/* Central Europe *
Dst_eet/* Eastern Europe *
Dst_can/* Canada */
DST_GB/* Britannia */
Dst_rum/* Romania */
Dst_tur/* Turkey * *
Dst_austalt/* Australia (after 1986) */
return value The success returns 0, the failure returns 1, and the error code is stored in errno. Additional instructions efault the memory space referred to by the pointer TV and TZ exceeds the access permission.
Example #include <sys/time.h>
#include <unistd.h>
Main () {
struct Timeval TV;
struct timezone tz;
Gettimeofday (&TV, &tz);
printf ("TV_SEC; %d\n ", tv,.tv_sec);
printf ("TV_USEC; %d\n ", tv.tv_usec);
printf ("Tz_minuteswest; %d\n ", tz.tz_minuteswest);
printf ("Tz_dsttime,%d\n", tz.tz_dsttime);
}
Perform tv_sec:974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
 

Gmtime (get current time and date)
Related functions Time,asctime,ctime,localtime
Table header File #include <time.h>
Defining functions struct Tm*gmtime (const TIME_T*TIMEP);
Function description Gmtime () Converts the information in the time_t structure referred to by the parameter TIMEP to the time-date representation used by the real world, and then returns the result from the Fabric TM.
The structure TM is defined as
struct TM
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int Tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
The int tm_sec represents the current number of seconds, the normal range is 0-59, but is allowed to 61 seconds
int Tm_min represents current score, range 0-59
int Tm_hour The number of hours from midnight, with a range of 0-23
int Tm_mday The number of days in the current month, range 01-31
The int Tm_mon represents the current month, starting from January, ranging from 0-11
int tm_year number of years since 1900
int Tm_wday The number of days of the week, starting from Monday, with a range of 0-6
int Tm_yday The number of days since January 1 this year, ranging from 0-365
int TM_ISDST Daylight Saving time flag
The time date returned by this function is not converted by the time zone, but UTC time.
return value Returns the structure TM representing the current UTC time
Example #include <time.h>
Main () {
Char *wday[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t TIMEP;
struct TM *p;
Time (&TIMEP);
P=gmtime (&TIMEP);
printf ("%d%d%d", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
printf ("%s%d;%d;%d\n", Wday[p->tm_wday], P->tm_hour, P->tm_min, p->tm_sec);
}
Perform 2000/10/28 Sat 8:15:38
 

LocalTime (get local current time and date)
Related functions Time, Asctime, CTime, gmtime
Table header File #include <time.h>
Defining functions struct TM *localtime (const time_t * TIMEP);
Function description LocalTime () Converts the information in the time_t structure referred to by the parameter TIMEP to the time-date representation used by the real world, and then returns the result from the Fabric TM. Please refer to Gmtime () for the definition of structure TM. The time date returned by this function has been converted to the local time zone.
return value Returns the structure TM representing the current local time.
Example #include <time.h>
Main () {
Char *wday[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t TIMEP;
struct TM *p;
Time (&TIMEP);
P=localtime (&TIMEP); /* Get local time */
printf ("%d%d%d", (1900+p->tm_year), (L+p->tm_mon), p->tm_mday);
printf ("%s%d:%d:%d\n", Wday[p->tm_wday],p->tm_hour, P->tm_min, p->tm_sec);
}
Perform 2000/10/28 Sat 11:12:22
 

Mktime (Converts the time structure data to elapsed seconds)
Related functions Time,asctime,gmtime,localtime
Table header File #include <time.h>
Defining functions time_t mktime (Strcut TM * timeptr);
Function description The Mktime () is used to convert the TM structure data referred to by the parameter timeptr to the number of seconds elapsed since the UTC time of January 1, 1970 0:0 0 seconds.
return value Returns the number of seconds elapsed.
Example /* Take time (in seconds) using localtime ()
Convert to a struct TM and then use Mktine () to convert the struct TM to the original number of seconds */
#include <time.h>
Main ()
{
time_t TIMEP;
Strcut TM *p;
Time (&TIMEP);
printf ("Time ():%d \ n", TIMEP);
P=localtime (&TIMEP);
TIMEP = Mktime (p);
printf ("Time ()->localtime ()->mktime ():%d\n", TIMEP);
}
Perform Time (): 974943297
Time ()->localtime ()->mktime (): 974943297
 

Settimeofday (set current time)
Related functions Time,ctime,ftime,gettimeofday
Table header File #include <sys/time.h>
#include <unistd.h>
Defining functions int settimeofday (const struct timeval *tv,const struct timezone *tz);
Function description Settimeofday () Sets the current time to the structure information referred to by TV, and the local time zone information is set to the structure referred to by TZ. Please refer to Gettimeofday () for detailed instructions. Note that only root permissions can be used to modify the time of this function.
return value The success returns 0, the failure returns 1, and the error code is stored in errno.
Error code Eperm does not call Settimeofday () by root, and does not have sufficient permissions.
EINVAL time zone or some data is incorrect and cannot be set correctly.
 

Time (get current)
Related functions Ctime,ftime,gettimeofday
Table header File #include <time.h>
Defining functions time_t time (time_t *t);
Function description This function returns the number of seconds from 0:0 to 0 seconds since the UTC time in A.D. January 1, 1970. If T is not a null pointer, this function also saves the return value to the memory referred to by the T-pointer.
return value Success returns the number of seconds, and failure returns the ((time_t)-1) value, which causes the error to exist in errno.
Example #include <time.h>
Mian ()
{
int seconds= time ((time_t*) NULL);
printf ("%d\n", seconds);
}
Perform 9.73E+08

C + + Time acquisition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.