osTimezone.c 6.1 KB
Newer Older
S
slguan 已提交
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/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
slguan 已提交
17 18 19
#define _DEFAULT_SOURCE
#include "os.h"

S
Shengliang Guan 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
#if (_WIN64)
#include <iphlpapi.h>
#include <mswsock.h>
#include <psapi.h>
#include <stdio.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Mswsock.lib ")
#endif
#include <objbase.h>
#pragma warning(push)
#pragma warning(disable : 4091)
#include <DbgHelp.h>
#pragma warning(pop)
#elif defined(_TD_DARWIN_64)
#include <errno.h>
#include <libproc.h>
#else
#include <argp.h>
#include <linux/sysctl.h>
#include <sys/file.h>
#include <sys/resource.h>
#include <sys/statvfs.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
#endif

wafwerar's avatar
wafwerar 已提交
49 50 51 52 53 54 55 56 57 58 59 60
void taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, int8_t *outDaylight, enum TdTimezone *tsTimezone) {
  if (inTimezoneStr == NULL || inTimezoneStr[0] == 0) return;

  char *buf = taosMemoryMalloc(strlen(inTimezoneStr) + 1);
  buf[strlen(inTimezoneStr)] = 0;
  for (int32_t i = 0; i < strlen(inTimezoneStr); i++) {
      if(inTimezoneStr[i]==' ' || inTimezoneStr[i]=='(') {
          buf[i] = 0;
          break;
      }
      buf[i] = inTimezoneStr[i];
  }
S
Shengliang Guan 已提交
61

62
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
S
Shengliang Guan 已提交
63
  char winStr[TD_LOCALE_LEN * 2];
wafwerar's avatar
wafwerar 已提交
64
  sprintf(winStr, "TZ=%s", buf);
S
slguan 已提交
65 66
  putenv(winStr);
  tzset();
S
Shengliang Guan 已提交
67 68 69 70 71 72
   * get CURRENT time zone.
   * system current time zone is affected by daylight saving time(DST)
   *
   * e.g., the local time zone of London in DST is GMT+01:00,
   * otherwise is GMT+00:00
   */
S
slguan 已提交
73 74 75 76 77
#ifdef _MSC_VER
#if _MSC_VER >= 1900
  // see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019
  int64_t timezone = _timezone;
  int32_t daylight = _daylight;
S
Shengliang Guan 已提交
78
  char  **tzname = _tzname;
S
slguan 已提交
79 80 81
#endif
#endif

82
  int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR);
wafwerar's avatar
wafwerar 已提交
83
  *tsTimezone = tz;
S
slguan 已提交
84 85
  tz += daylight;
  /*
S
Shengliang Guan 已提交
86 87 88 89
   * format:
   * (CST, +0800)
   * (BST, +0100)
   */
wafwerar's avatar
wafwerar 已提交
90
  sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz));
91 92 93 94
  *outDaylight = daylight;

#elif defined(_TD_DARWIN_64)

wafwerar's avatar
wafwerar 已提交
95
  setenv("TZ", buf, 1);
96 97
  tzset();
  int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR);
wafwerar's avatar
wafwerar 已提交
98
  *tsTimezone = tz;
99
  tz += daylight;
S
Shengliang Guan 已提交
100

wafwerar's avatar
wafwerar 已提交
101
  sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz));
S
Shengliang Guan 已提交
102
  *outDaylight = daylight;
103 104

#else
wafwerar's avatar
wafwerar 已提交
105
  setenv("TZ", buf, 1);
106 107
  tzset();
  int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR);
wafwerar's avatar
wafwerar 已提交
108
  *tsTimezone = tz;
109
  tz += daylight;
wafwerar's avatar
wafwerar 已提交
110
  sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz));
111 112 113 114
  *outDaylight = daylight;

#endif

wafwerar's avatar
wafwerar 已提交
115
  taosMemoryFree(buf);
S
Shengliang Guan 已提交
116 117
}

