delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2010/05/26/05:24:15

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=2.2 required=5.0 tests=AWL,BAYES_00,CHARSET_FARAWAY_HEADER
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
Date: Wed, 26 May 2010 12:23:54 +0300
Message-ID: <AANLkTinhHkaC_eMAcMTykCo-YLrPvzjS-8cTSeNAMIZC@mail.gmail.com>
Subject: 1.7.5-1: problem with settimeofday() & gettimeofday()
From: =?KOI8-R?B?/sXLIP7Fy8nT1A==?= <4ekuct AT tut DOT by>
To: cygwin AT cygwin DOT com
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Hello. It seems that I found a problem with settimeofday() and
gettimeofday() calls in a single context. The problem is that if we
set a new time with settimeofday() call it completes succefully and
system time is updated. But then if we try to get current time with
gettimeofday() call it returns old time!

The following example could be used:

#include <stdio.h>
#include <sys/time.h>
int main(int argc, char* argv[])
{
  struct timeval tv;
  struct tm time_to_set;

  // Get current date & time since Epoch.
  if (gettimeofday(&tv, NULL) != 0)
  {
    printf("Cannot get current date & time since Epoch.");
  }

  // Calculate local time.
  if (localtime_r(&tv.tv_sec, &time_to_set) != &time_to_set)
  {
    printf("Cannot convert file time to local file time.");
  }

  printf("Old time: %d.%d.%d - %d:%d:%d\r\n", time_to_set.tm_year +
1900, time_to_set.tm_mon + 1, time_to_set.tm_mday,
time_to_set.tm_hour, time_to_set.tm_min, time_to_set.tm_sec);

  time_to_set.tm_hour = 20;
  time_to_set.tm_min = 33;
  time_to_set.tm_sec = 11;
  time_to_set.tm_year = 2010 - 1900;
  time_to_set.tm_mon = 1;
  time_to_set.tm_mday = 4 + 1;

  // Make new system time.
  if ((tv.tv_sec = mktime(&time_to_set)) == (time_t)-1)
  {
    printf("Cannot convert system time");
  }
  tv.tv_usec = 0;

  // Set new system time.
  if (settimeofday(&tv, NULL) != 0)
  {
    printf("Cannot set system time");
  }

  // Get current date & time since Epoch.
  if (gettimeofday(&tv, NULL) != 0)
  {
    printf("Cannot get current date & time since Epoch.");
  }

  // Calculate local time.
  if (localtime_r(&tv.tv_sec, &time_to_set) != &time_to_set)
  {
    printf("Cannot convert file time to local file time.");
  }

  printf("New time (2010.2.5 - 20:33:11): %d.%d.%d - %d:%d:%d\r\n",
time_to_set.tm_year + 1900, time_to_set.tm_mon + 1,
time_to_set.tm_mday, time_to_set.tm_hour, time_to_set.tm_min,
time_to_set.tm_sec);

  return 0;
}

In my case the output is:

Old time: 2010.5.26 - 12:47:23
New time (2010.2.5 - 20:33:11): 2010.5.26 - 12:47:23

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019