TDengine/include/os/osTime.h

106 lines
3.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-09-30 09:10:36 +00:00
#ifndef _TD_OS_TIME_H_
#define _TD_OS_TIME_H_
#ifdef __cplusplus
extern "C" {
#endif
2022-03-11 08:49:07 +00:00
// If the error is in a third-party library, place this header file under the third-party library header file.
2022-03-21 16:54:21 +00:00
// When you want to use this feature, you should find or add the same function in the following section.
2022-03-11 08:49:07 +00:00
#ifndef ALLOW_FORBID_FUNC
2022-06-17 13:46:09 +00:00
#define strptime STRPTIME_FUNC_TAOS_FORBID
#define gettimeofday GETTIMEOFDAY_FUNC_TAOS_FORBID
#define localtime LOCALTIME_FUNC_TAOS_FORBID
#define localtime_s LOCALTIMES_FUNC_TAOS_FORBID
#define localtime_r LOCALTIMER_FUNC_TAOS_FORBID
#define time TIME_FUNC_TAOS_FORBID
#define mktime MKTIME_FUNC_TAOS_FORBID
2022-03-11 08:49:07 +00:00
#endif
2022-04-22 01:54:27 +00:00
#ifdef WINDOWS
2022-06-17 13:46:09 +00:00
#define CLOCK_REALTIME 0
2022-11-29 01:34:28 +00:00
#define CLOCK_MONOTONIC 0
2022-06-17 13:46:09 +00:00
#define MILLISECOND_PER_SECOND (1000i64)
2021-09-30 13:21:51 +00:00
#else
2022-06-21 12:04:55 +00:00
#define MILLISECOND_PER_SECOND ((int64_t)1000LL)
2021-09-30 13:21:51 +00:00
#endif
#define MILLISECOND_PER_MINUTE (MILLISECOND_PER_SECOND * 60)
#define MILLISECOND_PER_HOUR (MILLISECOND_PER_MINUTE * 60)
#define MILLISECOND_PER_DAY (MILLISECOND_PER_HOUR * 24)
#define MILLISECOND_PER_WEEK (MILLISECOND_PER_DAY * 7)
2022-03-31 08:00:47 +00:00
2022-06-21 12:04:55 +00:00
#define NANOSECOND_PER_USEC (1000LL)
#define NANOSECOND_PER_MSEC (1000000LL)
#define NANOSECOND_PER_SEC (1000000000LL)
2022-03-31 08:00:47 +00:00
#define NANOSECOND_PER_MINUTE (NANOSECOND_PER_SEC * 60)
#define NANOSECOND_PER_HOUR (NANOSECOND_PER_MINUTE * 60)
#define NANOSECOND_PER_DAY (NANOSECOND_PER_HOUR * 24)
#define NANOSECOND_PER_WEEK (NANOSECOND_PER_DAY * 7)
2021-09-30 13:21:51 +00:00
2021-09-30 08:41:02 +00:00
int32_t taosGetTimeOfDay(struct timeval *tv);
2022-04-22 01:54:27 +00:00
int32_t taosClockGetTime(int clock_id, struct timespec *pTS);
//@return timestamp in second
int32_t taosGetTimestampSec();
//@return timestamp in millisecond
static FORCE_INLINE int64_t taosGetTimestampMs() {
struct timeval systemTime;
2024-07-22 10:47:41 +00:00
(void)taosGetTimeOfDay(&systemTime);
2022-06-21 12:04:55 +00:00
return (int64_t)systemTime.tv_sec * 1000LL + (int64_t)systemTime.tv_usec / 1000;
}
//@return timestamp in microsecond
static FORCE_INLINE int64_t taosGetTimestampUs() {
struct timeval systemTime;
2024-07-22 10:47:41 +00:00
(void)taosGetTimeOfDay(&systemTime);
2022-06-21 12:04:55 +00:00
return (int64_t)systemTime.tv_sec * 1000000LL + (int64_t)systemTime.tv_usec;
}
//@return timestamp in nanosecond
static FORCE_INLINE int64_t taosGetTimestampNs() {
struct timespec systemTime = {0};
2024-07-22 10:47:41 +00:00
(void)taosClockGetTime(CLOCK_REALTIME, &systemTime);
2022-06-21 12:04:55 +00:00
return (int64_t)systemTime.tv_sec * 1000000000LL + (int64_t)systemTime.tv_nsec;
}
2022-11-11 14:55:21 +00:00
//@return timestamp of monotonic clock in millisecond
static FORCE_INLINE int64_t taosGetMonoTimestampMs() {
struct timespec systemTime = {0};
2024-07-22 10:47:41 +00:00
(void)taosClockGetTime(CLOCK_MONOTONIC, &systemTime);
2022-11-11 14:55:21 +00:00
return (int64_t)systemTime.tv_sec * 1000LL + (int64_t)systemTime.tv_nsec / 1000000;
}
2022-10-09 08:53:44 +00:00
char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm);
struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf);
2022-10-09 13:30:41 +00:00
struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst);
2022-06-17 13:46:09 +00:00
time_t taosTime(time_t *t);
time_t taosMktime(struct tm *timep);
2023-08-07 09:32:20 +00:00
int64_t user_mktime64(const uint32_t year, const uint32_t mon, const uint32_t day, const uint32_t hour,
const uint32_t min, const uint32_t sec, int64_t time_zone);
2022-03-11 08:49:07 +00:00
#ifdef __cplusplus
}
#endif
2022-06-17 13:46:09 +00:00
#endif /*_TD_OS_TIME_H_*/