I am wondering if anyone / C macro knows for calculating Unix static time with hard coded date and time, like in:
time_t t = UNIX_TIMESTAMP(2012, 5, 10, 9, 26, 13);
I study this because I want to have a numeric static timestamp. This will be done a hundred times throughout the software, each time with a different date, and I want to make sure that it is fast, because it will work hundreds of times per second. Converting dates that would permanently slow things down many times (i.e., calling mktime () is slower than a static number compiled in place, right?)
[updated to try to make this paragraph clearer, November 23, 2012]
Update
I want to clarify a question with additional information about the process used. When my server receives requests, for each request it starts a new process. This process is constantly updated with new plugins, and often such updates require updating the database. They should be started only once. To find out if an update is needed, I want to use a Unix date (which is better than using a counter, because the counter will interrupt more often from time to time.)
Thus, the plugins will receive an update signal and call the on_update () function. There I want to do something like this:
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...
}
}
, , unix, , 100 1000 , 100 000 , .
, .
, last_update - ( .)
, :
#define _SNAP_UNIX_TIMESTAMP_FDAY(year) \
(((year) % 400) == 0 ? 29LL : \
(((year) % 100) == 0 ? 28LL : \
(((year) % 4) == 0 ? 29LL : \
28LL)))
#define _SNAP_UNIX_TIMESTAMP_YDAY(year, month, day) \
( \
static_cast<qint64>(day) \
+ ((month) >= 2 ? 31LL : 0LL) \
+ ((month) >= 3 ? _SNAP_UNIX_TIMESTAMP_FDAY(year) : 0LL) \
+ ((month) >= 4 ? 31LL : 0LL) \
+ ((month) >= 5 ? 30LL : 0LL) \
+ ((month) >= 6 ? 31LL : 0LL) \
+ ((month) >= 7 ? 30LL : 0LL) \
+ ((month) >= 8 ? 31LL : 0LL) \
+ ((month) >= 9 ? 31LL : 0LL) \
+ ((month) >= 10 ? 30LL : 0LL) \
+ ((month) >= 11 ? 31LL : 0LL) \
+ ((month) >= 12 ? 30LL : 0LL) \
)
#define SNAP_UNIX_TIMESTAMP(year, month, day, hour, minute, second) \
( static_cast<qint64>(second) \
+ static_cast<qint64>(minute) * 60LL \
+ static_cast<qint64>(hour) * 3600LL \
+ (_SNAP_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) * 86400LL \
+ (static_cast<qint64>(year) - 1970LL) * 31536000LL \
+ ((static_cast<qint64>(year) - 1969LL) / 4LL) * 86400LL \
- ((static_cast<qint64>(year) - 1901LL) / 100LL) * 86400LL \
+ ((static_cast<qint64>(year) - 1601LL) / 400LL) * 86400LL )
. . SLOWER, mktime(). , , time_t . , .