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

Re^2: Yesterday's date

by kcott (Archbishop)
on May 29, 2023 at 04:05 UTC ( [id://11152450]=note: print w/replies, xml ) Need Help??


in reply to Re: Yesterday's date
in thread Yesterday's date

G'day hippo,

++ I agree with your approach; particularly in light of the OP's "I am not concerned about daylight savings time, time zone nor do I need HMS.".

Having said that, I believe use of the ONE_DAY constant from Time::Seconds (also a core module) would add clarity.

It's the same value:

$ perl -E 'use Time::Seconds; say ONE_DAY' 86400

It works as a direct replacement in your code:

$ perl -e ' use strict; use warnings; use Time::Piece; use Time::Seconds; my $t = Time::Piece->strptime($ARGV[0], "%d/%m/%Y"); printf "Date as supplied: %s\n", $t->dmy("/"); $t = $t - ONE_DAY; printf "Date minus one day: %s\n", $t->dmy("/"); ' 01/12/2022 Date as supplied: 01/12/2022 Date minus one day: 30/11/2022

— Ken

Replies are listed 'Best First'.
Re^3: Yesterday's date
by parv (Parson) on May 29, 2023 at 20:12 UTC

    If $Time::Seconds::ONE_DAY remains constant regardless of current time, then do not see how that would add "clarity" or see its purpose. If 24 *60 *60 conjures up magic number, then just assign that to a local variable.

      G'day parv,

      hippo's code uses 86400. For those regularly working with dates and times, this might immediately leap out as the number of seconds in a day; for others, it might just be a cryptic number (which takes some thought to understand).

      Time::Seconds puts that number into a named constant. From its source:

      use constant { ... ONE_DAY => 86_400, ... };

      I do believe that ONE_DAY is clearer than 86400. Even more so in this particular instance where "$t - ONE_DAY" aligns with the text in the following printf statement, "Date minus one day".

      I don't have a problem with "24 * 60 * 60". I would have used that calculation many times myself, over the decades, where ONE_DAY (or equivalent) was not available. It does, however, seem like extra, unecessary work:

      ... my $one_day = 24 * 60 * 60; ... $t = $t - $one_day; ...

      Unless it's needed more than once, I probably wouldn't bother to "assign that to a local variable":

      ... $t = $t - 24 * 60 * 60; ...

      — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found