Poco::LocalDateTime Not Accounting for Daylight Saving

Please post support and help requests here.
Poul
Posts: 2
Joined: 30 Oct 2013, 23:01

Poco::LocalDateTime Not Accounting for Daylight Saving

Postby Poul » 30 Oct 2013, 23:27

I have found that the Poco::LocalDateTime class does not adjust for daylight saving.
Below I have made a short example demonstrating the problem:

Code: Select all

#include <stdio.h>
#include <time.h>
#include <Poco/LocalDateTime.h>
#include <Poco/DateTimeFormatter.h>
#include <iostream>
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time (&rawtime);
  timeinfo = localtime (&rawtime);
  std::string c_time_str = asctime(timeinfo);
  time_t c_time = mktime(timeinfo);

  Poco::LocalDateTime localTime;
  std::string poco_time_str;
  Poco::DateTimeFormatter::append(poco_time_str, localTime, "%w %b %d %H:%M:%S %Y"); 
  time_t poco_time = localTime.timestamp().epochTime();

  std::cout << "C Time:\t\t" << c_time_str;
  std::cout << "Poco Time:\t" <<  poco_time_str << std::endl;
  std::cout << "C Timestamp:\t" <<  c_time << std::endl;
  std::cout << "Poco Timestamp:\t" <<  poco_time << std::endl;
  std::cout << "Difference:\t" << c_time - poco_time << std::endl;
  return 0;
}


The ouput is as follows:

Code: Select all

C Time:              Wed Oct 30 22:12:32 2013
Poco Time:           Wed Oct 30 21:12:32 2013
C Timestamp:         1383167552
Poco Timestamp:      1383171152
Difference:          -3600


The C time above is correct but Poco is one hour behind.
I don't know if this is a problem with the way I am initialising the Poco::LocalDateTime object or a bug in the library?

guenter
Posts: 1194
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Poco::LocalDateTime Not Accounting for Daylight Saving

Postby guenter » 31 Oct 2013, 14:36

The Poco::LocalDateTime class is correct, but there's a bug in the Poco::DateTimeFormatter::append() function for LocalDateTime.
Instead of

Code: Select all

void DateTimeFormatter::append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt)
{
   DateTimeFormatter::append(str, dateTime.utc(), fmt, dateTime.tzd());
}

it should be:

Code: Select all

void DateTimeFormatter::append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt)
{
   DateTimeFormatter::append(str, dateTime._dateTime, fmt, dateTime.tzd());
}

As a workaround, you can also use DateTimeFormatter::format().

guenter
Posts: 1194
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Poco::LocalDateTime Not Accounting for Daylight Saving

Postby guenter » 31 Oct 2013, 20:03

See <https://github.com/pocoproject/poco/issues/316>

Poul
Posts: 2
Joined: 30 Oct 2013, 23:01

Re: Poco::LocalDateTime Not Accounting for Daylight Saving

Postby Poul » 31 Oct 2013, 21:50

Thanks Guenter, that worked a treat.


Return to “Support”

Who is online

Users browsing this forum: No registered users and 1 guest

cron