2009-12-09 95 views
1

Unix在內部是否存儲GMT時間偏移量? 喜歡例如:印度標準時間是格林尼治標準時間+5:30.是這5:30存儲一些地方?格林威治標準時間偏移

我需要這樣使用它在腳本中像下面

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

是的......如果你問一個*特定的問題,你甚至可以學習如何做一些有用的事情! :) – hobbs 2009-12-09 03:38:19

回答

0

下面的程序打印「-04:00」我在美國東部時間並打印'04:30' 當我設置TZ爲‘亞洲/加爾各答’:

#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腳本中調用它。

0

內核保持GMT時間內,當問及對本地時間計算使用的時區信息的偏移。這樣,如果需要更改時區,則內部不需要更改時鐘。

1

傳統上,在UNIX中,內核保持當前時間爲獨立於時區的形式,這是它嚮應用程序報告的內容。

應用程序查詢環境變量和/或用戶配置(對於不同的用戶或一個用戶的不同會話可以是不同的),以確定哪個時區報告時間。爲此,保存系統知道的所有時區的偏移量(這些表格需要不斷更新,才能對夏時制算法進行政治​​更改)。

0

在內核或驅動程序中,沒有。

通常,它存儲在一個名爲/ etc/localtime的文件中。該文件通常是指向其他地方的文件的鏈接,該文件包含用於將GMT轉換爲本地時間的所有「規則」(包括壓縮形式),包括夏令時開始和結束時,與GMT的偏移量等等。

+1

它可以被'TZ'環境變量覆蓋。 – ephemient 2009-12-09 03:52:46

相關問題