mmartin has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I was wondering if one method is 'better' then the other...?
I'm using the Date and Time values quite a few times in my script to calculate what is basically a 'start' and
'end' times to be used within another script. Which can be present and/or future date and times...
So is one of these 2 methods below better to use, for any reason, then the other one?
the number of years since 1900 (*i.e. this year == 113, get actual year by "$year += 1900")...
I'm using each piece of the Date and Time variables at some point later on in the script. So I do need
each individual piece of the date and Time vars cause I use them quite a few times in the script.
Any thoughts or suggestions would be much appreciated.
Thanks in Advance,
Matt
I was wondering if one method is 'better' then the other...?
I'm using the Date and Time values quite a few times in my script to calculate what is basically a 'start' and
'end' times to be used within another script. Which can be present and/or future date and times...
So is one of these 2 methods below better to use, for any reason, then the other one?
Each of those produces pretty much the same result except the Month is Zero-based and the year is#!/usr/bin/perl use strict; use warnings; use DateTime; ### Method one... Using the DateTime Function: my $date = DateTime->now->set_time_zone('America/New_York'); # Get the individual values of each below: my $month = $date->month(); my $day = $date->day(); my $year = $date->year(); my $min = $date->minute; my $hour = $date->hour; my $sec = $date->second; print "\tMonth --> $month\n"; print "\t Day --> $day\n"; print "\t Year --> $year\n"; print "\t Hour --> $hour\n"; print "\t Min --> $min\n"; print "\t Sec --> $sec\n"; print "\t\n$month/$day/$year $hour:$min:$sec\n\n"; print "####################################\n\n"; ### Method 2: Use the localtime(time) Function: my ($sec1,$min1,$hour1,$day1,$month1,$year1,@rest) = localtime(time); $month1 += 1; # Add one to Month to get actual $year1 += 1900; # Add 1900 to year to get actual print "\tMonth --> $month1\n"; print "\t Day --> $day1\n"; print "\t Year --> $year1\n"; print "\t Hour --> $hour1\n"; print "\t Min --> $min1\n"; print "\t Sec --> $sec1\n"; print "\t\n$month1/$day1/$year1 $hour1:$min1:$sec1\n\n"; ***************** END SCRIPT ***************** _______________________OUTPUT_______________________ Month --> 7 Day --> 11 Year --> 2013 Hour --> 13 Min --> 13 Sec --> 28 7/11/2013 13:13:28 #################################### Month --> 7 Day --> 11 Year --> 2013 Hour --> 13 Min --> 13 Sec --> 28 7/11/2013 13:13:28
the number of years since 1900 (*i.e. this year == 113, get actual year by "$year += 1900")...
I'm using each piece of the Date and Time variables at some point later on in the script. So I do need
each individual piece of the date and Time vars cause I use them quite a few times in the script.
Any thoughts or suggestions would be much appreciated.
Thanks in Advance,
Matt
|
---|
Back to
Seekers of Perl Wisdom