【问题标题】:Is there Linux system call to provide seconds count since specific date? [duplicate]是否有 Linux 系统调用来提供自特定日期以来的秒数? [复制]
【发布时间】:2014-05-08 04:30:11
【问题描述】:

例如,我想计算我在地球上生活了多少秒。 使用 gettimeofday()/time(),我可以得到自 Epoch 以来的秒数,我想要的是从 Epoch 到我生日的差异,所以我的问题是有这样的功能:

time_t gettimeof_specified_day(my_birthday);

它返回从 Epoch 到我生日的秒数,然后我可以使用 difftime(now,birthday) 来得到我想要的。

【问题讨论】:

  • 请注意,Epoch time 与自纪元以来的秒数不同,因为它省略了闰秒。根据目的,它可能是或可能不是重复的,下面的答案可能是也可能不是答案(它不是重复的,如果您询问确切的秒数,下面的答案是不正确的)。

标签: c linux


【解决方案1】:

您可以使用mktime(3) 函数将任何特定的日历日期和时间(表示为struct tm 实例)转换为time_t 值:

// Convert April 14, 1980 00:00:00 (local time, based on your computer's
// current time zone) into a time_t
struct tm my_birthday;
memset(&my_birthday, 0, sizeof(my_birthday));
my_birthday.tm_year = 80;  // Number of years since 1900
my_birthday.tm_mon = 3;    // Month from 0-11
my_birthday.tm_mday = 14;  // Day of month from 1-31
time_t t = mktime(&my_birthday);

【讨论】:

    猜你喜欢
    • 2011-02-08
    • 2021-07-03
    • 1970-01-01
    • 2016-12-23
    • 2016-11-12
    • 2015-05-27
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    相关资源
    最近更新 更多