suba has asked for the wisdom of the Perl Monks concerning the following question:

How does a Time::Piece Object get marked as being local time?
$date_string = "Jun 03 19:49:27 2018"; $fmt = '%b %d %H:%M:%S %Y'; $time=Time::Piece->strptime( $date_string, $fmt ); print $time->strftime() ."\n";
gives me
So, 03 Jun 2018 19:49:27 UTC
No surprise. I would like this to be local though.
my $date_string = "Jun 03 19:49:27 2018 +0200"; my $fmt = '%b %d %H:%M:%S %Y %z'; my $time=Time::Piece->strptime( $date_string, $fmt ); print $time->strftime() ."\n";
does not do the trick
So, 03 Jun 2018 19:49:27 UTC
After looking into the module itself I find that there is a flag marking the object as local. If I set it manually, I get the result I want.
$date_string = "Jun 03 19:49:27 2018"; $fmt = '%b %d %H:%M:%S %Y'; $time=Time::Piece->strptime( $date_string, $fmt ); $time->[10] = 1; print $time->strftime() ."\n"; ... So, 03 Jun 2018 19:49:27 CEST
Looks ugly though, is there any better way to get there? Thanks for any hint.

Replies are listed 'Best First'.
Re: Time::Piece and Timzone?
by hippo (Archbishop) on Jun 05, 2018 at 14:03 UTC

    Your second option works fine for me (using Time::Piece 1.31_04):

    $ perl -MTime::Piece my $date_string = "Jun 03 19:49:27 2018 +0200"; my $fmt = '%b %d %H:%M:%S %Y %z'; my $time=Time::Piece->strptime( $date_string, $fmt ); print $time->strftime() ."\n"; Sun, 03 Jun 2018 17:49:27 UTC

    So I guess the strptime() implementation in your O/S differs from mine. Which O/S are you using?

      You are correct, code runs just fine on a OS-X machine.
      Had a look at the changelog for Time::Piece, 1.15 seems to be too old...

      1.16 - Implement %z for the internal implementation of strptime().

      Thanks for the help

        Yes, it does now. However in the somewhat older versions (prior to 1.17) such as the one used by suba on an RHEL6 box, that wasn't the case.

      Linux (RedHat 6), perl5.10.1, Time::Piece v1.15
        Red Hat 6 is also old.
Re: Time::Piece and Timzone?
by thanos1983 (Parson) on Jun 05, 2018 at 16:42 UTC

    Hello suba,

    Your answer has been already answered but just to add another module in case that you are interested.

    Sample bellow from Date::Manip:

    #!/usr/bin/env perl use strict; use warnings; use Date::Manip; use feature 'say'; my $date_string = "Jun 03 19:49:27 2018"; say UnixDate($date_string, "Time: %T Month: %b Day: %e Year: %Y Timezo +ne: %Z"); say UnixDate("now", "Time: %T Month: %b Day: %e Year: %Y Timezone: %Z" +); __END__ $ perl test.pl Time: 19:49:27 Month: Jun Day: 3 Year: 2018 Timezone: CEST Time: 18:40:13 Month: Jun Day: 5 Year: 2018 Timezone: CEST

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Time::Piece and Timzone?
by ikegami (Patriarch) on Jun 06, 2018 at 07:37 UTC

    You can use the following to construct a local time:

    localtime->strptime(...)

    The ability to do this was only added relatively recently.