2011年4月5日星期二

Get Local Time Zone Offset in C under Linux

The following codes which I wrote makes use of C function calls localtime() and gmtime() to get the local time zone offset:
  1 #include <stdio.h>
2 #include <time.h>
3
4 int main(int argc, char **argv) {
5 time_t t1, t2, base_t;
6 double offset = 0;
7
8 base_t = time(NULL);
9 t1 = mktime(gmtime(&base_t));
10 t2 = mktime(localtime(&base_t));
11 offset = difftime(t2, t1);
12
13 printf("Local Time Zone offset = %0.3f seconds (%0.3f hours)\n", offset, offset / 3600.0);
14
15 return 0;
16 }
17

沒有留言: