带时区的C / C ++ Linux时钟(包括夏令时)

时间:2014-10-21 14:01:48

标签: c++ c linux linux-kernel

我希望通过选择时区来显示时钟HRS MINS SECONDS。我正在使用以下用于Linux系统API的C代码来获取时间:

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

void print_time ()
{
 struct timeval tv;
 struct tm* ptm;
 char time_string[40];
 long milliseconds;

 /* Obtain the time of day, and convert it to a tm struct. */
 gettimeofday (&tv, NULL);
 ptm = localtime (&tv.tv_sec);
 /* Format the date and time, down to a single second. */
 strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm);
 /* Compute milliseconds from microseconds. */
 milliseconds = tv.tv_usec / 1000;
 /* Print the formatted time, in seconds, followed by a decimal point
   and the milliseconds. */
 printf ("%s.%03ld\n", time_string, milliseconds);
}

如何通过时区设置选项实现时钟。任何日期时间库?

0 个答案:

没有答案