簡體   English   中英

在Linux中設置系統時區

[英]Set system time zone in Linux

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

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

請注意, settimeofdaytz (時區)參數已過時

由環境中沒有TZ變量的進程使用的默認時區由/etc/localtime的內容決定。 /usr/share/zoneinfo找到所需的時區,然后復制或符號鏈接。

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

有一些互動工具可以幫助您選擇時區,但它們因分布而異(例如Debian的dpkg-reconfigure tzdata

您可以使用此設置時區

setenv("TZ", "PST8PDT", 1);
tzset();
#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM