123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #include "my_systime.h"
- #include "my_config.h"
- #include <assert.h>
- #include <algorithm> // std::min
- #include <chrono>
- #include <cstdio> // std::sprintf()
- #include <limits> // std::numeric_limits
- #include <time.h> // time_t, timespec
- void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec) {
- assert(nsec != std::numeric_limits<Timeout_type>::max());
- if (nsec == TIMEOUT_INF) {
- *abstime = TIMESPEC_POSINF;
- return;
- }
- unsigned long long int now = my_getsystime() + (nsec / 100);
- unsigned long long int tv_sec = now / 10000000ULL;
- #if SIZEOF_TIME_T < SIZEOF_LONG_LONG
-
- tv_sec = std::min(tv_sec, static_cast<unsigned long long int>(
- std::numeric_limits<time_t>::max()));
- #endif
- abstime->tv_sec = static_cast<time_t>(tv_sec);
- abstime->tv_nsec = (now % 10000000ULL) * 100 + (nsec % 100);
- }
- void set_timespec(struct timespec *abstime, Timeout_type sec) {
- assert(sec != std::numeric_limits<Timeout_type>::max());
- if (sec == TIMEOUT_INF) {
- *abstime = TIMESPEC_POSINF;
- return;
- }
- set_timespec_nsec(abstime, sec * 1000000000ULL);
- }
- void get_date(char *to, int flag, time_t date) {
- struct tm *start_time;
- time_t skr;
- struct tm tm_tmp;
- skr = date ? (time_t)date : my_time(0);
- if (flag & GETDATE_GMT)
- gmtime_r(&skr, &tm_tmp);
- else
- localtime_r(&skr, &tm_tmp);
- start_time = &tm_tmp;
- if (flag & GETDATE_SHORT_DATE) {
- to += std::sprintf(to, "%02d%02d%02d", start_time->tm_year % 100,
- start_time->tm_mon + 1, start_time->tm_mday);
- } else if (flag & GETDATE_SHORT_DATE_FULL_YEAR) {
- to += std::sprintf(to, "%4d%02d%02d", start_time->tm_year + 1900,
- start_time->tm_mon + 1, start_time->tm_mday);
- } else {
- to += std::sprintf(
- to, ((flag & GETDATE_FIXEDLENGTH) ? "%4d-%02d-%02d" : "%d-%02d-%02d"),
- start_time->tm_year + 1900, start_time->tm_mon + 1,
- start_time->tm_mday);
- }
- if (flag & GETDATE_DATE_TIME) {
- if (flag & GETDATE_T_DELIMITER) {
- *to = 'T';
- ++to;
- }
- to += std::sprintf(
- to,
- ((flag & GETDATE_FIXEDLENGTH) ? " %02d:%02d:%02d" : " %2d:%02d:%02d"),
- start_time->tm_hour, start_time->tm_min, start_time->tm_sec);
- } else if (flag & GETDATE_HHMMSSTIME) {
- if (flag & GETDATE_T_DELIMITER) {
- *to = 'T';
- ++to;
- }
- to += std::sprintf(to, "%02d%02d%02d", start_time->tm_hour,
- start_time->tm_min, start_time->tm_sec);
- }
- }
|