与 GMT 标准时间的偏移

发布于 2024-08-13 18:30:01 字数 171 浏览 12 评论 0原文

Unix 内部是否存储机器与 GMT 的偏移量? 例如:印度标准时间是 GMT + 5:30。这个 5:30 存储在哪里吗?

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

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

Does Unix store the offset of the machine from GMT internally?
like for eg:india standard time is GMT + 5:30.is this 5:30 stored some where?

i need this to use it in a script like below

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

路还长,别太狂 2024-08-20 18:30:01

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

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

Traditionally in UNIX, the kernel keeps the current time in a timezone-independent form, which is what it reports to applications.

Applications consult environment variables and/or user configuration (which can be different for different users or different sessions for the one user) to determine which timezone to report the time in. For this purpose, there are tables kept on disk which hold the offsets of all timezones that the system knows about (these tables need to be continuously updated for political changes to daylight saving algorithms).

佞臣 2024-08-20 18:30:01

以下程序在 EDT 中为我打印“-04:00”,并在我将 TZ 设置为“亚洲/加尔各答”时打印“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 脚本中调用它。

The following program prints '-04:00' for me in EDT and prints '04:30' when I set TZ to 'Asia/Kolkata':

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

Feel free to use/change whatever you want and then call it from your shell script.

风流物 2024-08-20 18:30:01

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

The kernel keeps GMT time internally, and when asked for the local time calculates the offset using the timezone information. This way, if a timezone change is required, internally, the clock doesn't need change.

川水往事 2024-08-20 18:30:01

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

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

In the kernel or a driver, no.

Usually, it's stored in a file called /etc/localtime. That file is often a link to a file elsewhere that contains (in compressed form) all the "rules" for converting GMT to the local time, including when daylight saving time begins and ends, the offset from GMT, and so forth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文