你知道一个用于计算Unix时间和日期的C宏吗?

我想知道是否有人知道/有一个C宏来计算硬编码日期和时间的静态Unix时间,如:

time_t t = UNIX_TIMESTAMP(2012, 5, 10, 9, 26, 13); 

我正在调查,因为我想要一个数字静态时间戳。 这将在整个软件中完成数百次,每次都有不同的日期,我想确保它很快,因为它每秒运行数百次。 转换多次会明确减慢速度的日期(即调用mktime()比编译静态数字要慢,对吧?)

[更新以试图使这一段更清晰,2012年11月23日]

更新

我想澄清有关正在使用的过程的更多信息的问题。 当我的服务器收到请求时,对于每个请求,它都会启动一个新进程。 使用新插件不断更新该过程,并且此类更新通常需要数据库更新。 那些必须只运行一次。 要知道是否需要更新,我想使用Unix日期(这比使用计数器更好,因为计数器更有可能偶尔会破坏一次。)

因此,插件将接收更新信号并调用其on_update()函数。 在那里我想做这样的事情:

 void some_plugin::on_update(time_t last_update) { if(last_update < UNIX_TIMESTAMP(2010, 3, 22, 20, 9, 26)) { ...run update... } if(last_update < UNIX_TIMESTAMP(2012, 5, 10, 9, 26, 13)) { ...run update... } // as many test as required... } 

正如您所看到的,如果我每次都必须计算unix时间戳,这可能代表每个进程数千个调用,如果您每次接收100次点击x 1000次调用,那么当您可以让编译器计算这些数字时,您会浪费100,000次调用一次在编译时。

将值放在静态变量中是没有意义的,因为此代码将在每个进程运行时运行一次。

请注意,last_update变量会根据被点击的网站而变化(它来自数据库。)

好的,我现在得到了代码:

 // helper (Days in February) #define _SNAP_UNIX_TIMESTAMP_FDAY(year) \ (((year) % 400) == 0 ? 29LL : \ (((year) % 100) == 0 ? 28LL : \ (((year) % 4) == 0 ? 29LL : \ 28LL))) // helper (Days in the year) #define _SNAP_UNIX_TIMESTAMP_YDAY(year, month, day) \ ( \ /* January */ static_cast(day) \ /* February */ + ((month) >= 2 ? 31LL : 0LL) \ /* March */ + ((month) >= 3 ? _SNAP_UNIX_TIMESTAMP_FDAY(year) : 0LL) \ /* April */ + ((month) >= 4 ? 31LL : 0LL) \ /* May */ + ((month) >= 5 ? 30LL : 0LL) \ /* June */ + ((month) >= 6 ? 31LL : 0LL) \ /* July */ + ((month) >= 7 ? 30LL : 0LL) \ /* August */ + ((month) >= 8 ? 31LL : 0LL) \ /* September */+ ((month) >= 9 ? 31LL : 0LL) \ /* October */ + ((month) >= 10 ? 30LL : 0LL) \ /* November */ + ((month) >= 11 ? 31LL : 0LL) \ /* December */ + ((month) >= 12 ? 30LL : 0LL) \ ) #define SNAP_UNIX_TIMESTAMP(year, month, day, hour, minute, second) \ ( /* time */ static_cast(second) \ + static_cast(minute) * 60LL \ + static_cast(hour) * 3600LL \ + /* year day (month + day) */ (_SNAP_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) * 86400LL \ + /* year */ (static_cast(year) - 1970LL) * 31536000LL \ + ((static_cast(year) - 1969LL) / 4LL) * 86400LL \ - ((static_cast(year) - 1901LL) / 100LL) * 86400LL \ + ((static_cast(year) - 1601LL) / 400LL) * 86400LL ) 

警告:请勿使用这些宏动态计算日期。 它比mktime()慢。 这就是说,如果你有一个硬编码日期,那么编译器将在编译时计算time_t值。 编译速度较慢,但​​反复执行速度更快。

公式在POSIX中:

 tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 + (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 - ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400 

资料来源:XBD 4.15秒自大纪元以来http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15

如果你测量它实际上太慢了,那么这是一个实用的简单解决方案:

 void myfun() { static time_t t = 0; if (t == 0) t = slow_unix_timestamp(2012, 5, 10, 9, 26, 13); } 

现在只计算一次。

不是宏,但时间戳只会初始化一次,后续对get_timestamp()调用很简单就是内存访问。 您在运行时支付初始化惩罚,但只是第一次get_timestamp() ,因为您知道可以在程序的早期初始化它并允许后续调用有效“免费”。

 time_t initialize_timestamp(int y, int m, int d, int h, int min, s) { tm t; t.tm_year = y - 1900; t.tm_mon = m; t.tm.mday = d; t.tm_hour = h; t.tm_min = min; t.tm_sec = s; return mktime(&t); } time_t get_static_timestamp() { static time_t ts = initialize_timestamp(2012, 5, 10, 9, 26, 13); return ts; }