Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Using Time::Piece Strptime

by 1nickt (Canon)
on Jan 26, 2016 at 01:08 UTC ( [id://1153628]=note: print w/replies, xml ) Need Help??


in reply to Using Time::Piece Strptime

"I would like it to print Friday Mar 11, 2016."

strptime() is for parsing datetime strings.

strftime() is for formatting datetime strings.

You can get the output you want by using Time::Piece's strftime() with the appropriate POSIX strftime pattern. Note the chained object methods so you can parse and format in one statement:

$ perl -MTime::Piece -E' my $date = "3/11/16"; say Time::Piece->strptime($date, "%D")->strftime("%A %b %e, %Y"); '
Output:
Friday Mar 11, 2016

Regarding your existing use of strptime: as Mr. Muskrat points out, the pattern %D is a shortcut for %m/%d/%y.

At a guess I would say your input looks like 3/11/16 and the date is being parsed by %D -- the rest of the patterns are ignored. You probably thought %D was 'day' and '%M' was month etc.

Hope this helps!


Update: moved the lead above the fold
The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Using Time::Piece Strptime
by stevieb (Canon) on Jan 26, 2016 at 01:43 UTC

    Don't assume the input. I think it's more important to show that the return is an object, and that the output can be whatever manipulation one wants :)

    The object will print out a date if it didn't fail, so the input is clearly legit in this case. OP just needs to know how to dump it properly.

      ??

      The code I posted shows how to get the desired output specified in the OP.

      It also addresses the input pattern issue, since I believe it is unlikely that the input data is actually like 3/11/16, 00, Mar, 2016 and that it's more likely the OP made an error with his pattern that his input data masked.

      Update: reply to (unmarked) addition above:

      "The object will print out a date if it didn't fail, so the input is clearly legit in this case. OP just needs to know how to dump it properly."

      Actually I would say it's rather important to get the right parsed datetime, not simply that "the object ... didn't fail." It's not the input that's at issue, it's the pattern used to parse the input.


      The way forward always starts with a minimal test.
        We won't know unless OP provides input data. I'm always up to learning, and being corrected.

        You could use regexpression

        #!/usr/bin/perl -w use Time::Piece; use strict; my $date = '3/11/16, 00, Mar, 2016'; Time::Piece->strptime($date, '%D, %M, %b, %Y') =~ /^(.+)\d\d:\d\d:\d\d + (.+)$/ ; my $date1 =join ' ',$1,$2; print "$date1\n";
Re^2: Using Time::Piece Strptime
by GrorkGnom (Initiate) on Jan 26, 2016 at 15:19 UTC

    Here's the code piece I am working with:

    my $dfile = 'd1528235.txt'; open my $dfh, '<', $dfile; my $date; while ( <$dfh> ) { chomp; if ( not $date and $date = join '/', /=\((\d\d)(\d\d)(\d\d)/ ) { my $str1 = '$date'; my $date1; $date1 = Time::Piece->strptime($date, '%D, %M, %b, %Y') ; $worksheet->merge_range('A2:G2',$date1,$format7,);

    This is what I have actually gotten to work and it works pretty well except it gives me the default time in the middle displaying out 00:00:00 between the Day and the Year.

      Please post a working code snippet and a sample of the input data file if you want help cleaning up your strptime pattern. (see How do I post a question effectively?)

      You can, in any case, eliminate the undesired time part of your output with:

      my $date_sans_time = $date1->strftime("%A %b %e, %Y");
      as I showed above.


      update: added link
      The way forward always starts with a minimal test.

        THANK YOU, THANK YOU, THANK YOU! With all the information you have shown me here and using the CPAN info for strptime and strftime, I've got my date displaying correctly now in my spreadsheet. What a pain!

        Here is the final code that did it. I had to do some manipulating to get it to finally work. Incidently, the raw date I was using from the text file is a Julian calendar date. I generated it from a mainframe job that outputs a set of commands to a text file.

        my $dfile = 'd1528235.txt'; open my $dfh, '<', $dfile; my $date; while ( <$dfh> ) { chomp; if ( not $date and $date = join '/', /=\((\d\d)(\d\d)(\d\d)/ ) { my $str1 = '$date'; my $date1; $date1 = Time::Piece->strptime($date, '%D, %M, %dhd, %Y') ; my $date2 = $date1->strftime("%A %b %d, %Y"); $worksheet->merge_range('A2:G2',$date2,$format5,);

      Date formats in excel can be tricky. See Excel::Writer::XLSX::Utility

      #!perl use strict; use Excel::Writer::XLSX; use Excel::Writer::XLSX::Utility; use Time::Piece; my $wb = Excel::Writer::XLSX->new( 'dates.xlsx' ); my $ws = $wb->add_worksheet(); my $fmt = $wb->add_format(); $fmt->set_num_format( 'dddd mmm dd, yyyy' ); my $date = '3/15/16, 00, Mar, 2016'; my $t = Time::Piece->strptime($date, '%D, %M, %b, %Y') ; my $xldate = xl_date_list($t->year, $t->mon, $t->mday); $ws->write( 0, 0, $xldate, $fmt ); $wb->close();
      poj
Re^2: Using Time::Piece Strptime
by GrorkGnom (Initiate) on Jan 27, 2016 at 15:35 UTC
    1nickt,

    To get it into a variable, I used both strptime, then passed that variable into a second one using strftime to format and print the date to a cell in Excel. Looking back, this set of scripts I have is quite complex for what it is doing. Glad I was able to get it to work. My date now shows up in Excel correct as i.e. Friday Jan 27, 2016.

    I found that there are a lot of different variables to strptime using Time::Piece as well as strftime. Just had to figure out how to pass the output of it to where I wanted it.

      I'm very glad you were able to get your script working.

      The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-04-18 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found