This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

1.7.5-1: problem with settimeofday() & gettimeofday()


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]