在UNIX上如何使用C日期和时间函数? [英] How do I use the C date and time functions on UNIX?
问题描述
p>
术语
日期/时间可以有两种格式: / p>
- 日历时间(aka simpletime) - 时间作为绝对值,通常是由于某些基准时间(通常称为协调世界时)
- 本地时间(又称分解时间) - 由年,月,日等组成的日历时间,考虑到本地时区,包括夏令时(如适用)。
数据类型
日期/时间函数和类型在当时声明。 h头文件。
时间可以存储为一个整数或一个结构的实例:
-
使用 time_t 算术类型 - 将日历时间存储为自UNIX时代以来经过的秒数1970年01月01日00:00:00
-
使用结构 timeval - 将日历时间存储为自UNIX时代以来的秒数和纳秒数1970年01月01日00:00:00
-
使用结构 tm 存储本地时间,它包含以下属性:
tm_hour
tm_min
tm_isdst
上面的tm_isdst属性用于指示夏令时(DST)。如果值为正值,则为DST,如果值为0,则不是DST。
打印当前协调世界时间的程序
#include< stdio.h>
#include< time.h>
int main(int argc,char * argv [])
{
time_t now;
now = time(NULL);
printf(从1970年1月1日00:00:00开始的%ld秒,(长))
return 0;
}
在上面的程序中,时间读取UNIX系统时间,从1970年1月1日00:00:00减去(UNIX时代),并在几秒钟内返回结果。
打印当前本地时间的程序
#include< stdio.h>
#include< time.h>
int main(int argc,char * argv [])
{
time_t now;
struct tm * lcltime;
now = time(NULL);
lcltime = localtime(& now);
printf(时间是%d:%d\\\
,lcltime-> tm_hour,lcltime-> tm_min);
return 0;
}
在上面的程序中, localtime 将从UNIX时期开始的经过时间(秒)转换为分解时间。本地时间读取UNIX环境TZ(通过调用tzset函数)返回相对于时区的时间并设置tm_isdst属性。
UNIX中的TZ变量(使用bash)将如下所示:
export TZ = GMT
或
export TZ = US /东方
打印当前格式格林威治标准时间的程序
#include< stdio.h>
#include< time.h>
int main(int argc,char * argv [])
{
time_t now;
struct tm * gmt;
char formatted_gmt [50];
now = time(NULL);
gmt = gmtime(& now);
strftime(formatted_gmt,sizeof(formatted_gmt),%I:%M%p,gmt);
printf(时间是%s\\\
,formatted_gmt);
return 0;
}
在上面的程序中, strftime 提供专门的日期格式。
要考虑的其他问题
Jon Skeet spoke of the complexity of programming dates and times at the 2009 DevDays in London.
Can you give me an introduction to the ANSI C date/time functions on UNIX and indicate some of the deeper issues I should also consider when using dates and times?
Terminology
A date/time can be in two formats:
- calendar time (a.k.a. simpletime) – time as an absolute value typically since some base time, often referred to as the Coordinated Universal Time
- localtime (a.k.a. broken-down time) – a calendar time made up of components of year, month, day etc. which takes into account the local time zone including Daylight Saving Time if applicable.
Data types
The date/time functions and types are declared in the time.h header file.
Time can be stored as a whole number or as an instance of a structure:
as a number using the time_t arithmetic type – to store calendar time as the number of seconds elapsed since the UNIX epoch January 1, 1970 00:00:00
using the structure timeval – to store calendar time as the number of seconds and nanoseconds elapsed since the UNIX epoch January 1, 1970 00:00:00
using the structure tm to store localtime, it contains attributes such as the following:
tm_hour tm_min tm_isdst
The tm_isdst attribute above is used to indicate Daylight Saving Time (DST). If the value is positive it is DST, if the value is 0 it is not DST.
Program to print the current Coordinated Universal Time
#include <stdio.h>
#include <time.h>
int main ( int argc, char *argv[] )
{
time_t now;
now = time ( NULL );
printf ( "It’s %ld seconds since January 1, 1970 00:00:00", (long) now );
return 0;
}
In the program above the function time reads the UNIX system time, subtracts that from January 1, 1970 00:00:00 (the UNIX epoch) and returns its result in seconds.
Program to print the current local time
#include <stdio.h>
#include <time.h>
int main ( int argc, char *argv[] )
{
time_t now;
struct tm *lcltime;
now = time ( NULL );
lcltime = localtime ( &now );
printf ( "The time is %d:%d\n", lcltime->tm_hour, lcltime->tm_min );
return 0;
}
In the program above the function localtime converts the elapsed time in seconds from the UNIX epoch into the broken-down time. localtime reads the UNIX environment TZ (through a call to the tzset function) to return the time relative to the timezone and to set the tm_isdst attribute.
A typical setting of the TZ variable in UNIX (using bash) would be as follows:
export TZ=GMT
or
export TZ=US/Eastern
Program to print the current formatted Greenwich Mean Time
#include <stdio.h>
#include <time.h>
int main ( int argc, char *argv[] )
{
time_t now;
struct tm *gmt;
char formatted_gmt [50];
now = time ( NULL );
gmt = gmtime ( &now );
strftime ( formatted_gmt, sizeof(formatted_gmt), "%I:%M %p", gmt );
printf ( "The time is %s\n", formatted_gmt );
return 0;
}
In the program above the function strftime provides specialised formatting of dates.
Other issues to consider
这篇关于在UNIX上如何使用C日期和时间函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!