Datetime localization in command line scripts


#1

Hi all, I am trying to process the ticket times, but have a trouble with Timezone. Final output is 2 hours before the ticket was actually created. Code is like this, commented lines are some experiments but everything behaves almost the same. If I set the Format to “unknown”, the time is 4 hours before correct ticket creation time. System date and timezone are set OK. Web GUI shows the time correctly.

use lib ("/opt/rt4/lib");
use 5.010;

use RT -init;

my $ticket = RT::Ticket->new($RT::SystemUser);
$ticket->Load(123456);

#say $ticket->Created;
my $date = RT::Date->new($RT::SystemUser);

#$date->Set(Format => ‘unknown’,Value => $ticket->Created);
#$date->Set(Format => ‘unknown’,Value => $ticket->Created,Timezone => “Europe/Prague”);
#$date->Set(Format => ‘datemanip’,Value => $ticket->Created);
#$date->Set(Format => ‘unix’,Value => $ticket->Created);
$date->Set(Format => ‘ISO’,Value => $ticket->Created);

say $date->ISO;
say $date->W3CDTF;
say $date->RFC2822;
say $date->RFC2616;

Any hint for that? :slight_smile:
Thank you, Petr


#2

What do you get for the say $ticket->Created line? Is it the time in UTC, or your locale timezone or something else? Might be easier to see if you run the script and show the output.

Also you might want to add a say $date->LocalizedDateTime(); and say $date->ISO(Timezone -> 'Europe/Prague'); to see what they show.


#3

Thank you, ok, let’s give it the try. First the code:

use lib ("/opt/rt4/lib");
use 5.010;

use RT -init;

my $ticket = RT::Ticket->new($RT::SystemUser);
$ticket->Load(123456);

print "system (date): ";
system (‘date’);
my $date = RT::Date->new($RT::SystemUser);
$date->SetToNow;
say "date->LocalizedDateTime(): " . $date->LocalizedDateTime();
say "date->ISO(Timezone => ‘Europe/Prague’): " . $date->ISO(Timezone => ‘Europe/Prague’);

say "ticket->Created: " . $ticket->Created;

say “date->Set(Format => ‘ISO’,Value => ticket->Created)”;
$date->Set(Format => ‘ISO’,Value => $ticket->Created);
say "date->LocalizedDateTime(): " . $date->LocalizedDateTime();
say "date->ISO(Timezone => ‘Europe/Prague’): " . $date->ISO(Timezone => ‘Europe/Prague’);

say "date->ISO: " .$date->ISO;
say "date->W3CDTF: " . $date->W3CDTF;
say "date->RFC2822: " . $date->RFC2822;
say "date->RFC2616: " . $date->RFC2616;

Then the output:

system (date): Wed Dec  5 11:24:59 CET 2018
date->LocalizedDateTime(): Wed, Dec 5, 2018 10:24:59 AM
date->ISO(Timezone => ‘Europe/Prague’): 2018-12-05 10:24:59
ticket->Created: 2018-01-17 12:53:09
date->Set(Format => ‘ISO’,Value => ticket->Created)
date->LocalizedDateTime(): Wed, Jan 17, 2018 12:53:09 PM
date->ISO(Timezone => ‘Europe/Prague’): 2018-01-17 12:53:09
date->ISO: 2018-01-17 12:53:09
date->W3CDTF: 2018-01-17T12:53:09Z
date->RFC2822: Wed, 17 Jan 2018 12:53:09 +0000
date->RFC2616: Wed, 17 Jan 2018 12:53:09 GMT

The ticket was created at 13:53:09. Still don’t know why the system date is an hour after the RT date :slight_smile: System date should be the correct one.


#4

Focusing up at the start, RT appears to think your local time is one hour before what your system seems to think it is. (I assume “Europe/Prague” is CET timezone at the moment as daylight saving shouldn’t apply?). What database are you using? If its mysql/mariadb, login on the SQL command line, switch the the RT database and then find out what the output from these commands are:

SELECT @@GLOBAL.time_zone, @@SESSION.time_zone;

select now()

compared to the system(date) at the moment? I’m wondering if the database is in a different timezone to the underlying system (maybe UTC for example).


#5

Hmm, not the case.

MariaDB [rtdb]> SELECT @@GLOBAL.time_zone, @@SESSION.time_zone;
±-------------------±--------------------+
| @@GLOBAL.time_zone | @@SESSION.time_zone |
±-------------------±--------------------+
| SYSTEM             | SYSTEM              |
±-------------------±--------------------+
1 row in set (0.00 sec)
MariaDB [rtdb]> select now();
±--------------------+
| now()               |
±--------------------+
| 2018-12-05 17:31:54 |
±--------------------+
1 row in set (0.00 sec)
MariaDB [rtdb]> Bye
# date
Wed Dec  5 17:31:55 CET 2018

I wonder why the web UI shows the time correctly. I am trying to do the same as in sub ProcessTicketDates in lib/RT/Interface/Web.pm but setting the Format to unknown gives 2 hours shift instead of one.