简体   繁体   中英

Set system time zone in Linux

Are there any functions in C language to set system time in Linux? I have to set my system time zone also. I other words, if my timezone is IST, can I set it to UTC?

Use stime to set the time,and tzset to set the timezone.

Note that the tz (timezone) parameter to settimeofday is obsolete .

The default time zone, used by processes that don't have a TZ variable in their environment, is decided by the contents of /etc/localtime . Find the time zone you want in /usr/share/zoneinfo and copy or symlink it.

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

There are some interactive tools to help you pick a time zone, but they vary by distribution (eg Debian's dpkg-reconfigure tzdata )

You can set the timezone by using this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM