如何在UNIX上使用C日期和时间函数?

时间:2022-12-17 15:04:05

Jon Skeet spoke of the complexity of programming dates and times at the 2009 DevDays in London.

Jon Skeet谈到了2009年伦敦DevDays编程日期和时间的复杂性。

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?

您能否介绍一下UNIX上的ANSI C日期/时间函数,并指出在使用日期和时间时我还应该考虑的一些更深层次的问题?

1 个解决方案

#1


16  

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
  • 日历时间(a.k.a.simpletime) - 时间作为绝对值,通常是一些基准时间,通常称为协调世界时
  • 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.
  • localtime(a.k.a.细分时间) - 由年,月,日等组成的日历时间,考虑到当地时区(包括夏令时)(如果适用)。

Data types

The date/time functions and types are declared in the time.h header file.

日期/时间函数和类型在time.h头文件中声明。

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

    作为使用time_t算术类型的数字 - 将日历时间存储为自UNIX纪元1970年1月1日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

    使用结构timeval - 将日历时间存储为自UNIX纪元1970年1月1日00:00:00以来经过的秒数和纳秒数

  • using the structure tm to store localtime, it contains attributes such as the following:

    使用结构tm存储localtime,它包含如下属性:

    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.

上面的tm_isdst属性用于指示夏令时(DST)。如果值为正,则为DST,如果值为0,则不是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.

在上面的程序中,函数时间读取UNIX系统时间,从1970年1月1日00:00:00(UNIX纪元)中减去它,并以秒为单位返回其结果。

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.

在上面的程序中,函数localtime将从UNIX纪元到耗尽时间的经过时间(以秒为单位)转换。 localtime读取UNIX环境TZ(通过调用tzset函数)返回相对于时区的时间并设置tm_isdst属性。

A typical setting of the TZ variable in UNIX (using bash) would be as follows:

UNIX中TZ变量的典型设置(使用bash)如下:

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.

在上面的程序中,函数strftime提供了日期的专门格式。

Other issues to consider

#1


16  

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
  • 日历时间(a.k.a.simpletime) - 时间作为绝对值,通常是一些基准时间,通常称为协调世界时
  • 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.
  • localtime(a.k.a.细分时间) - 由年,月,日等组成的日历时间,考虑到当地时区(包括夏令时)(如果适用)。

Data types

The date/time functions and types are declared in the time.h header file.

日期/时间函数和类型在time.h头文件中声明。

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

    作为使用time_t算术类型的数字 - 将日历时间存储为自UNIX纪元1970年1月1日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

    使用结构timeval - 将日历时间存储为自UNIX纪元1970年1月1日00:00:00以来经过的秒数和纳秒数

  • using the structure tm to store localtime, it contains attributes such as the following:

    使用结构tm存储localtime,它包含如下属性:

    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.

上面的tm_isdst属性用于指示夏令时(DST)。如果值为正,则为DST,如果值为0,则不是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.

在上面的程序中,函数时间读取UNIX系统时间,从1970年1月1日00:00:00(UNIX纪元)中减去它,并以秒为单位返回其结果。

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.

在上面的程序中,函数localtime将从UNIX纪元到耗尽时间的经过时间(以秒为单位)转换。 localtime读取UNIX环境TZ(通过调用tzset函数)返回相对于时区的时间并设置tm_isdst属性。

A typical setting of the TZ variable in UNIX (using bash) would be as follows:

UNIX中TZ变量的典型设置(使用bash)如下:

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.

在上面的程序中,函数strftime提供了日期的专门格式。

Other issues to consider