# Please, correct (bug) reading date/time from PostgreSQL (TPgSQLStatement)

**URL:** <https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361>\
**Category:** ROOT\
**Created:** [June 16, 2016, 1:55pm UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361 "2016-06-16T13:55:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gertsen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gertsen/32/15699_2.png) [@gertsen](https://root-forum.cern.ch/u/gertsen)\
**Post date:** [June 16, 2016, 1:55pm UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/1 "2016-06-16T13:55:22Z")

</div>

Good afternoon.  
I’m using ROOT (ROOT 5.34.36) classes to work with our PostgreSQL database (8.4).  
I write TDatime variable to the database and then immediately read it back via TSQLStatement::SetDatime/GetDatime functions. But it returns another date/time value, it’s different for 16 hours.  
It’s concerned with wrong "void TPgSQLStatement::ConvertTimeToUTC(const TString &PQvalue, Int\_t& year, Int\_t& month, Int\_t& day, Int\_t& hour, Int\_t& min, Int\_t& sec) " function.  
It checks +/- UTC and finds the minus inside the date part, e.g. “2004-10-19 10:23:54” (compare with "“2004-10-19 10:23:54-02"”). Then it checks position of the minus relative ‘.’. But there is no any ‘.’  
I propose to use ‘:’ symbol (using in the time part) - it works.  
i.e.  
instead of:

```auto
void TPgSQLStatement::ConvertTimeToUTC(const TString &PQvalue, Int_t& year, Int_t& month, Int_t& day, Int_t& hour, Int_t& min, Int_t& sec)
{
   // Convert timestamp value to UTC if a zone is included.

   Ssiz_t p = PQvalue.Last('.');

```

use:

```auto
void TPgSQLStatement::ConvertTimeToUTC(const TString &PQvalue, Int_t& year, Int_t& month, Int_t& day, Int_t& hour, Int_t& min, Int_t& sec)
{
   // Convert timestamp value to UTC if a zone is included.

   Ssiz_t p = PQvalue.Last(':');

```

With respect, Konstantin.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 16, 2016, 6:09pm UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/2 "2016-06-16T18:09:46Z")

</div>

Hi Konstantin,

thanks for the report and the fix proposal.  
This will be tackled very soon. I’ll keep you posted.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![gertsen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gertsen/32/15699_2.png) [@gertsen](https://root-forum.cern.ch/u/gertsen)\
**Post date:** [February 16, 2017, 7:57am UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/3 "2017-02-16T07:57:51Z")

</div>

Good morning.  
Have you corrected this bug in the ROOT repo?  
I don’t see it in the patches.  
Thank you

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [February 16, 2017, 12:20pm UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/4 "2017-02-16T12:20:15Z")

</div>

Hi Konstantin,

unfortunately not.  
Could you share the test for this feature?

Danilo

---

<div class="post-metadata">

**Author:** ![gertsen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gertsen/32/15699_2.png) [@gertsen](https://root-forum.cern.ch/u/gertsen)\
**Post date:** [February 17, 2017, 9:21am UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/5 "2017-02-17T09:21:35Z")

</div>

Hi.  
Excuse me, how can I prepare the test? Should I write the special script deploying PostgreSQL libraries and test database with test inserting?

P.S. e.g. I have datetimes for run #930 from 2016-12-21 15:13:22 to 2016-12-21 15:28:23 in my PostgreSQL database (screenshot from pgadmin attached), but TSqlStatement::GetDatime returns “Tue Dec 20 18:13:22 2016” and “Tue Dec 20 18:28:23 2016” because of this bug.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/3/32bd51393c5a53aadade1dc279e666cdb6adb4ba.png)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [February 17, 2017, 1:40pm UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/6 "2017-02-17T13:40:42Z")

</div>

Hi,

one way would be to provide all the input and the code which allows to recreate the issue in an isolated environment.

D

---

<div class="post-metadata">

**Author:** ![gertsen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gertsen/32/15699_2.png) [@gertsen](https://root-forum.cern.ch/u/gertsen)\
**Post date:** [February 5, 2018, 9:26am UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/7 "2018-02-05T09:26:51Z")

</div>

Hi.  
This bug is still remaining in the last ROOT 6 version for the last years. Unfortunately, I have to patch our experiment software every installation to exclude this bug.

So, I try to clear this bug one more time:  
In Postgresql we have the following timestamp format without timezone: YYYY-MM-DD HH:MM::SS (e.g. 2016-12-21 15:13:22), as it is shown on the above picture.  
When we get the datime from the PostgreSQL database, we get the wrong value every time. It happens because of the following bug in TPgSQLStatement.cxx.  
We use:

> Bool\_t TPgSQLStatement::GetDatime(Int\_t npar, Int\_t& year, Int\_t& month, Int\_t& day, Int\_t& hour, Int\_t& min, Int\_t& sec)  
> {  
> TString val=PQgetvalue(fStmt-\>fRes,fIterationCount,npar);  
> TDatime d = TDatime(val.Data());  
> year = d.GetYear();  
> month = d.GetMonth();  
> day= d.GetDay();  
> hour = d.GetHour();  
> min = d.GetMinute();  
> sec= d.GetSecond();  
> ConvertTimeToUTC(val, year, month, day, hour, min, sec);  
> return kTRUE;  
> }

It is ok but it uses ConvertTimeToUTC function:

> void TPgSQLStatement::ConvertTimeToUTC(const TString &PQvalue, Int\_t& year, Int\_t& month, Int\_t& day, Int\_t& hour, Int\_t& min, Int\_t& sec)  
> {  
> Ssiz\_t p = PQvalue.Last(‘.’);  
> // Check if timestamp has timezone  
> TSubString \*s\_zone = nullptr;  
> Bool\_t hasZone = kFALSE;  
> Ssiz\_t tzP = PQvalue.Last(‘+’);  
> if ((tzP != kNPOS) && (tzP \> p) ) {  
> s\_zone = new TSubString(PQvalue(tzP+1,PQvalue.Length()-tzP));  
> hasZone=kTRUE;  
> } else {  
> Ssiz\_t tzM = PQvalue.Last(‘-’);  
> if ((tzM != kNPOS) && (tzM \> p) ) {  
> s\_zone = new TSubString(PQvalue(tzM+1,PQvalue.Length()-tzM));  
> hasZone = kTRUE;  
> }  
> }  
> if (hasZone == kTRUE) {  
> …

What can we see:  
First, _p_ variable is equal the position of the point (‘.’) in the datetime string. We have no points, so _p_ == npos (-1).  
Then the function searches for the last plus (‘+’) symbol. It is absent, so then it searches for the last minus (‘-’) symbol and it is found in the date part (YYYY-MM-DD). Ok, now the conditions ( **tzM != kNPOS is true** because we have found ‘-’) and ( **tzP \> p is true** because _p_ is equal -1) are true, so:

> hasZone flag is set to kTRUE!

Then in condition: if (hasZone == kTRUE) - the time is shifted but it is absolutely wrong because we have no timezone in our case.

Thank you

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [July 10, 2018, 11:17am UTC](https://root-forum.cern.ch/t/please-correct-bug-reading-date-time-from-postgresql-tpgsqlstatement/21361/8 "2018-07-10T11:17:31Z")

</div>

See [https://sft.its.cern.ch/jira/browse/ROOT-9524](https://sft.its.cern.ch/jira/browse/ROOT-9524) - but I believe it has been addressed already by Danilo, and the patch is in v6.14.
