Overview
Features
Download
Documentation
Community
Add-Ons & Services

DateTime vs LocalDateTime

Please post support and help requests here.

DateTime vs LocalDateTime

Postby ajaimes » 27 Feb 2013, 06:43

Hello,

I don't really understand the difference between DateTime and LocalDateTime. When I execute the following code DateTime always gives me one day ahead, while LocalDateTime always give the correct date. However, I need to use DateTime's because I'm using MySQL and if I use LocalDateTimes dates usually go crazy, and that does not happen with DateTimes. How can I make DateTime give me the correct date in the following code?

Thanks...

Code: Select all
#include <iostream>
#include <Poco/DateTime.h>
#include <Poco/DateTimeFormatter.h>

int main() {
   Poco::DateTime d;
   std::cout << Poco::DateTimeFormatter::format(d, "%d/%m/%Y") << std::endl;
   return 0;
}


Edit: Ok, the problem was related to Time Zones. I think I now understand how they work. I don't really need to use LocalDateTime. I'm posting a solution... would you please confirm I'm using it the right way:

Code: Select all
#include <iostream>
#include <string>
#include <Poco/DateTime.h>
#include <Poco/DateTimeFormat.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/DateTimeParser.h>
#include <Poco/Timezone.h>

class DateTime {
private:
   int tzd;
   Poco::DateTime dateTime;
public:
   DateTime() {
      tzd = Poco::Timezone::tzd();
      dateTime.makeLocal(tzd);
   }

   std::string str() {
      return Poco::DateTimeFormatter::format(dateTime, Poco::DateTimeFormat::HTTP_FORMAT, tzd);
   }

   bool parse(std::string date) {
      int tzd;
      return Poco::DateTimeParser::tryParse("%d/%m/%Y", date, dateTime, tzd);
   }
};

int main() {
   DateTime d;
   std::cout << d.str() << std::endl;

   d.parse("27/02/2013");
   std::cout << d.str() << std::endl;

   return 0;
}


The program outputs this:

Code: Select all
Wed, 27 Feb 2013 11:01:54 -0600
Wed, 27 Feb 2013 00:00:00 -0600


Thank you
ajaimes
 
Posts: 2
Joined: 27 Feb 2013, 06:38

Re: DateTime vs LocalDateTime

Postby ajaimes » 27 Feb 2013, 19:47

Yes!, it seems to work...
ajaimes
 
Posts: 2
Joined: 27 Feb 2013, 06:38


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests

cron