精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。
UNIX timekeeping is an untidy area, made more confusing by national and international laws and customs. Broadly, there are two kinds of functions: one group is concerned with getting and setting system times, and the other group is concerned with converting time representations between a bewildering number of formats.
UNIX时间保持是一个邋遢的地区,国家和国际的法律和习惯使它变得更加扑朔迷离。广泛地说,有两种功能:一组是关于获取和设置系统时间,另一组是关于令人困惑的格式数字转换。
Before we start, we’ll define some terms: 在开始之前,我们将定义一些术语:
• Atime zone is a definition of the time at a particular location relative to the time at other locations. Most time zones are bound to political borders, and vary from one another in steps of one hour, although there are still a number of time zones that are offset from adjacent time zones by 30 minutes. Time zones tend to have three-letter abbreviations (TLAs) such as PST (Pacific Standard Time), EDT (Eastern Daylight Time), BST (British Summer Time), AET (Australian Eastern Time), MEZ (Mitteleuropäische Zeit). As the example shows, you should not rely on the combination ST to represent Standard Time.
时区是时间在特定位置相对于在其它位置的时间定义。大多数时区都绑定到政治边界,彼此之间逐步相差一个小时,,但是仍有一个时区偏移相邻时区30分钟。时区往往有三个字母的缩写(tla)如PST(太平洋标准时间),EDT(东部时间),BST(英国夏令时),AET(澳洲东部时间),MEZ(Mitteleuropaische时间)。正如示例所示,您不应该依赖于组合ST来表示标准时间。
• UTC is the international base time zone, and has the distinction of being one of those abbreviations which nobody can expand. It means Universal Coordinated Time, despite the initials. It obviously doesn’t stand for the French Temps Universel Coordonné either. It corresponds very closely, but not exactly, to Greenwich Mean Time (GMT), the local time in England in the winter, and is the basis of all UNIX timestamps. The result is that for most of us, UTC is not the current local time, though it might be close enough to be confusing or far enough away to be annoying.
UTC是国际基础时区,区别是其中之一缩写,没有人可以扩大。这意味着通用协调时间,尽管刚开始如此。它显然不能代表法国协调世界时间。它密切对应格林威治标准时间(GMT),但不完全是,TMG是在英国冬天当地的时间,所有UNIX时间戳的基础。其结果是,对于大多数人来说,UTC不是当前本地时间,但它可能是最接近的。
• From the standpoint of UNIX, you can consider the Epoch to be the beginning of recorded history: it’s 00:00:00 UTC, 1 January 1970. All system internal dates are relative to the Epoch.
从UNIX的角度来看,您可以考虑时代的开始历史记录:就是UTC,1970年1月1日。所有系统内部的日期都是相对的时代。
• Daylight Savings Time is a method of making the days appear longer in summer by setting the clocks forward, usually by one hour. Thus in summer, the sun appears to set one hour later than would otherwise be the case.
夏令时是通过拨动时钟指针让这一日延长,通常是一个小时。因此在夏天,太阳似乎比世界时间晚了一个小时,否则会出现以下情况。
Even after clarifying these definitions, timekeeping remains a pain. We’ll look at the main problems in the following sections: 即使在明确这些定义之后,计时仍然是一个麻烦的事。我们来看看主要的问题在以下部分:
The time functions are not easy to use: to get them to do anything useful requires a lot of work. You’d think that UNIX would supply a primitive call upon which you could easily build, but unfortunately there isn’t any such call, and the ones that are available do not operate in an intuitively obvious way. For example, there is no standard function for returning the current time in a useful format.
时间函数不易于使用:让他们做任何有用的事都需要大量的工作。你会认为UNIX提供一个原始的方法,你很容易构建,但不幸的是没有这样的调用。并且那些可用的操作不以一种直观的方式运行。例如,一个有用的格式里没有标准函数返回当前时间。
There is no single system call that is supported across all platforms. Functions are implemented as system calls in some systems and as library functions in others. As a result, it doesn’t make sense to maintain our distinction between system calls and library functions when it comes to timekeeping. In our discussion of the individual functions, we’ll note which systems implement them as system calls and which as library calls.
没有单一的系统调用能够在所有平台上都支持。功能实现在一些系统的系统调用和库函数。因此,当涉及到计时,它维持我们的系统调用和库函数之间的区别没有意义。在我们的各个功能的讨论,我们会注意哪些系统实现这些系统调用和作为库的调用。
• The system uses the time_t format, which represents the number of seconds since the Epoch. This format is not subject to time zones or daylight savings time, but it is accurate only to one second, which is not accurate enough for many applications.
本系统采用的time_t格式,它表示自历元的秒数。这种格式不受限于时区和夏令时,但是它只能精确到一秒,这对于很多应用程序都不够准确。
• The struct timeval format is something like an extended time_t with a resolution of 1 microsecond: 日期格式结构就像延长的time_t分辨率为1微秒:
#include <sys/time.h>
struct timeval
{
long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};
It is used for a number of newer functions, such as gettimeofday and setitimer. 用于一些新的功能,比如gettimeofday,setitimer。
• Many library routines represent the calendar time as a struct tm. It is usually defined in /usr/include/time.h: 许多库例程作为struct tm代表日历时间。它通常是定义在/usr/include/time.h:
struct tm
{
int tm_sec; /* seconds after the minute [0-60] */
int tm_min; /* minutes after the hour [0-59] */
int tm_hour; /* hours since midnight [0-23] */
int tm_mday; /* day of the month [1-31] */
int tm_mon; /* months since January [0-11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday [0-6] */
int tm_yday; /* days since January 1 [0-365] */
int tm_isdst; /* Daylight Savings Time flag */
long tm_gmtoff; /* offset from UTC in seconds */
char *tm_zone; /* timezone abbreviation */
};
Unlike time_t, a struct tm does not uniquely define the time: it may be a UTC time, or it may be local time, depending on the time zone information for the system.
time_t不同,struct tm无法唯一定义:它可能是UTC时间,或者它可能是当地时间,这取决于系统的时区信息。
• Dates as a text string are frequently represented in a strange manner, for example Sat Sep 17 14:28:03 1994\n. This format includes a \n character, which is seldom needed — often you will have to chop it off again.
日期作为文本字符串经常用一种奇怪的方式,例如1994年9月17日14:28:03 \ n。这种格式包括\ n个字符,这是很少需要——通常你必须切掉了。