我如何使用C日期和时间函数对UNIX? [英] How do I use the C date and time functions on UNIX?

查看:134
本文介绍了我如何使用C日期和时间函数对UNIX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乔恩斯基特谈到编程日期和时间的复杂性,在伦敦举行的2009年DevDays。

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

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

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:


  • 日历时间(又名simpletime) - 时间的绝对值一般,因为一些基本的时间,通常被称为协调世界时

  • 本地时间(也称为分解时间) - 由年,月,日等,其中考虑到包括夏令时(如果适用)本地时区的组件的日历时间

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

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:


  • 作为使用 time_t的算术类型的一些 - 以日历时间保存为自UNIX纪元经过1970年1月1日的秒数00:00:00

  • 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

使用结构的timeval - 存储日历时间从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

使用结构 TM 存储本地时间,它包含了属性,如以下

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

tm_hour  
tm_min  
tm_isdst  


在上面的tm_isdst属性是用来表示夏令时(DST)。如果该值为正它是DST,如果该值为0它没有DST

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.

#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;
}

在功能时间上面的程序读取UNIX系统时间,减去从1970年00:00:00(UNIX新纪元),并在几秒钟内返回其结果。1月1日,

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.

#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;
}

在功能本地时间上面的程序转换的秒数时间从UNIX纪元到分解时间。本地时间读取UNIX环境TZ(通过向tzset函数的调用)相对于时间以返回到时区并设置tm_isdst的属性

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.

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

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

export TZ=GMT

export TZ=US/Eastern

程序打印当前格式化格林威治时间

#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;
}

在功能的strftime 上面的程序提供了专门的格式日期。

In the program above the function strftime provides specialised formatting of dates.

  • Leap seconds
  • What should we do to prepare for 2038?

这篇关于我如何使用C日期和时间函数对UNIX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