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?
#!/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
Each of those produces pretty much the same result except the Month is Zero-based and the year is
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.