【问题标题】:Offset from GMT standard time与 GMT 标准时间的偏移
【发布时间】:2010-12-24 16:38:41
【问题描述】:

Unix 是否在内部存储机器相对于 GMT 的偏移量? 例如:印度标准时间是 GMT + 5:30。这个 5:30 是否存储在某个地方?

我需要这个才能在下面的脚本中使用它

if[[ off is "some value"]]
then
some statements
fi

【问题讨论】:

  • 是的...如果你问一个具体的问题,你甚至可以学习如何用它做一些有用的事情! :)

标签: unix shell gmt


【解决方案1】:

传统上,在 UNIX 中,内核以独立于时区的形式保存当前时间,这是它向应用程序报告的内容。

应用程序查询环境变量和/或用户配置(对于不同的用户或不同的会话对于一个用户可能不同)以确定报告时间的时区。为此,磁盘上保存了一些表系统知道的所有时区的偏移量(这些表需要不断更新以适应夏令时算法的政治变化)。

【讨论】:

    【解决方案2】:

    以下程序在 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 脚本中调用它。

    【讨论】:

      【解决方案3】:

      内核在内部保留 GMT 时间,并在被要求提供本地时间时使用时区信息计算偏移量。这样,如果需要更改时区,内部时钟就不需要更改。

      【讨论】:

        【解决方案4】:

        在内核或驱动程序中,没有。

        通常,它存储在一个名为 /etc/localtime 的文件中。该文件通常是指向其他文件的链接,其中包含(以压缩形式)将 GMT 转换为本地时间的所有“规则”,包括夏令时开始和结束的时间、与 GMT 的偏移量等。

        【讨论】:

        • 它可以被TZ 环境变量覆盖。
        猜你喜欢
        • 2019-01-03
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        • 2021-05-10
        • 1970-01-01
        • 2010-11-04
        • 2010-11-06
        • 2012-05-01
        相关资源
        最近更新 更多