[英]Set system time zone in Linux
在C語言中是否有任何函數可以在Linux中設置系統時間? 我還必須設置我的系統時區。 換句話說,如果我的時區是IST,我可以將其設置為UTC嗎?
請注意, settimeofday
的tz
(時區)參數已過時 。
由環境中沒有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.