Unix 是否在内部存储机器与 GMT 的偏移量?例如:印度标准时间是 GMT + 5:30。这个 5:30 是否存储在某个地方?
我需要这个才能在下面的脚本中使用它
if[[ off is "some value"]]
then
some statements
fi
传统上,在 UNIX 中,内核以独立于时区的形式保存当前时间,这是它向应用程序报告的内容。
应用程序咨询环境变量和/或用户配置(对于不同的用户或不同的会话对于一个用户可能不同)以确定报告时间的时区。为此,磁盘上保存了一些表来保存偏移量系统知道的所有时区(这些表需要不断更新以适应夏令时算法的政治变化)。
内核在内部保留 GMT 时间,并在被要求提供本地时间时使用时区信息计算偏移量。这样,如果需要在内部更改时区,则时钟不需要更改。
在内核或驱动程序中,没有。
通常,它存储在一个名为 /etc/localtime 的文件中。该文件通常是指向其他文件的链接,该文件包含(以压缩形式)将 GMT 转换为本地时间的所有“规则”,包括夏令时开始和结束的时间、与 GMT 的偏移量等。
以下程序在 EDT 中为我打印“-04:00”,并在我将 TZ 设置为“Asia/Kolkata”时打印“04:30”:
#include <stdio.h>
#include <time.h>
int
main ()
{
int hours;
int minutes;
int negative_sign = 1;
tzset ();
// printf ("tzname: %s tzname[1]: %s\n", tzname [0], tzname [1]);
// printf ("DST: %d\n", daylight); /* 0 when no DST */
// printf ("timezone: %ld\n", timezone);
/* 'timezone' is the number of seconds west of GMT */
/* It is negative for tzs east of GMT */
if (timezone <= 0) {
timezone = -timezone;
negative_sign = 0;
}
if (daylight) {
timezone -= 3600; /* substract 1h when DST is active */
if (timezone <= 0) {
timezone = -timezone;
negative_sign = 0;
}
}
timezone /= 60; /* convert to minutes */
hours = timezone / 60;
minutes = timezone % 60;
printf ("%s%02d:%02d\n", (negative_sign ? "-" : ""), hours, minutes);
return 0;
}
随意使用/更改您想要的任何内容,然后从您的 shell 脚本中调用它。