|
Time and Time Zone
Earth divided into 24 zones throughout four seasons, each time zone has its own local time.
UTC time and GMT time
We can think of Greenwich Mean Time is Time Coordinated Time (GMT = UTC), Greenwich Mean Time, and UTC time is calculated using the number of seconds.
UTC time and local time
UTC time zone difference = local time +
Eastern time zone difference is positive, west negative. In this case, the difference in time zones east eight districts denoted +0800
UTC + (+0800) = local (Beijing) time
UTC and Unix timestamp
See on your computer are from the UTC time (January 1, 1970 0:00:00) counted the number of seconds. See UTC time that is, from 1970 this point in time to play a specific time the total number of seconds. This is the number of seconds Unix timestamp.
time (to obtain the current time)
Function Description:
#include < time.h>
time_t time (time_t * t);
This function returns the UTC time from the year January 1, 1970 from 0:00:00 to now counting the number of seconds elapsed. If t is not a null pointer, then this function will return the value stored to memory t the pointer.
Returns: success, returns the number of seconds, then failed to return ((time_t) -1) value, the cause of the error is stored in errno.
Code Description:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < time.h>
int main (int argc, char ** argv)
{
int seconds = time (NULL);
printf ( "% d \ n", seconds);
return 0;
}
Results of the:
[Root @ linuxhost_CentOS unixtime] # g ++ -g -o unixtime_time unixtime_time.cpp
[Root @ linuxhost_centos unixtime] # ./unixtime_time
1445008165
gmtime (get current time and date)
Function Description:
#include
struct tm * gmtime (const time_t * timep);
gmtime () converts the time_t structure parameters timep referred to the date and time information into a real-world representation used, then the results returned by the structure tm.
Tm structure is defined as follows:
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;
};
int tm_sec represent the current number of seconds, the normal range is 0-59, but allows to 61 seconds
int tm_min represent the current score, range 0-59
When int tm_hour number since midnight, in the range of 0-23
int tm_mday few days of the current month, range 01-31
int tm_mon represent the current month, counting from January, ranging from 0-11
int tm_year counting the years from 1900 to date
int tm_wday a few days a week, from Monday to date, in the range of 0-6
int tm_yday number of days from the date of January 1 this year so far, ranging from 0-365
int tm_isdst Daylight Savings Time flag
This function returns the date and time without time zone conversion, but UTC time.
Returns: the structure tm represent the current UTC time
Code Description:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < time.h>
int main (int argc, char ** argv)
{
const char * wday [] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
struct tm * p;
time (& timep);
p = gmtime (& timep);
printf ( "curday =% d-% d-% d \ n", (1900 + p-> tm_year), (1 + p-> tm_mon), p-> tm_mday);
printf ( "curweek =% s, curtime =% d:% d:% d \ n", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec);
return 0;
}
Results Explanation:
[Root @ linuxhost_centos unixtime] # g ++ -g -o unixtime_gmtime unixtime_gmtime.cpp
[Root @ linuxhost_centos unixtime] # ./unixtime_gmtime
curday = 2015-10-16
curweek = Fri, curtime = 15:12:12
[Root @ linuxhost_centos unixtime] # date -u
Fri Oct 16 15:12:13 UTC 2015
[Root @ linuxhost_centos unixtime] # date
Fri Oct 16 23:12:16 CST 2015
[Root @ linuxhost_centos unixtime] # date -R # here to print out the time zone information, Beijing East eight districts
Fri, 16 Oct 2015 23:12:18 +0800
You can see the date and time gmtime return without conversion outdated zone, and date printed here exactly eight hours difference (China time zone).
ctime (time and date will be represented in string format)
Function Description:
#include
char * ctime (const time_t * timep);
ctime () converts the time_t structure parameters timep referred to the date and time information into a real-world representation method used and the result is a string form returned. If more call the relevant date and time functions, this string may be damaged.
Code Description:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < time.h>
int main (int argc, char ** argv)
{
time_t timep;
time (& timep);
printf ( "% s", ctime (& timep));
return 0;
}
Results Explanation:
[Root @ linuxhost_centos unixtime] # g ++ -g -o unixtime_ctime unixtime_ctime.cpp
[Root @ linuxhost_centos unixtime] # ./unixtime_ctime
Fri Oct 16 23:14:33 2015
[Root @ linuxhost_centos unixtime] # date
Fri Oct 16 23:14:34 CST 2015
asctime (showing the time and date in string format)
Function Description:
#include
char * asctime (const struct tm * timeptr);
asctime () converts the structure tm timeptr parameters referred to in the date and time information into a real-world representation method used and the result is a string form returned. If more call the relevant date and time functions, this string may be damaged. This function ctime different parameters that are passed in a different structure.
Code Description:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < time.h>
int main (int argc, char ** argv)
{
time_t timep;
time (& timep);
printf ( "% s", asctime (gmtime (& timep)));
}
Results Explanation:
[Root @ linuxhost_centos unixtime] # g ++ -g -o unixtime_asctime unixtime_asctime.cpp
[Root @ linuxhost_centos unixtime] # ./unixtime_asctime
Fri Oct 16 15:15:54 2015
[Root @ linuxhost_centos unixtime] # date
Fri Oct 16 23:15:55 CST 2015
[Root @ linuxhost_centos unixtime] # date -u
Fri Oct 16 15:15:57 UTC 2015
[Root @ linuxhost_centos unixtime] # date -R
Fri, 16 Oct 2015 23:16:01 +0800
Note the struct tm time structure by gmtime return, so there is no time zone conversion.
gettimeofday (get the current time)
Function Description:
#include < sys / time.h>
#include < unistd.h>
int gettimeofday (struct timeval * tv, struct timezone * tz)
gettimeofday () will have the structure of the present time tv within the meaning of return, local time zone information is placed in the structure referred to in tz.
timeval structure is defined as:
struct timeval {
long tv_sec; / * seconds * /
long tv_usec; / * microseconds * /
};
timezone structure is defined as:
struct timezone {
int tz_minuteswest; / * time difference between Greenwich and the number of minutes * /
/ * Daylight Savings Time status * /; int tz_dsttime
};
Both structures are defined in the state represented by the following /usr/include/sys/time.h,tz_dsttime
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 / * United Kingdom * /
DST_RUM / * Romania * /
DST_TUR / * * Turkey /
(After 1986) DST_AUSTALT / * * Australia /
Returns: return 0 if successful, failure returns -1, an error code is stored in errno.
Code Description:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < sys / time.h>
int main (int argc, char ** argv)
{
struct timeval tv;
struct timezone tz;
gettimeofday (& tv, & tz);
printf ( "tv_sec =% d, tv_usec =% d, tz_minuteswest =% d, tz_dsttime =% d \ n",
tv.tv_sec, tv.tv_usec, tz.tz_minuteswest, tz.tz_dsttime);
return 0;
}
Results Explanation:
[Root @ VM_174_centos unixtime] # g ++ -g -o unixtime_gettimeofday unixtime_gettimeofday.cpp
[Root @ VM_174_centos unixtime] # ./unixtime_gettimeofday
tv_sec = 1445008619, tv_usec = 699804, tz_minuteswest = -480, tz_dsttime = 0
[Root @ linuxhost_centos unixtime] # date
Fri Oct 16 23:17:00 CST 2015
[Root @ linuxhost_centos unixtime] # date -u
Fri Oct 16 15:17:02 UTC 2015
Here the time zone difference is -480, that is to say more than we GMT Ming (China time zone) at 8:00 hours.
localtime (get local current time and date)
Function Description:
#include
struct tm * localtime (const time_t * timep);
localtime () converts the time_t structure parameters timep referred to the date and time information into a real-world representation used, then the results returned by the structure tm. Please refer to the definition of the structure tm gmtime (). This function returns the date and time have been converted into the local time zone.
Code Description:
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < time.h>
int main (int argc, char ** argv)
{
const char * wday [] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
struct tm * p;
time (& timep);
p = localtime (& timep);
printf ( "curday =% d-% d-% d \ n", (1900 + p-> tm_year), (1 + p-> tm_mon), p-> tm_mday);
printf ( "curweek =% s, curtime =% d:% d:% d \ n", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec);
return 0;
}
Results Explanation:
[Root @ linuxhost_centos unixtime] # g ++ -g -o unixtime_localtime unixtime_localtime.cpp
[Root @ linuxhost_centos unixtime] # ./unixtime_localtime
curday = 2015-10-16
curweek = Fri, curtime = 23:23:37
[Root @ linuxhost_centos unixtime] # ./unixtime_gmtime
curday = 2015-10-16
curweek = Fri, curtime = 15:23:37
The results presented here with gmtime compare the results, it can be seen, gmtime given is GMT standard time, localtime given is based on the local time zone converted (this is Beijing, the East eight districts, + 0800).
mktime (structural data into time elapsed number)
Function Description:
time_t mktime (strcut tm * timeptr);
mktime () is used to convert data parameters timeptr tm structure referred to as AD at 0:00:00 on January 1, 1970 UTC time since counting the number of seconds elapsed.
Returns: Returns the number of seconds elapsed.
Code Description:
/ *
* Use time () to obtain the time (in seconds), use localtime ()
* Convert struct tm reuse mktine () will be converted into the original struct tm Seconds
* /
#include < stdio.h>
#include < stdlib.h>
#include < unistd.h>
#include < time.h>
int main (int argc, char ** argv)
{
time_t timep;
struct tm * p;
time (& timep);
printf ( "time () =% d \ n", timep);
p = localtime (& timep);
timep = mktime (p);
printf ( "time () -> localtime () -> mktime ():% d \ n", timep);
return 0;
}
Results Explanation:
[Root @ linuxhost_centos unixtime] # g ++ -g -o unixtime_mktime unixtime_mktime.cpp
[Root @ linuxhost_centos unixtime] # ./unixtime_mktime
time () = 1445010682
time () -> localtime () -> mktime (): 1445010682
settimeofday (currently set time)
Function Description:
#include < sys / time.h>
#include < unistd.h>
int settimeofday (const struct timeval * tv, const struct timezone * tz);
Structure settimeofday () will be set to the current time structure information within the meaning of the tv, the local time zone information is set to tz referred. Please refer to the detailed description gettimeofday (). Note that only root privileges to use this function to modify the time.
Returns: return 0 if successful, failure returns -1, an error code is stored in errno.
EPERM not call settimeofday by the root authority (), insufficient privileges.
EINVAL or a time zone data is incorrect, you can not set the time correctly. |
|
|
|