Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to format dates prior to 1970?

by Hanfresco (Novice)
on Jul 10, 2017 at 13:07 UTC ( #1194689=perlquestion: print w/replies, xml ) Need Help??

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

#!/usr/bin/perl use v5.10; use strict; use warnings; use Data::Dumper; use DateTime::Format::Strptime; use Time::Piece; $Data::Dumper::Sortkeys = 1; my $date = '05/10/1969'; my $ymd = '%Y-%m-%d'; my $mdy = '%m/%d/%Y'; my $t = Time::Piece->strptime($date,$mdy); say $t->ymd; # Prints as normal say $t->strftime($ymd); # Segmentation fault
Hi All, I'm getting a segmentation fault from the code above trying to format a date. After digging around a bit I see that it only faults on dates prior to 1970 (perhaps because POSIX doesn't support pre-1970 dates?). My goal is to write a subroutine that can do the following and work for pre-1970 dates:
sub formatTime { my ($date, $in_format, $out_format) = @_; $t = Time::Piece->strptime($date,$in_format); return $t->strftime($out_format); }
My question is, how do you folks deal with this? If it matters, I'm on Windows 8 with Strawberry Perl v5.26.0.1 64bit.

Replies are listed 'Best First'.
Re: How to format dates prior to 1970?
by Corion (Pope) on Jul 10, 2017 at 13:33 UTC

    Works for me:

    > perl -MTime::Piece -wE "say $Time::Piece::VERSION; my $t=Time::Piece +->strptime('05/10/1969','%m/%d/%Y'); say $t->ymd; say $t->strftime('% +Y-%m-%d')" 1.20_01 1969-05-10 1969-05-10
    > perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x +86-multi-thread
Re: How to format dates prior to 1970?
by hippo (Chancellor) on Jul 10, 2017 at 13:21 UTC

    I, too, am unable to reproduce your segfault using perl 5.20.3 on 64-bit Linux. Maybe it's a Windows thing?

    • DateTime 1.20
    • Time::Piece 1.31_04
      I really hope it's not a Windows thing, but will try on Linux when I get a chance and report back :/

        Which version of Time::Piece are you running? The changelog has an entry for 1.30_01 which says "Fix windows mem corruption". Maybe just needs an upgrade?

Re: How to format dates prior to 1970?
by NetWallah (Canon) on Jul 10, 2017 at 17:40 UTC
    Perl 5.24 on win7/64-bit blows up for me:
    perl -v This is perl 5, version 24, subversion 0 (v5.24.0) built for MSWin32-x +64-multi-thread >perl -MTime::Piece -wE "say $Time::Piece::VERSION; my $t=Time::Piece- +>strptime('05/10/1969','%m/%d/%Y'); say $t->ymd; say $t->strftime('%Y +-%m-%d')" 1.31 1969-05-10 <<< *popup* - perl stopped responding
    Faulting application name: perl.exe, version: 5.24.0.2400, time stamp: + 0x575a1be0 Faulting module name: Piece.dll, version: 0.0.0.0, time stamp: 0x575a1 +c19 Exception code: 0xc0000005 Fault offset: 0x00000000000022e9 Faulting process id: 0x44b0 Faulting application start time: 0x01d2f9a2b44679e1 Faulting application path: C:\Perl64\bin\perl.exe Faulting module path: C:\Perl64\lib\auto\Time\Piece\Piece.dll Report Id: 7213cb17-38fa-4446-823a-d4990b1501b7 Faulting package full name: Faulting package-relative application ID:

                    All power corrupts, but we need electricity.

      Hope this gets fixed someday!

        The first step on that road would be to submit a bug report against Time::Piece. I don't see one there yet.

Re: How to format dates prior to 1970?
by 1nickt (Abbot) on Jul 10, 2017 at 13:11 UTC

    Your code works for me on both Perl 5.22 on MacOS X and on Perl 5.16.3 on Ubuntu Linux:

    $ perl 1194689.pl 1969-05-10 1969-05-10


    The way forward always starts with a minimal test.
      Thanks for the response. But that is bizarre. I get a Windows pop-up that says
      Perl interpreter has stopped working A problem caused the program to stop working correctly. Windows will c +lose the program and notify you if a solution is available.
      What system are you running the code on? If I click "Debug" and open this with Microsoft Visual Studio, I get
      Unhandled exception at 0x000000006F4C230B (Piece.xs.dll) in perl.exe: +0xC0000005: Access violation reading location 0x0000000000000000. If there is a handler for this exception, the program may be safely co +ntinued.

        Just FWIW: problem occurs with with AS 5.24 on Win 7-64:

        C:\>perl -v * This is perl 5, version 24, subversion 0 (v5.24.0) built for MSWin32-x86-multi-thread-64int on Win 7 pro, 64bit
        #!/usr/bin/perl use v5.10; # (OP's code, verbatim) use strict; use warnings; use Data::Dumper; use DateTime::Format::Strptime; use Time::Piece; $Data::Dumper::Sortkeys = 1; my $date = '05/10/1969'; my $ymd = '%Y-%m-%d'; my $mdy = '%m/%d/%Y'; my $t = Time::Piece->strptime($date,$mdy); say $t->ymd; # Prints as normal say $t->strftime($ymd); # Segmentation fault

        Execution:

        C:\>F:\_Perl_\PMonks\1194689.pl 1969-05-10 C:\> And, the same popup as cited above with the following content: Problem signature: Problem Event Name: APPCRASH Application Name: perl.exe Application Version: 5.24.0.2400 Application Timestamp: 578689b9 Fault Module Name: Piece.dll Fault Module Version: 0.0.0.0 Fault Module Timestamp: 57868a01 Exception Code: c0000005 Exception Offset: 00002398 OS Version: 6.1.7601.2.1.0.256.48 Locale ID: 1033 Additional Information 1: 0a9e Additional Information 2: 0a9e372d3b4ad19135b953a78882e789 Additional Information 3: 0a9e Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

        Well, since the exception is apparently caused by Time::Piece (even though it's a core module), try using something else:

        use strict; use warnings; use feature 'say'; use DateTime::Format::Strptime; my $parser = DateTime::Format::Strptime->new(pattern => '%m/%d/%Y', on +_error => 'croak'); my $date = '05/10/1969'; my $dt = $parser->parse_datetime( $date ); say $dt->ymd;
        What does that do on your Windows system?


        The way forward always starts with a minimal test.
Re: How to format dates prior to 1970?
by karthikkumar (Initiate) on Nov 30, 2018 at 14:29 UTC

    This Time::Piece strftime, seems problem in windows OS even now with version 1.33

    So alternate functions available, which works well.

    Example this is not giving any output:

    $t->strftime("%b %d %Y, %A [%H:%M:%S]")

    So, I am re-writing without strftime, and this is working fine for me even on windows OS.

    $t->monname . " " . sprintf("%.2d", $t->mday) . " " . $t->year . ", " . $t->fullday . " [" . $t->hms . "]"

      Hi, you probably still don't want to do that by hand. Try DateTime instead?


      The way forward always starts with a minimal test.
Re: How to format dates prior to 1970?
by thechartist (Monk) on Dec 01, 2018 at 03:32 UTC

    I ran this on Windows 10 and did not experience any error. I am running perl 5.26.1 build for MSWin32-x64-multi-thread, module version 1.3202

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1194689]
Approved by 1nickt
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-10-31 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (290 votes). Check out past polls.

    Notices?