113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci ***************************************************************************/
2413498266Sopenharmony_ci/* <DESC>
2513498266Sopenharmony_ci * Set your system time from a remote HTTP server's Date: header.
2613498266Sopenharmony_ci * </DESC>
2713498266Sopenharmony_ci */
2813498266Sopenharmony_ci/* This example code only builds as-is on Windows.
2913498266Sopenharmony_ci *
3013498266Sopenharmony_ci * While Unix/Linux user, you do not need this software.
3113498266Sopenharmony_ci * You can achieve the same result as synctime using curl, awk and date.
3213498266Sopenharmony_ci * Set proxy as according to your network, but beware of proxy Cache-Control.
3313498266Sopenharmony_ci *
3413498266Sopenharmony_ci * To set your system clock, root access is required.
3513498266Sopenharmony_ci * # date -s "`curl -sI https://nist.time.gov/timezone.cgi?UTC/s/0 \
3613498266Sopenharmony_ci *        | awk -F': ' '/Date: / {print $2}'`"
3713498266Sopenharmony_ci *
3813498266Sopenharmony_ci * To view remote webserver date and time.
3913498266Sopenharmony_ci * $ curl -sI https://nist.time.gov/timezone.cgi?UTC/s/0 \
4013498266Sopenharmony_ci *        | awk -F': ' '/Date: / {print $2}'
4113498266Sopenharmony_ci *
4213498266Sopenharmony_ci * Synchronising your computer clock via Internet time server usually relies
4313498266Sopenharmony_ci * on DAYTIME, TIME, or NTP protocols. These protocols provide good accurate
4413498266Sopenharmony_ci * time synchronization but it does not work well through a
4513498266Sopenharmony_ci * firewall/proxy. Some adjustment has to be made to the firewall/proxy for
4613498266Sopenharmony_ci * these protocols to work properly.
4713498266Sopenharmony_ci *
4813498266Sopenharmony_ci * There is an indirect method. Since most webserver provide server time in
4913498266Sopenharmony_ci * their HTTP header, therefore you could synchronise your computer clock
5013498266Sopenharmony_ci * using HTTP protocol which has no problem with firewall/proxy.
5113498266Sopenharmony_ci *
5213498266Sopenharmony_ci * For this software to work, you should take note of these items.
5313498266Sopenharmony_ci * 1. Your firewall/proxy must allow your computer to surf internet.
5413498266Sopenharmony_ci * 2. Webserver system time must in sync with the NTP time server,
5513498266Sopenharmony_ci *    or at least provide an accurate time keeping.
5613498266Sopenharmony_ci * 3. Webserver HTTP header does not provide the milliseconds units,
5713498266Sopenharmony_ci *    so there is no way to get an accurate time.
5813498266Sopenharmony_ci * 4. This software could only provide an accuracy of +- a few seconds,
5913498266Sopenharmony_ci *    as Round-Trip delay time is not taken into consideration.
6013498266Sopenharmony_ci *    Compensation of network, firewall/proxy delay cannot be simply divide
6113498266Sopenharmony_ci *    the Round-Trip delay time by half.
6213498266Sopenharmony_ci * 5. Win32 SetSystemTime() API will set your computer clock according to
6313498266Sopenharmony_ci *    GMT/UTC time. Therefore your computer timezone must be properly set.
6413498266Sopenharmony_ci * 6. Webserver data should not be cached by the proxy server. Some
6513498266Sopenharmony_ci *    webserver provide Cache-Control to prevent caching.
6613498266Sopenharmony_ci *
6713498266Sopenharmony_ci * References:
6813498266Sopenharmony_ci * https://web.archive.org/web/20100228012139/ \
6913498266Sopenharmony_ci *    tf.nist.gov/timefreq/service/its.htm
7013498266Sopenharmony_ci * https://web.archive.org/web/20100409024302/ \
7113498266Sopenharmony_ci *    tf.nist.gov/timefreq/service/firewall.htm
7213498266Sopenharmony_ci *
7313498266Sopenharmony_ci * Usage:
7413498266Sopenharmony_ci * This software will synchronise your computer clock only when you issue
7513498266Sopenharmony_ci * it with --synctime. By default, it only display the webserver's clock.
7613498266Sopenharmony_ci *
7713498266Sopenharmony_ci * Written by: Frank (contributed to libcurl)
7813498266Sopenharmony_ci *
7913498266Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
8013498266Sopenharmony_ci * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
8113498266Sopenharmony_ci * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
8213498266Sopenharmony_ci *
8313498266Sopenharmony_ci * IN NO EVENT SHALL THE AUTHOR OF THIS SOFTWARE BE LIABLE FOR
8413498266Sopenharmony_ci * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
8513498266Sopenharmony_ci * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
8613498266Sopenharmony_ci * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
8713498266Sopenharmony_ci * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
8813498266Sopenharmony_ci * OF THIS SOFTWARE.
8913498266Sopenharmony_ci *
9013498266Sopenharmony_ci */
9113498266Sopenharmony_ci
9213498266Sopenharmony_ci#include <stdio.h>
9313498266Sopenharmony_ci#include <time.h>
9413498266Sopenharmony_ci#include <curl/curl.h>
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci#ifdef _WIN32
9713498266Sopenharmony_ci#include <windows.h>
9813498266Sopenharmony_ci#endif
9913498266Sopenharmony_ci
10013498266Sopenharmony_ci
10113498266Sopenharmony_ci#define MAX_STRING              256
10213498266Sopenharmony_ci#define MAX_STRING1             MAX_STRING + 1
10313498266Sopenharmony_ci
10413498266Sopenharmony_ci#define SYNCTIME_UA "synctime/1.0"
10513498266Sopenharmony_ci
10613498266Sopenharmony_citypedef struct
10713498266Sopenharmony_ci{
10813498266Sopenharmony_ci  char http_proxy[MAX_STRING1];
10913498266Sopenharmony_ci  char proxy_user[MAX_STRING1];
11013498266Sopenharmony_ci  char timeserver[MAX_STRING1];
11113498266Sopenharmony_ci} conf_t;
11213498266Sopenharmony_ci
11313498266Sopenharmony_ciconst char DefaultTimeServer[3][MAX_STRING1] =
11413498266Sopenharmony_ci{
11513498266Sopenharmony_ci  "https://nist.time.gov/",
11613498266Sopenharmony_ci  "https://www.google.com/"
11713498266Sopenharmony_ci};
11813498266Sopenharmony_ci
11913498266Sopenharmony_ciconst char *DayStr[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
12013498266Sopenharmony_ciconst char *MthStr[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
12113498266Sopenharmony_ci                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
12213498266Sopenharmony_ci
12313498266Sopenharmony_ciint  ShowAllHeader;
12413498266Sopenharmony_ciint  AutoSyncTime;
12513498266Sopenharmony_ciSYSTEMTIME SYSTime;
12613498266Sopenharmony_ciSYSTEMTIME LOCALTime;
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci#define HTTP_COMMAND_HEAD       0
12913498266Sopenharmony_ci#define HTTP_COMMAND_GET        1
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci
13213498266Sopenharmony_cisize_t SyncTime_CURL_WriteOutput(void *ptr, size_t size, size_t nmemb,
13313498266Sopenharmony_ci                                 void *stream)
13413498266Sopenharmony_ci{
13513498266Sopenharmony_ci  fwrite(ptr, size, nmemb, stream);
13613498266Sopenharmony_ci  return (nmemb*size);
13713498266Sopenharmony_ci}
13813498266Sopenharmony_ci
13913498266Sopenharmony_cisize_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
14013498266Sopenharmony_ci                                 void *stream)
14113498266Sopenharmony_ci{
14213498266Sopenharmony_ci  char  TmpStr1[26], TmpStr2[26];
14313498266Sopenharmony_ci
14413498266Sopenharmony_ci  (void)stream;
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci  if(ShowAllHeader == 1)
14713498266Sopenharmony_ci    fprintf(stderr, "%s", (char *)(ptr));
14813498266Sopenharmony_ci
14913498266Sopenharmony_ci  if(strncmp((char *)(ptr), "Date:", 5) == 0) {
15013498266Sopenharmony_ci    if(ShowAllHeader == 0)
15113498266Sopenharmony_ci      fprintf(stderr, "HTTP Server. %s", (char *)(ptr));
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci    if(AutoSyncTime == 1) {
15413498266Sopenharmony_ci      *TmpStr1 = 0;
15513498266Sopenharmony_ci      *TmpStr2 = 0;
15613498266Sopenharmony_ci      if(strlen((char *)(ptr)) > 50) /* Can prevent buffer overflow to
15713498266Sopenharmony_ci                                         TmpStr1 & 2? */
15813498266Sopenharmony_ci        AutoSyncTime = 0;
15913498266Sopenharmony_ci      else {
16013498266Sopenharmony_ci        int RetVal = sscanf((char *)(ptr), "Date: %25s %hu %s %hu %hu:%hu:%hu",
16113498266Sopenharmony_ci                            TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear,
16213498266Sopenharmony_ci                            &SYSTime.wHour, &SYSTime.wMinute,
16313498266Sopenharmony_ci                            &SYSTime.wSecond);
16413498266Sopenharmony_ci
16513498266Sopenharmony_ci        if(RetVal == 7) {
16613498266Sopenharmony_ci          int i;
16713498266Sopenharmony_ci          SYSTime.wMilliseconds = 500;    /* adjust to midpoint, 0.5 sec */
16813498266Sopenharmony_ci          for(i = 0; i<12; i++) {
16913498266Sopenharmony_ci            if(strcmp(MthStr[i], TmpStr2) == 0) {
17013498266Sopenharmony_ci              SYSTime.wMonth = i + 1;
17113498266Sopenharmony_ci              break;
17213498266Sopenharmony_ci            }
17313498266Sopenharmony_ci          }
17413498266Sopenharmony_ci          AutoSyncTime = 3;       /* Computer clock will be adjusted */
17513498266Sopenharmony_ci        }
17613498266Sopenharmony_ci        else {
17713498266Sopenharmony_ci          AutoSyncTime = 0;       /* Error in sscanf() fields conversion */
17813498266Sopenharmony_ci        }
17913498266Sopenharmony_ci      }
18013498266Sopenharmony_ci    }
18113498266Sopenharmony_ci  }
18213498266Sopenharmony_ci
18313498266Sopenharmony_ci  if(strncmp((char *)(ptr), "X-Cache: HIT", 12) == 0) {
18413498266Sopenharmony_ci    fprintf(stderr, "ERROR: HTTP Server data is cached."
18513498266Sopenharmony_ci            " Server Date is no longer valid.\n");
18613498266Sopenharmony_ci    AutoSyncTime = 0;
18713498266Sopenharmony_ci  }
18813498266Sopenharmony_ci  return (nmemb*size);
18913498266Sopenharmony_ci}
19013498266Sopenharmony_ci
19113498266Sopenharmony_civoid SyncTime_CURL_Init(CURL *curl, char *proxy_port,
19213498266Sopenharmony_ci                        char *proxy_user_password)
19313498266Sopenharmony_ci{
19413498266Sopenharmony_ci  if(strlen(proxy_port) > 0)
19513498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_PROXY, proxy_port);
19613498266Sopenharmony_ci
19713498266Sopenharmony_ci  if(strlen(proxy_user_password) > 0)
19813498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_password);
19913498266Sopenharmony_ci
20013498266Sopenharmony_ci#ifdef SYNCTIME_UA
20113498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_USERAGENT, SYNCTIME_UA);
20213498266Sopenharmony_ci#endif
20313498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, SyncTime_CURL_WriteOutput);
20413498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, SyncTime_CURL_WriteHeader);
20513498266Sopenharmony_ci}
20613498266Sopenharmony_ci
20713498266Sopenharmony_ciint SyncTime_CURL_Fetch(CURL *curl, char *URL_Str, char *OutFileName,
20813498266Sopenharmony_ci                        int HttpGetBody)
20913498266Sopenharmony_ci{
21013498266Sopenharmony_ci  FILE *outfile;
21113498266Sopenharmony_ci  CURLcode res;
21213498266Sopenharmony_ci
21313498266Sopenharmony_ci  outfile = NULL;
21413498266Sopenharmony_ci  if(HttpGetBody == HTTP_COMMAND_HEAD)
21513498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
21613498266Sopenharmony_ci  else {
21713498266Sopenharmony_ci    outfile = fopen(OutFileName, "wb");
21813498266Sopenharmony_ci    curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
21913498266Sopenharmony_ci  }
22013498266Sopenharmony_ci
22113498266Sopenharmony_ci  curl_easy_setopt(curl, CURLOPT_URL, URL_Str);
22213498266Sopenharmony_ci  res = curl_easy_perform(curl);
22313498266Sopenharmony_ci  if(outfile)
22413498266Sopenharmony_ci    fclose(outfile);
22513498266Sopenharmony_ci  return res;  /* (CURLE_OK) */
22613498266Sopenharmony_ci}
22713498266Sopenharmony_ci
22813498266Sopenharmony_civoid showUsage(void)
22913498266Sopenharmony_ci{
23013498266Sopenharmony_ci  fprintf(stderr, "SYNCTIME: Synchronising computer clock with time server"
23113498266Sopenharmony_ci          " using HTTP protocol.\n");
23213498266Sopenharmony_ci  fprintf(stderr, "Usage   : SYNCTIME [Option]\n");
23313498266Sopenharmony_ci  fprintf(stderr, "Options :\n");
23413498266Sopenharmony_ci  fprintf(stderr, " --server=WEBSERVER        Use this time server instead"
23513498266Sopenharmony_ci          " of default.\n");
23613498266Sopenharmony_ci  fprintf(stderr, " --showall                 Show all HTTP header.\n");
23713498266Sopenharmony_ci  fprintf(stderr, " --synctime                Synchronising computer clock"
23813498266Sopenharmony_ci          " with time server.\n");
23913498266Sopenharmony_ci  fprintf(stderr, " --proxy-user=USER[:PASS]  Set proxy username and"
24013498266Sopenharmony_ci          " password.\n");
24113498266Sopenharmony_ci  fprintf(stderr, " --proxy=HOST[:PORT]       Use HTTP proxy on given"
24213498266Sopenharmony_ci          " port.\n");
24313498266Sopenharmony_ci  fprintf(stderr, " --help                    Print this help.\n");
24413498266Sopenharmony_ci  fprintf(stderr, "\n");
24513498266Sopenharmony_ci  return;
24613498266Sopenharmony_ci}
24713498266Sopenharmony_ci
24813498266Sopenharmony_ciint conf_init(conf_t *conf)
24913498266Sopenharmony_ci{
25013498266Sopenharmony_ci  int i;
25113498266Sopenharmony_ci
25213498266Sopenharmony_ci  *conf->http_proxy       = 0;
25313498266Sopenharmony_ci  for(i = 0; i<MAX_STRING1; i++)
25413498266Sopenharmony_ci    conf->proxy_user[i]     = 0;    /* Clean up password from memory */
25513498266Sopenharmony_ci  *conf->timeserver       = 0;
25613498266Sopenharmony_ci  return 1;
25713498266Sopenharmony_ci}
25813498266Sopenharmony_ci
25913498266Sopenharmony_ciint main(int argc, char *argv[])
26013498266Sopenharmony_ci{
26113498266Sopenharmony_ci  CURL    *curl;
26213498266Sopenharmony_ci  conf_t  conf[1];
26313498266Sopenharmony_ci  int     RetValue;
26413498266Sopenharmony_ci
26513498266Sopenharmony_ci  ShowAllHeader   = 0;    /* Do not show HTTP Header */
26613498266Sopenharmony_ci  AutoSyncTime    = 0;    /* Do not synchronise computer clock */
26713498266Sopenharmony_ci  RetValue        = 0;    /* Successful Exit */
26813498266Sopenharmony_ci  conf_init(conf);
26913498266Sopenharmony_ci
27013498266Sopenharmony_ci  if(argc > 1) {
27113498266Sopenharmony_ci    int OptionIndex = 0;
27213498266Sopenharmony_ci    while(OptionIndex < argc) {
27313498266Sopenharmony_ci      if(strncmp(argv[OptionIndex], "--server=", 9) == 0)
27413498266Sopenharmony_ci        snprintf(conf->timeserver, MAX_STRING, "%s", &argv[OptionIndex][9]);
27513498266Sopenharmony_ci
27613498266Sopenharmony_ci      if(strcmp(argv[OptionIndex], "--showall") == 0)
27713498266Sopenharmony_ci        ShowAllHeader = 1;
27813498266Sopenharmony_ci
27913498266Sopenharmony_ci      if(strcmp(argv[OptionIndex], "--synctime") == 0)
28013498266Sopenharmony_ci        AutoSyncTime = 1;
28113498266Sopenharmony_ci
28213498266Sopenharmony_ci      if(strncmp(argv[OptionIndex], "--proxy-user=", 13) == 0)
28313498266Sopenharmony_ci        snprintf(conf->proxy_user, MAX_STRING, "%s", &argv[OptionIndex][13]);
28413498266Sopenharmony_ci
28513498266Sopenharmony_ci      if(strncmp(argv[OptionIndex], "--proxy=", 8) == 0)
28613498266Sopenharmony_ci        snprintf(conf->http_proxy, MAX_STRING, "%s", &argv[OptionIndex][8]);
28713498266Sopenharmony_ci
28813498266Sopenharmony_ci      if((strcmp(argv[OptionIndex], "--help") == 0) ||
28913498266Sopenharmony_ci          (strcmp(argv[OptionIndex], "/?") == 0)) {
29013498266Sopenharmony_ci        showUsage();
29113498266Sopenharmony_ci        return 0;
29213498266Sopenharmony_ci      }
29313498266Sopenharmony_ci      OptionIndex++;
29413498266Sopenharmony_ci    }
29513498266Sopenharmony_ci  }
29613498266Sopenharmony_ci
29713498266Sopenharmony_ci  if(*conf->timeserver == 0)     /* Use default server for time information */
29813498266Sopenharmony_ci    snprintf(conf->timeserver, MAX_STRING, "%s", DefaultTimeServer[0]);
29913498266Sopenharmony_ci
30013498266Sopenharmony_ci  /* Init CURL before usage */
30113498266Sopenharmony_ci  curl_global_init(CURL_GLOBAL_ALL);
30213498266Sopenharmony_ci  curl = curl_easy_init();
30313498266Sopenharmony_ci  if(curl) {
30413498266Sopenharmony_ci    struct tm *lt;
30513498266Sopenharmony_ci    struct tm *gmt;
30613498266Sopenharmony_ci    time_t tt;
30713498266Sopenharmony_ci    time_t tt_local;
30813498266Sopenharmony_ci    time_t tt_gmt;
30913498266Sopenharmony_ci    double tzonediffFloat;
31013498266Sopenharmony_ci    int tzonediffWord;
31113498266Sopenharmony_ci    char timeBuf[61];
31213498266Sopenharmony_ci    char tzoneBuf[16];
31313498266Sopenharmony_ci
31413498266Sopenharmony_ci    SyncTime_CURL_Init(curl, conf->http_proxy, conf->proxy_user);
31513498266Sopenharmony_ci
31613498266Sopenharmony_ci    /* Calculating time diff between GMT and localtime */
31713498266Sopenharmony_ci    tt       = time(0);
31813498266Sopenharmony_ci    lt       = localtime(&tt);
31913498266Sopenharmony_ci    tt_local = mktime(lt);
32013498266Sopenharmony_ci    gmt      = gmtime(&tt);
32113498266Sopenharmony_ci    tt_gmt   = mktime(gmt);
32213498266Sopenharmony_ci    tzonediffFloat = difftime(tt_local, tt_gmt);
32313498266Sopenharmony_ci    tzonediffWord  = (int)(tzonediffFloat/3600.0);
32413498266Sopenharmony_ci
32513498266Sopenharmony_ci    if((double)(tzonediffWord * 3600) == tzonediffFloat)
32613498266Sopenharmony_ci      snprintf(tzoneBuf, 15, "%+03d'00'", tzonediffWord);
32713498266Sopenharmony_ci    else
32813498266Sopenharmony_ci      snprintf(tzoneBuf, 15, "%+03d'30'", tzonediffWord);
32913498266Sopenharmony_ci
33013498266Sopenharmony_ci    /* Get current system time and local time */
33113498266Sopenharmony_ci    GetSystemTime(&SYSTime);
33213498266Sopenharmony_ci    GetLocalTime(&LOCALTime);
33313498266Sopenharmony_ci    snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
33413498266Sopenharmony_ci             DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
33513498266Sopenharmony_ci             MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
33613498266Sopenharmony_ci             LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
33713498266Sopenharmony_ci             LOCALTime.wMilliseconds);
33813498266Sopenharmony_ci
33913498266Sopenharmony_ci    fprintf(stderr, "Fetch: %s\n\n", conf->timeserver);
34013498266Sopenharmony_ci    fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf);
34113498266Sopenharmony_ci
34213498266Sopenharmony_ci    /* HTTP HEAD command to the Webserver */
34313498266Sopenharmony_ci    SyncTime_CURL_Fetch(curl, conf->timeserver, "index.htm",
34413498266Sopenharmony_ci                        HTTP_COMMAND_HEAD);
34513498266Sopenharmony_ci
34613498266Sopenharmony_ci    GetLocalTime(&LOCALTime);
34713498266Sopenharmony_ci    snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
34813498266Sopenharmony_ci             DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
34913498266Sopenharmony_ci             MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
35013498266Sopenharmony_ci             LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
35113498266Sopenharmony_ci             LOCALTime.wMilliseconds);
35213498266Sopenharmony_ci    fprintf(stderr, "\nAfter  HTTP. Date: %s%s\n", timeBuf, tzoneBuf);
35313498266Sopenharmony_ci
35413498266Sopenharmony_ci    if(AutoSyncTime == 3) {
35513498266Sopenharmony_ci      /* Synchronising computer clock */
35613498266Sopenharmony_ci      if(!SetSystemTime(&SYSTime)) {  /* Set system time */
35713498266Sopenharmony_ci        fprintf(stderr, "ERROR: Unable to set system time.\n");
35813498266Sopenharmony_ci        RetValue = 1;
35913498266Sopenharmony_ci      }
36013498266Sopenharmony_ci      else {
36113498266Sopenharmony_ci        /* Successfully re-adjusted computer clock */
36213498266Sopenharmony_ci        GetLocalTime(&LOCALTime);
36313498266Sopenharmony_ci        snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
36413498266Sopenharmony_ci                 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
36513498266Sopenharmony_ci                 MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
36613498266Sopenharmony_ci                 LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
36713498266Sopenharmony_ci                 LOCALTime.wMilliseconds);
36813498266Sopenharmony_ci        fprintf(stderr, "\nNew System's Date: %s%s\n", timeBuf, tzoneBuf);
36913498266Sopenharmony_ci      }
37013498266Sopenharmony_ci    }
37113498266Sopenharmony_ci
37213498266Sopenharmony_ci    /* Cleanup before exit */
37313498266Sopenharmony_ci    conf_init(conf);
37413498266Sopenharmony_ci    curl_easy_cleanup(curl);
37513498266Sopenharmony_ci  }
37613498266Sopenharmony_ci  return RetValue;
37713498266Sopenharmony_ci}
378