Do you know the C macro for calculating Unix time and date?

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...
  }
  // as many test as required...
}

, , unix, , 100 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<qint64>(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<qint64>(second) \
                + static_cast<qint64>(minute) * 60LL \
                + static_cast<qint64>(hour) * 3600LL \
    + /* year day (month + day) */ (_SNAP_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) * 86400LL \
    + /* year */ (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 . , .

+3
3

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

+4

, , :

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

.

+2

Not a macro, but the timestamp will be initialized only once, subsequent calls get_timestamp()will be easy access to memory. You pay a penalty for initialization at runtime, but it is called only for the first time get_timestamp(), knowing that you can initialize it at an early stage of your program and allow subsequent calls to be “free”.

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;
}
+1
source

Source: https://habr.com/ru/post/1536242/


All Articles