在Linux下面编程,经常要碰到用到与time/timestamp相关的东西,下面是我以前收集的,放在这里共享。
In the file: dbus-sysdeps-unix.c
/** * Get current time, as in gettimeofday(). * * @param tv_sec return location for number of seconds * @param tv_usec return location for number of microseconds (thousandths) */ void _dbus_get_current_time (long *tv_sec, long *tv_usec) { struct timeval t;
gettimeofday (&t, NULL);
if (tv_sec) *tv_sec = t.tv_sec; if (tv_usec) *tv_usec = t.tv_usec; }
How to use it:
/** * Get current time, as in gettimeofday(). * * @param tv_sec return location for number of seconds * @param tv_usec return location for number of microseconds (thousandths) */ void _dbus_get_current_time (long *tv_sec, long *tv_usec) { struct timeval t;
gettimeofday (&t, NULL);
if (tv_sec) *tv_sec = t.tv_sec; if (tv_usec) *tv_usec = t.tv_usec; }
Two time structures:
Struct timeval:
struct timeval |
14.11 我知道库函数 localtime() 可以把 time_t 转换成结构 struct tm, 而 ctime() 可以把 time_t 转换成为可打印的字符串。怎样才能进行反向操作, 把 struct tm 或一个字符串转换成 time_t?
ANSI C 提供了库函数 mktime(), 它把 struct tm 转换成 time_t。
把一个字符串转换成 time_t 比较难些, 这是由于可能遇到各种各样的日期和时间格式。某些系统提供函数 strptime(), 基本上是 strftime() 的反向函数。其它常用的函数有 partime() (与 RCS 包一起被广泛的发布) 和 getdate() (还有少数其它函数, 发布在 C 的新闻组)。参见问题 18.18。
C/C++中的日期和时间 time_t(cjp: long)与struct tm
2006-07-18 14:07
摘要: http://wiseman.cnblogs.com 关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元) 1.概念 通过学习许多C/C++库,你可以有很多操作、使用时间的方法。但在这之前你需要了解一些“时间”和“日期”的概念,主要有以下几个: Coordinated Universal Time(UTC):协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。比如,中国内地的时间与UTC的时差为+8,也就是UTC+8。美国是UTC-5。 Calendar Time:日历时间,是用“从一个标准时间
|