2013-06-20 64 views
3

在Linux中是否有任何C語言函數來設置系統時間? 我還必須設置我的系統時區。 換句話說,如果我的時區是IST,我可以將它設置爲UTC嗎?在Linux中設置系統時區

+0

看看設置時區'hwclock'源代碼。 – ymn

+0

[可以在調用strftime之前設置時區?](http://stackoverflow.com/questions/1620188/how-can-i-set-the-time-zone-before-calling-strftime) – devnull

回答

0
#include <sys/time.h> 
#include <time.h> 

int main() 
{ 
    time_t theTimeInSeconds = time(0); 

    theTimeInSeconds -= (5.5f * 60 * 60); // sub (5.5 * 60 mins * 60 secs) from current time 

    struct timeval tv = { theTimeInSeconds, 0 }; 

    settimeofday(&tv, 0); 

    return 0; 
} 
2

使用stime來設置時間,而tzset來設置時區。

請注意,到settimeofdaytz(時區)參數是陳舊

1

在其環境中沒有TZ變量的進程使用的默認時區由/etc/localtime的內容決定。在/usr/share/zoneinfo中找到您想要的時區並複製或符號鏈接。

rm /etc/localtime 
ln -s /usr/share/zoneinfo/Etc/GMT /etc/localtime 

有一些互動工具來幫助你選擇一個時區,但他們的分佈變化(例如Debian的dpkg-reconfigure tzdata

0

您可以使用此

setenv("TZ", "PST8PDT", 1); 
tzset(); 
相關問題