Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: perl basic count days between two dates

by davido (Cardinal)
on Oct 02, 2013 at 22:08 UTC ( [id://1056684]=note: print w/replies, xml ) Need Help??


in reply to perl basic count days between two dates

  • ...perl basic..."
  • ...I am very new to perl...
  • ...and programming in general.
  • ...question about getting the amount of days between two dates.
  • ...any advice for me.?

We were all new to Perl at some point, and all new to programming in general. Nothing wrong with that. But this is not a "basic" problem. However, Perl does provide tools to help you avoid becoming an expert in date calculations. My advice: If you're not using the power of Perl's modules (core and CPAN), you're not programming Perl. You're just using its syntax and interpreter.

Your technique is neglecting leap years, isn't it?

Here is a CPANy version:

use strict; use warnings; use DateTime; use IO::Prompt::Tiny qw( prompt ); # Get a DateTime object for birthday and now based on user input. my( $bday, $now ) = map { DateTime->new( prompt_date( $_ ) ) } qw( Birth Today's ); die "Birth date must be in the past.\n" unless $bday < $now; # Perform the math. print "\nYou are ", $bday->delta_days($now)->in_units('days'), " days +old.\n"; # Prompting: pass date entry type, receive a day=>__, month=>__, year= +>__ hash. sub prompt_date { print shift, " date entry:\n"; return map { $_ => prompt "\tPlease enter $_:" } qw( day month year +); }

A sample run:

Birth date entry: Please enter day: 27 Please enter month: 9 Please enter year: 1954 Today's date entry: Please enter day: 02 Please enter month: 10 Please enter year: 2013 You are 21555 days old.

(I used the birth date of a famous individual in the Perl community.)

Date calculations are very hard to get right. At minimum we have to deal with leap years. There could be other challenges that I'm not aware of, and since I don't want to become an expert in nuances of the Gregorian Calendar, I offload as much of that burden as possible to the DateTime module.

Prompting with a message isn't all that hard, but IO::Prompt::Tiny seems more convenient than a bunch of print statements, <STDIN> invocations, and chomp, so I used that module too.

Once we've handled the prompting and the date manipulation, there's not much left to do except a quick sanity check, and printing the result of the calculation. Keep in mind that there are countless problem domains in the universe. As a programmer you will be asked to become an expert on some of them. But it would be impossible to become an expert on all of them. Using well-tested modules that solve problems such that you don't need to become an expert on a peripheral topic will enable you to focus more on becoming a great programmer, and less on becoming a great date calculator, genome researcher, and so on. There's no shame in using available tools to make your life as a programmer easier. In fact, it's smart; you become more effective, and your code becomes more robust.

A little more terse:

use strict; use warnings; use DateTime; use IO::Prompt::Tiny qw( prompt ); # Get a DateTime object for birthday and now based on user input. my( $bday, $now ) = map { print "$_ date entry:\n"; DateTime->new( map { $_ => prompt "\tPlease enter $_:" } qw(day mont +h year) ) } qw(Birth Today's); die "Birth date must be in the past.\n" unless $bday < $now; # Perform the math. print "\nYou are ", $bday->delta_days($now)->in_units('days'), " days +old.\n";

Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-24 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found