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

Re: What's "Better" DateTime or localtime?

by sundialsvc4 (Abbot)
on Jul 12, 2013 at 04:54 UTC ( #1043873=note: print w/replies, xml ) Need Help??

in reply to What's "Better" DateTime or localtime?

Maybe a “better” question is:   What, exactly, does “better” mean to you ... given this particular situation ... and why?

This, actually, is not a rhetorical question ...

On the one hand, we all are paid to make our customers” problems (and ours ...) “just go away.”

Yet, we must always add to that ... “for good.”

“We are professionals.   (?)   Do not try this at home.”

You have, so far, written two apparently-equivalent solutions to the same problem.   If that problem will never, ever, re-appear (courtesy of The Marketing Department™...?) in some out-of-the-blue variation that now makes you, having spent X irrecoverable weeks of your life (successfully...?) “solving” the problem in the first way, now be flagellating yourself that you did not instead use the second ...

... “then, Pilgrim, you may count yourself Very Lucky.™   And, Very Rare.™”

Yet it does happen ... both ways.   Heads.   Tails.   Use your best, most suspicious, most cynical skeptical judgment.   And, good luck to ye.

  • Comment on Re: What's "Better" DateTime or localtime?

Replies are listed 'Best First'.
Re^2: What's "Better" DateTime or localtime?
by mmartin (Monk) on Jul 17, 2013 at 20:34 UTC
    Your a poet and you didn't even know it....

    I really just wanted people's opinion on what they liked using "better"...

    I decided to use the localtime method/function (*whichever its called) along with the Time::Piece Module.
    I found that Module had lots of useful Methods with it. Some that I used include:
        *If $t contains 'localtime', then...
            - $t->sec;
            - $t->min;
            - $t->hour;
            - $t->mon;
            - $t->mday;
            - $t->wday;
            - $t->year;
            - $t->mdy("/");

    Those were just some of the Methods I found useful when I was writing my script. I'm sure the other Module/Function has
    plenty of similar Methods along with them. But I just found that this one was a bit more useful for myself...


      One thing to watch out for is daylight savings conversions when adding/subtracting days. E.g., this may not work as expected, depending on what day and what time of day it is run (just as if using the builtin localtime):
      use Time::Piece qw(localtime); use Time::Seconds qw(ONE_DAY); my $t = localtime; my $yesterday = $t - ONE_DAY(); my $tomorrow = $t + ONE_DAY(); print "Today: ", $t->ymd(),"\n"; print "Yesterday: ", $yesterday->ymd(),"\n"; print "Tomorrow: ", $tomorrow->ymd(),"\n";
      I have found this sort of mistake in someone's code before (though the code did not use Time::Piece, just plain localtime). One way to be sure of adding or subtracting the correct number of days would be to truncate to the beginning of the day, then add an extra half day, or subtract a half-day less:
      my $t = localtime->strptime(localtime->ymd(), '%Y-%m-%d'); my $yesterday = $t - ( 0.5 * ONE_DAY() ); my $tomorrow = $t + ( 1.5 * ONE_DAY() ); print "Today: ", $t->ymd(),"\n"; print "Yesterday: ", $yesterday->ymd(),"\n"; print "Tomorrow: ", $tomorrow->ymd(),"\n";
      One advantage of DateTime is that it does make it a little easier to get this correct. E.g. in my timezone:
      my $t = localtime->strptime('2013-03-11', '%Y-%m-%d'); my $yesterday = $t - ONE_DAY(); my $tomorrow = $t + ONE_DAY(); print "Today: ", $t->ymd(),"\n"; print "Yesterday: ", $yesterday->ymd(),"\n"; print "Tomorrow: ", $tomorrow->ymd(),"\n"; #Prints: Today: 2013-03-11 Yesterday: 2013-03-09 Tomorrow: 2013-03-12
        Hey runrig, thanks for the reply!

        Cool, good info there, thanks!

        I don't know if its the "best" way to go, but I was using the dates I created using the "Time::Piece" Module, then
        I use the "Date::Manip" module to calculate a future date/time (*or past date if you needed to). The cool thing about
        the Date::manip Module is that you can use human relate-able strings to calculate the date.

        For example, before I added the "Date::Manip" Module, and currently still, I have a Command Line Option that the user inputs when
        executing. Where the value of the argument is in the form of "a number followed by a time-unit". So for instance the
        user could enter any of the following:
                --end-in="2 hours"
                --end-in="3 hrs"
                --end-in="20 min"

        Then after the user executes and after some error checking of the users input, I take the option the user entered, like "30 mins"
        and then use that in the DateCalc Function, which is part of the Date::Manip Module. For the error checking I split the users
        input into a number and a "time_unit", so "30mins" would get split into "30" and "mins", then I check each one.

        So I would then take the current_date calculated by "Time::Piece qw(localtime)" and use that as the first argument to the "DateCalc"
        Function, like so:
        ## Create the 'date manipulation' string for the DateCalc Function: # *The user's input is called $end_timeSpan... my $date_manip = "in $end_timeSpan" # For this example lets say: # $current_time == "07/17/2013 10:30:00" ### Calculate the future date from the $current_date: # *Output from function is in the form: # "2013071811:00:00" --> YYYYMMDDHH:MM:SS my $future_date = DateCalc($current_date,$date_manip) # After running the above & capturing each piece from DateCalc, the # $future_date == "07/17/2013 11:00:00", which would be 30 mins # from the $current_date.

        So that's basically what I'm doing with the Date stuff. There is some other stuff I do with the Date and Time but that above is
        the most important part.

        Thanks again for all the suggestions and info.

        Thanks Again,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043873]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-05-28 05:42 GMT
Find Nodes?
    Voting Booth?