wafwerar's avatar
wafwerar 已提交
118
void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
S
Shengliang Guan 已提交
119 120 121
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  char *tz = getenv("TZ");
  if (tz == NULL || strlen(tz) == 0) {
wafwerar's avatar
wafwerar 已提交
122
    strcpy(outTimezoneStr, "not configured");
S
Shengliang Guan 已提交
123
  } else {
wafwerar's avatar
wafwerar 已提交
124
    strcpy(outTimezoneStr, tz);
S
Shengliang Guan 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  }

#elif defined(_TD_DARWIN_64)
  char  buf[4096] = {0};
  char *tz = NULL;
  {
    int n = readlink("/etc/localtime", buf, sizeof(buf));
    if (n < 0) {
      printf("read /etc/localtime error, reason:%s", strerror(errno));
      return;
    }
    buf[n] = '\0';
    for (int i = n - 1; i >= 0; --i) {
      if (buf[i] == '/') {
        if (tz) {
          tz = buf + i + 1;
          break;
        }
        tz = buf + i + 1;
      }
    }
    if (!tz || 0 == strchr(tz, '/')) {
      printf("parsing /etc/localtime failed");
      return;
    }

    setenv("TZ", tz, 1);
    tzset();
  }

  /*
   * NOTE: do not remove it.
   * Enforce set the correct daylight saving time(DST) flag according
   * to current time
   */
wafwerar's avatar
wafwerar 已提交
160
  time_t    tx1 = taosGetTimestampSec();
S
Shengliang Guan 已提交
161
  struct tm tm1;
wafwerar's avatar
wafwerar 已提交
162
  taosLocalTime(&tx1, &tm1);
S
Shengliang Guan 已提交
163 164 165 166 167 168 169

  /*
   * format example:
   *
   * Asia/Shanghai   (CST, +0800)
   * Europe/London   (BST, +0100)
   */
wafwerar's avatar
wafwerar 已提交
170
  snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0],
S
Shengliang Guan 已提交
171 172 173 174 175 176 177 178
           -timezone / 3600);

#else
  /*
   * NOTE: do not remove it.
   * Enforce set the correct daylight saving time(DST) flag according
   * to current time
   */
wafwerar's avatar
wafwerar 已提交
179
  time_t    tx1 = taosGetTimestampSec();
S
Shengliang Guan 已提交
180
  struct tm tm1;
wafwerar's avatar
wafwerar 已提交
181
  taosLocalTime(&tx1, &tm1);
S
slguan 已提交
182

S
Shengliang Guan 已提交
183
  /* load time zone string from /etc/timezone */
184
  // FILE *f = fopen("/etc/timezone", "r");
wafwerar's avatar
wafwerar 已提交
185
  errno = 0;
186
  TdFilePtr pFile = taosOpenFile("/etc/timezone", TD_FILE_READ);
S
Shengliang Guan 已提交
187
  char  buf[68] = {0};
188 189 190 191
  if (pFile != NULL) {
    int len = taosReadFile(pFile, buf, 64);
    if (len < 64 && taosGetErrorFile(pFile)) {
      taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
192
      printf("read /etc/timezone error, reason:%s", strerror(errno));
S
Shengliang Guan 已提交
193 194
      return;
    }
S
Shengliang Guan 已提交
195

196
    taosCloseFile(&pFile);
S
Shengliang Guan 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

    buf[sizeof(buf) - 1] = 0;
    char *lineEnd = strstr(buf, "\n");
    if (lineEnd != NULL) {
      *lineEnd = 0;
    }

    // for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables
    if (strlen(buf) > 0) {
      setenv("TZ", buf, 1);
    }
  }
  // get and set default timezone
  tzset();

  /*
   * get CURRENT time zone.
   * system current time zone is affected by daylight saving time(DST)
   *
   * e.g., the local time zone of London in DST is GMT+01:00,
   * otherwise is GMT+00:00
   */
  int32_t tz = (-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR;
wafwerar's avatar
wafwerar 已提交
220
  *tsTimezone = tz;
S
Shengliang Guan 已提交
221 222 223 224 225 226 227 228
  tz += daylight;

  /*
   * format example:
   *
   * Asia/Shanghai   (CST, +0800)
   * Europe/London   (BST, +0100)
   */
wafwerar's avatar
wafwerar 已提交
229
  snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz));
S
Shengliang Guan 已提交
230 231

#endif
S
slguan 已提交
232
}