参考链接:(大神讲的真好)
https://www.cnblogs.com/linuxbug/p/4887006.html
下面涉及到的函数全部都可以在:
man 3 ctime
中找到。
下面使用到的测试源码:
git clone https://github.com/rtczza/Linux_Time.git
头文件:
#include
函数原型:
time_t time(time_t *t);
函数描述:
returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
返回值:
On success, the value of time in seconds since the Epoch is returned. On error, ((time_t) -1) is returned, and errno is set appropriately.
获取当前时间 :从1970年1月1日00:00:00 到现在经过的秒数。
测试源码:
#include
#include
int main(int argc, char** argv)
{
time_t t= time(NULL);
printf("time :%ld\n",(long)t);
return 0;
}
头文件:
#include
函数原型:
struct tm *gmtime(const time_t *timep);
结构体:
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
结构体描述:
The members of the tm structure are:
tm_sec The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.
tm_min The number of minutes after the hour, in the range 0 to 59.
tm_hour The number of hours past midnight, in the range 0 to 23.
tm_mday The day of the month, in the range 1 to 31.
tm_mon The number of months since January, in the range 0 to 11.
tm_year The number of years since 1900.
tm_wday The number of days since Sunday, in the range 0 to 6.
tm_yday The number of days since January 1, in the range 0 to 365.
tm_isdst A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the information is not available.
获取当前时间,并转换为结构体中规定的格式。
其中需要注意的是:
1、tm_hour 表示的是UTC的时间,没有经过时区转换。
2、tm_mon 月份范围是:0-11. 所以打印时需要加一。
3、tm_year 年份是从1900年开始的年数,所以打印时需要加1900。
测试源码:
#include
#include
int main()
{
time_t t = time(NULL);
printf("time :%ld \n", (long)t);
struct tm *gt;
gt = gmtime(&t);
printf("gt->tm_sec \t:%d \n", gt->tm_sec);
printf("gt->tm_min \t:%d \n", gt->tm_min);
printf("gt->tm_hour \t:%d \n", gt->tm_hour); //是UTC时间,未经时区转换
printf("gt->tm_mday \t:%d \n", gt->tm_mday);
printf("gt->tm_mon+1 \t:%d \n", gt->tm_mon+1); //范围是0-11
printf("gt->tm_year+1900 :%d \n", gt->tm_year+1900); //从1900年开始的年数
printf("gt->tm_wday \t:%d \n", gt->tm_wday);
printf("gt->tm_yday \t:%d \n", gt->tm_yday);
printf("gt->tm_isdst \t:%d \n", gt->tm_isdst);
return 0;
}
头文件:
#include
函数原型:
char *ctime(const time_t *timep);
函数描述:
The call ctime(t) is equivalent to asctime(localtime(t)). It converts the calendar time t into a null-terminated string of the form
"Wed Jun 30 21:49:08 1993\n"
返回值:
成功返回时间字符串,失败返回NULL.
调用 ctime(t)
函数与 调用 asctime(localtime(t))
(下面介绍asctime
函数)是等价的。
ctime
函数是把time t
时间,转换为以回车结尾的字符串。
测试源码:
#include
#include
int main(int argc, char** argv)
{
time_t t = time(NULL);
char *ct = NULL;
ct = ctime(&t);
printf("ctime :%s", ct);
return 0;
}
头文件:
#include
函数原型:
char *asctime(const struct tm *tm);
函数描述:
The asctime() function converts the broken-down time value tm into a null-terminated string with the same format as ctime(). The return value points to a statically allocated string which might be overwritten by subsequent
calls to any of the date and time functions. The asctime_r() function does the same, but stores the string in a user-supplied buffer which should have room for at least 26 bytes.
返回值:
成功返回时间字符串,失败返回NULL.
asctime
函数是把struct tm
格式的时间,转换为以回车结尾的字符串。
测试源码:
#include
#include
int main(int argc, char** argv)
{
if(2 != argc)
{
printf("Not Found Param ! \n");
return -1;
}
int param = atoi(argv[1]);
time_t t= time(NULL); //获取当前时间
struct tm *gt;
if(1 == param)
gt = gmtime(&t); //把当前时间转换为struct tm 格式 :获取的是UTC时间:没有经过时区转换
else
gt = localtime(&t); //把当前时间转换为struct tm 格式 :获取的是本地时间:经过时区转换之后的
char *asct;
asct = asctime(gt); //把当前时间转换为字符串格式
printf("asctime :%s",asct);
return 0;
}
上面的测试程序中:
1、先使用time
函数获取本地时间,得到time_t
格式的时间。
2、然后把time_t
格式的时间作为参数,通过gmtime
函数或者localtime
函数,得到struct tm
格式的时间。
3、然后使用asctime
函数,把struct tm
格式的时间,转换为字符串。
参数为1,表示使用UTC时间:显示的上午07点钟。
参数不为1,表示使用本地时间:显示的是下午15点钟。
asctime
函数与ctime
函数返回的字符串格式是相同的。
区别在于输入的格式不同:
asctime
函数:输入struct tm
格式的时间。
ctime
函数:输入time_t
格式的时间。
头文件:
#include
函数原型:
struct tm *localtime(const time_t *timep);
函数描述:
The localtime() function converts the calendar time timep to broken-down time representation, expressed relative to the user's specified timezone. The function acts as if it called tzset(3) and sets the external variables
tzname with information about the current timezone, timezone with the difference between Coordinated Universal Time (UTC) and local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply
during some part of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The localtime_r() function does the same, but stores
the data in a user-supplied struct. It need not set tzname, timezone, and daylight.
获取当前时间,并转换为结构体中规定的格式。
其中需要注意的是:
1、tm_hour 表示的是本地时间,经过时区转换。
2、tm_mon 月份范围是:0-11. 所以打印时需要加一。
3、tm_year 年份是从1900年开始的年数,所以打印时需要加1900。
测试源码:
#include
#include
int main()
{
time_t t = time(NULL);
printf("time :%ld \n", (long)t);
struct tm *gt;
gt = localtime(&t);
printf("gt->tm_sec \t:%d \n", gt->tm_sec);
printf("gt->tm_min \t:%d \n", gt->tm_min);
printf("gt->tm_hour \t:%d \n", gt->tm_hour); //是本地时间,经过时区转换后的
printf("gt->tm_mday \t:%d \n", gt->tm_mday);
printf("gt->tm_mon+1 \t:%d \n", gt->tm_mon+1); //范围是0-11
printf("gt->tm_year+1900 :%d \n", gt->tm_year+1900); //从1900年开始的年数
printf("gt->tm_wday \t:%d \n", gt->tm_wday);
printf("gt->tm_yday \t:%d \n", gt->tm_yday);
printf("gt->tm_isdst \t:%d \n", gt->tm_isdst);
return 0;
}
其中gmtime与localtime函数的区别是:
gmtime:获取的是UTC时间,没有进过时区转换。
localtime:获取的是本地时间,进过了时区转换。
头文件:
#include
函数原型:
time_t mktime(struct tm *tm);
函数描述:
The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation. The function ignores the values supplied by the caller in the tm_wday and tm_yday fields. The value speci‐
fied in the tm_isdst field informs mktime() whether or not daylight saving time (DST) is in effect for the time supplied in the tm structure: a positive value means DST is in effect; zero means that DST is not in effect; and a
negative value means that mktime() should (use timezone information and system databases to) attempt to determine whether DST is in effect at the specified time.
The mktime() function modifies the fields of the tm structure as follows: tm_wday and tm_yday are set to values determined from the contents of the other fields; if structure members are outside their valid interval, they will
be normalized (so that, for example, 40 October is changed into 9 November); tm_isdst is set (regardless of its initial value) to a positive value or to 0, respectively, to indicate whether DST is or is not in effect at the
specified time. Calling mktime() also sets the external variable tzname with information about the current timezone.
返回值:
成功返回time_t格式的时间,失败返回(time_t)-1。
mktime
函数的作用是,把struct tm
格式的时间,转换为time_t
格式的时间
测试源码:
#include
#include
void Print(struct tm *gt)
{
printf("gt->tm_sec \t:%d \n", gt->tm_sec);
printf("gt->tm_min \t:%d \n", gt->tm_min);
printf("gt->tm_hour \t:%d \n", gt->tm_hour);
printf("gt->tm_mday \t:%d \n", gt->tm_mday);
printf("gt->tm_mon+1 \t:%d \n", gt->tm_mon+1);
printf("gt->tm_year+1900 :%d \n", gt->tm_year+1900);
printf("gt->tm_wday \t:%d \n", gt->tm_wday);
printf("gt->tm_yday \t:%d \n", gt->tm_yday);
printf("gt->tm_isdst \t:%d \n", gt->tm_isdst);
}
int main(int argc, char **argv)
{
if(2 != argc)
{
printf("Not Found Param ! \n");
return -1;
}
int param = atoi(argv[1]); //输入参数
time_t t = time(NULL); //获取time_t 格式的时间 (目前时间)
printf("time :%ld \n", (long)t);
struct tm *gt; //把 time_t 格式的时间,转换为 struct tm 格式的时间
if(1 == param)
gt = gmtime(&t);
else
gt = localtime(&t);
Print(gt);
time_t t2;
t2 = mktime(gt); //把 struct tm 格式的时间,转换为time_t 格式的时间
printf("time2 :%ld \n", (long)t2);
return 0;
}
需要注意的是,给mktime
函数输入的struct tm
参数为本地时间(即 经过时区转换之后的时间)。
从上面的截图也可以看出:
输入参数不为1时:
使用
localtime
转换后的本地时间。再经过mktime
函数转换后,前后两个时间戳是相同的。
输入参数为1时:
使用
gmtime
转换后的UTC时间。再经过mktime
函数转换后,前后两个时间戳是不同的。