Thread: [Cppcms-users] sqlite3 CURRENT_TIMESTAMP compare to date_time
Brought to you by:
artyom-beilis
From: <ele...@ex...> - 2012-05-17 10:40:06
|
Hi, Just a quick question. Given a column in an sqlite database is set to DEFAULT CURRENT_TIMESTAMP, which is stored in UTC, and not local time and an SQL SELECT such as SELECT * FROM table WHERE created_at < ? and a locale::date_time object representing current time, should date_time be first converted to UTC before being sent to the SQL query? ie. ostringstream os; os << as::ftime("%Y-%m-%d %H:%M:S") << as::timezone("UTC") << now; 1) Is this correct? 2) What if the column is not a timestamp but a date, and the query is comparing date with date_time. Should conversion to UTC be still done? Thanks, Petr |
From: Artyom B. <art...@ya...> - 2012-05-17 11:05:19
|
> Hi, > > Just a quick question. > > Given a column in an sqlite database is set to DEFAULT CURRENT_TIMESTAMP, > which is stored in UTC, and not local time and an SQL SELECT such as > > SELECT * FROM table WHERE created_at < ? > > and a locale::date_time object representing current time, should date_time > be first converted to UTC before being sent to the SQL query? > Yes... if you use cppdb you can do it by sql << "select ... ?" << booster::ptime::universal_time(booster::ptime::now()) << ... > ie. > > ostringstream os; > > os << as::ftime("%Y-%m-%d %H:%M:S") << > as::timezone("UTC") << now; > > 1) Is this correct? > Yes it is, however it is better to use as::gmt rather that as::timezone("UTC"); > 2) What if the column is not a timestamp but a date, and the query is > comparing date with date_time. Should conversion to UTC be still done? This is good question. The problem is in what time zone you refer to date. Usually date is meaningless out of timezone scope. What does 2011-03-04 mean? If it is in GMT time in certain time zones it would be from 7 AM to 7 AM So you likely do not want to store dates in UTC, if you store dates in UTC you likely need to store entire timestamp. > > Thanks, > Petr Artyom |
From: <ele...@ex...> - 2012-05-17 22:45:29
|
Thanks Artyom, > sql << "select ... ?" << > booster::ptime::universal_time(booster::ptime::now()) << ... I didnt notice there was an overloaded operator<< for ptime. Can we add something similar for date_time? Petr |
From: Artyom B. <art...@ya...> - 2012-05-18 05:04:24
|
it is not ptime, booster::ptime::universal_time converts ptime to std::tm and vise versa. The overload is for std::tm Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ >________________________________ > From: "ele...@ex..." <ele...@ex...> >To: Artyom Beilis <art...@ya...>; cpp...@li... >Sent: Friday, May 18, 2012 1:45 AM >Subject: Re: [Cppcms-users] sqlite3 CURRENT_TIMESTAMP compare to date_time > >Thanks Artyom, > > >> sql << "select ... ?" << >> booster::ptime::universal_time(booster::ptime::now()) << ... > >I didnt notice there was an overloaded operator<< for ptime. Can we add >something similar for date_time? > >Petr > > >------------------------------------------------------------------------------ >Live Security Virtual Conference >Exclusive live event will cover all the ways today's security and >threat landscape has changed and how IT managers can respond. Discussions >will include endpoint security, mobile security and the latest in malware >threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ >_______________________________________________ >Cppcms-users mailing list >Cpp...@li... >https://lists.sourceforge.net/lists/listinfo/cppcms-users > > > |
From: <ele...@ex...> - 2012-05-18 09:40:17
|
> it is not ptime, booster::ptime::universal_time converts ptime to std::tm > and vise versa. The overload > is for std::tm > I see, but the question stays - whats the best way to convert date_time to std::tm? I'd rather not mix both ptime and date_time in the project. |
From: Artyom B. <art...@ya...> - 2012-05-21 12:45:19
|
You can convert booster::locale::date_time to tm by selecting individual fields. Currently there is no automatic conversion as converting date_time to std::tm have meaningful when Gregorian calendar is used. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ >________________________________ > From: "ele...@ex..." <ele...@ex...> >To: Artyom Beilis <art...@ya...>; cpp...@li... >Sent: Friday, May 18, 2012 12:40 PM >Subject: Re: [Cppcms-users] sqlite3 CURRENT_TIMESTAMP compare to date_time > >> it is not ptime, booster::ptime::universal_time converts ptime to std::tm >> and vise versa. The overload >> is for std::tm >> > >I see, but the question stays - whats the best way to convert date_time to >std::tm? I'd rather not mix both ptime and date_time in the project. > > >------------------------------------------------------------------------------ >Live Security Virtual Conference >Exclusive live event will cover all the ways today's security and >threat landscape has changed and how IT managers can respond. Discussions >will include endpoint security, mobile security and the latest in malware >threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ >_______________________________________________ >Cppcms-users mailing list >Cpp...@li... >https://lists.sourceforge.net/lists/listinfo/cppcms-users > > > |