Re: Reading timestamp values from Datums gives garbage values - Mailing list pgsql-hackers

From Aleksander Alekseev
Subject Re: Reading timestamp values from Datums gives garbage values
Date
Msg-id CAJ7c6TOtm0q4f_jMTjR+KoFrb-GJ=r0VJzUw2YKz89oSRSP=FA@mail.gmail.com
Whole thread Raw
In response to Reading timestamp values from Datums gives garbage values  (Sushrut Shivaswamy <sushrut.shivaswamy@gmail.com>)
Responses Re: Reading timestamp values from Datums gives garbage values
List pgsql-hackers
Hi,

> When trying to read the query response from the Datum, I get garbage values.
> I've tried various types and none of them read the correct value.
> ```
>
> Datum current_timestamp = SPI_getbinval(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, 5, &isnull);
>
> double current_time = DatumGetFloat8(current_timestamp); // prints 0
>
> int64 time = DatumGetUint64(current_timestamp); // prints 5293917674
>
> ```
>
> Can you help me out with the correct way to read EPOCH values from datums?

I don't entirely understand why you are using DatumGetFloat8() /
DatumGetUint64() and double / int64 types. There are
DatumGetTimestamp() / DatumGetTimestampTz() and Timestamp /
TimestampTz.

I recommend using the PostgreSQL code as a source of more examples of
how to deal with the given types. The file pg_proc.dat is a good entry
point. See also commit 260a1f18 [1] and PostgreSQL documentation [2].

[1]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=260a1f18
[2]: https://www.postgresql.org/docs/16/xfunc-c.html

-- 
Best regards,
Aleksander Alekseev



pgsql-hackers by date:

Previous
From: Dave Page
Date:
Subject: Re: zlib detection in Meson on Windows broken?
Next
From: John Naylor
Date:
Subject: Re: PostgreSQL 17 Beta 1 release announcement draft