Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Date/Time Manipulation

by RobertJ (Acolyte)
on May 24, 2016 at 18:01 UTC ( [id://1164007]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, been a while since my last visit.

Looking for a simple (hopefully) way of subtracting one hour from date - time in a file.

The file is a text file of the following format:

2016-05-16 10:51:07 random text1

2016-05-16 11:58:37 random text2

2016-05-16 12:01:49 random text3

All the times are during the daytime so I do not have to worry about "role-over" at midnight.

I simply want to change the above to

2016-05-16 09:51:07 random text1

2016-05-16 10:58:37 random text2

2016-05-16 11:01:49 random text3

I have BBEdit which will run Perl and on a Mac with the latest Perl modules that come with the OS.

Replies are listed 'Best First'.
Re: Date/Time Manipulation
by johngg (Canon) on May 24, 2016 at 18:50 UTC

    You could use Time::Piece and Time::Seconds which are core modules in recent Perl versions.

    $ perl -MTime::Piece -MTime::Seconds -Mstrict -Mwarnings -E ' my $dateStr = q{2016-05-16 10:51:07}; my $dateFmt = q{%Y-%m-%d %H:%M:%S}; my $tp = Time::Piece->strptime( $dateStr, $dateFmt ); $tp -= ONE_HOUR; say $tp->strftime( $dateFmt );' 2016-05-16 09:51:07 $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Date/Time Manipulation
by hippo (Bishop) on May 24, 2016 at 18:55 UTC
    Looking for a simple (hopefully) way of subtracting one hour from date - time in a file.

    Simple, you say?

    perl -pe 's/ (\d\d)/sprintf(" %2.2i",$1-1)/e;'

      Thank you hippo

      After surrounding that with variable definitions, file paths, file open/close and prints to a new file in a Perl script, I am a VERY HAPPY CAMPER.

      Not sure how it works; will explore that.

        Forgot to log in, RobertJ. Again thanks.
Re: Date/Time Manipulation
by mhearse (Chaplain) on May 24, 2016 at 18:38 UTC
    Convert the date times to epoch. Then subtract 3600. Then convert epoch back to date time.
Re: Date/Time Manipulation
by talexb (Chancellor) on May 24, 2016 at 19:50 UTC

    It seems like you want to re-state the times in a time zone that's an hour earlier .. so one approach you could take would be to read the timestamps in, change the timezone to one that's an hour earlier, and output the result.

    However, that's just an educated guess. Good luck!

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-28 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found