[odb-users] time_point to timestamp - pgsql?

Ryan Edin ryan at helloryan.se
Thu Sep 7 06:55:31 EDT 2023


Hi!

I would use the following type-mapping to store time_point as a uint64.

// Map a time_point (datetime) to uint64_t.
PRAGMA_DB(
  map
  type(datetime)
  as(std::uint64_t)
  to(::std::chrono::duration_cast<::std::chrono::milliseconds>((?).time_since_epoch()).count())
  from(datetime(std::chrono::duration_cast<datetime::duration>(::std::chrono::milliseconds(?))))
)

// Ryan

On Thu, Sep 7, 2023 at 12:38 PM Anurag Gupta <anuragg at miltenyi.com> wrote:
>
> Hello,
> I read somewhere that odb natively supports std::chrono::system_clock::time_point but can't find the reference anywhere. I am trying to save date time using time_point as following:
>
> using datetime = std::chrono::system_clock::time_point;
> #pragma db value (datetime) type ("timestamp") // Is this correct?
>
>
>
>
> #pragma db object
> someClass {
> ...
> ...
>
> private:
>
> datetime last_modified;
>
> datetime created_date_time;
>
> ...
> }
>
> And after generating odb support code (using odb CLI) - here is the code snippet from someClass-odb.cxx:
>
>           // last_modified
>
>     //
>
>     {
>
>       ::datetime const& v =
>
>         o.last_modified;
>
>
>       bool is_null (false);
>
>       pgsql::value_traits<
>
>           ::datetime,
>
>           pgsql::id_timestamp >::set_image ( //***** HERE I GET THE ERROR
>
>         i.last_modified_value, is_null, v);
>
>       i.last_modified_null = is_null;
>
>     }
>
>
>     // created_date_and_time
>
>     //
>
>     {
>
>       ::datetime const& v =
>
>         o.created_date_and_time;
>
>
>       bool is_null (false);
>
>       pgsql::value_traits<
>
>           ::datetime,
>
>           pgsql::id_timestamp >::set_image (
>
>         i.created_date_and_time_value, is_null, v);
>
>       i.created_date_and_time_null = is_null;
>
>     }
>
> Right when building it shows error:
>
> /usr/local/include/odb/pgsql/traits.hxx: In instantiation of ‘static void odb::pgsql::default_value_traits<T, <anonymous> >::set_image(image_type&, bool&, T) [with T = std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >; odb::pgsql::database_type_id ID = odb::pgsql::id_timestamp; image_type = long long int]’:
> ....../build/odb_gen/mbom_classes-odb.cxx:414:44:   required from here  //****** THIS LINE POINTS TO ABOVE SOURCE LINE
> /usr/local/include/odb/pgsql/traits.hxx:373:43: error: invalid cast from type ‘std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >’ to type ‘odb::pgsql::default_value_traits<std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >, odb::pgsql::id_timestamp>::image_type’ {aka ‘long long int’}
>   373 |         i = details::endian_traits::hton (image_type (v));
>       |                                           ^~~~~~~~~~~~~~
>
> What am I doing wrong? How can I use std::chrono::system_clock::time_point in my code that can be persisted to PGSQL?
>
> Thank you!
> Anurag Gupta
>
>
>



More information about the odb-users mailing list