Perl DBIx :: Class: get current time from database

Here is my problem:

I want to calculate how long the record has been updated in the database.

The database is in PostgreSQL, the update_time field is populated with the trigger that it uses CURRENT_TIMESTAMP(2). The field is inflated to a DateTimeDBIx :: Class object . I get the current time in my code usingDateTime->now()

My problem is that when I get the field value, it turns off 1 hour (i.e. 1 hour earlier DateTime->now()). I am in the CET time zone, therefore 1 hour earlier than UTC.

The correct way to solve the problem is probably at the database level. I tried replacing CURRENT_TIMESTAMPwith LOCALTIMESTAMP, but to no avail.

I think that in fact a more reliable solution (that is, one that does not rely on getting the right to the database) will be to get the current timestamp from the database itself. I really need an era since I use it to calculate the difference.

So the question is: is there an easy way to do this: get the current time from the database using DBIx :: Class?

Another way to get DB and DateTime to agree is that the current time will also be OK!

+4
source share
2 answers

You can use dbh_dofrom your DBIx :: Class :: Storage to run arbitrary queries. It’s easy SELECT CURRENT_TIMESTAMP.

my ( $timestamp ) = $schema->storage->dbh_do(
  sub {
    my ($storage, $dbh) = @_;

    $dbh->selectrow_array("SELECT CURRENT_TIMESTAMP");
  },
);
+1
source

. , , , / (NOT NULL). , UTC . , - , DateTime .

+1

Source: https://habr.com/ru/post/1673007/


All Articles