osTime.c 1.5 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _BSD_SOURCE
S
Shengliang Guan 已提交
17

S
Shengliang Guan 已提交
18 19
#ifdef DARWIN
#define _XOPEN_SOURCE
S
Shengliang Guan 已提交
20
#else
S
Shengliang Guan 已提交
21
#define _XOPEN_SOURCE 500
S
Shengliang Guan 已提交
22 23
#endif

S
Shengliang Guan 已提交
24 25 26 27
#define _DEFAULT_SOURCE

#include "os.h"

S
Shengliang Guan 已提交
28
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
S
Shengliang Guan 已提交
29 30 31 32 33 34 35 36 37
/*
 * windows implementation
 */

#include <time.h>  
#include <winsock2.h>

int taosGetTimeOfDay(struct timeval *tv, struct timezone *tz) {
  time_t t;
S
Shengliang Guan 已提交
38 39 40 41 42 43 44 45
  t = time(NULL);
  SYSTEMTIME st;
  GetLocalTime(&st);

  tv->tv_sec = (long)t;
  tv->tv_usec = st.wMilliseconds * 1000;

  return 0;
S
Shengliang Guan 已提交
46 47 48 49 50 51 52
}

struct tm *localtime_r(const time_t *timep, struct tm *result) {
  localtime_s(result, timep);
  return result;
}

S
Shengliang Guan 已提交
53
#else
S
Shengliang Guan 已提交
54 55 56 57 58 59

/*
 * linux and darwin implementation
 */

#include <sys/time.h>
60
// #include "monotonic.h"
S
Shengliang Guan 已提交
61 62

FORCE_INLINE int32_t taosGetTimeOfDay(struct timeval *tv) {
S
Shengliang Guan 已提交
63
  return gettimeofday(tv, NULL);
S
Shengliang Guan 已提交
64 65
}

L
Liu Jicong 已提交
66 67
int32_t taosGetTimestampSec() { return (int32_t)time(NULL); }

S
Shengliang Guan 已提交
68
#endif