•Re: help on getting date
by merlyn (Sage) on Jun 02, 2003 at 16:29 UTC
|
| [reply] |
|
| [reply] |
|
| [reply] |
Re: help on getting date
by hardburn (Abbot) on Jun 02, 2003 at 16:17 UTC
|
You can get dates using localtime.
Comparing dates is trickier. Dig through the Date:: and DateTime:: modules. The DateTime:: namespace is for a new project trying to unify Perl's mess of Date and Time modules into a single, consistant API, so look there first.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] |
Re: help on getting date
by halley (Prior) on Jun 02, 2003 at 16:20 UTC
|
Personally, if sorting or comparing human-readable dates is important, I'd recommend YYYY-MM-DD format. It naturally sorts, and reduces ordering confusion about 3 June or 6 January. Bigger units to the left.
As mentioned, localtime() will return the individual components for you. Just remember to add 1 to the month and 1900 to the year.
If your data is already IN the poor MM-DD-YYYY format, then you'll need the help of one of the Date::* modules to convert or compute back to a useful scalar unit like epoch time, Julian dates, or something. Keep dates numeric internally, and provide formatted equivalents only for user presentation.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] |
|
First off I want to emphasize that you're right on all the really important points. The nit I will pick is that "need" is kind of a strong word, given that there's a pretty simple way to convert MM-DD-YYYY to YYYY-MM-DD format, even if it's not widely capable like a Date::* module:
# assumes you have 0-padded dates, but if you don't you can change
# the \d\d's to \d\d?'s , and do an appropriate sprintf.
my @ymd = map { my ($m, $d, $y) = $_ =~ /(\d\d)-(\d\d)-(\d{4})/; "$y-
+$m-$d"; } @dmy;
HTH
If not P, what? Q maybe? "Sidney Morgenbesser" | [reply] [d/l] |
|
why bother with the temporary variables, especially inside map? Heck, you'd also screen out a small subset of invalid data.
print map {$_ =~ /(\d\d)-(\d\d)-(\d{4})/ && "$3-$1-$2"; } ("01-02-1999");
of course, using this kind of code, you'd need to make sure that your dataset's valid -- you won't get Date::Manip's heavy duty "Am I valid datetime or not" routines.
| [reply] [d/l] |
Re: help on getting date
by crouchingpenguin (Priest) on Jun 02, 2003 at 18:23 UTC
|
1. Is there a way to get the system date using perl in the following format mm-dd-yyyy?
Yes, using POSIX::strftime:
print POSIX::strftime("%m-%d-%Y",localtime()),"\n";
2. is there a way to compare the dates to see which is greater or lesser then a given date which is in mm-dd-yyyy format?
Yes, using POSIX::mktime() and optionally POSIX::difftime():
my $start_date_string = '05-03-2003';
my @date_parts = split('-',$start_date_string);
# build time
my $then_time = POSIX::mktime(
0,0,0,
$date_parts[1],
($date_parts[0] -1),
($date_parts[2] - 1900)
);
### or could be mktime() from another split string here
my $now_time = time();
#could also use POSIX::difftime:
#"the time difference (in seconds) between two times
#(as returned by 'time()')"
if( $then_time < $now_time ){
print POSIX::ctime($then_time)
. " was before "
. POSIX::ctime($now_time) . "\n";
}else{
print POSIX::ctime($then_time)
. " was after "
. POSIX::ctime($now_time) . "\n";
}
All that fun and POSIX comes with core perl.
Update:Oops, thanks rob_au for catching my typo.
cp
----
"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic." | [reply] [d/l] [select] |
|
Excellent post. Just one point which I would pick up on, is that values for the month, weekday and day of the year passed to POSIX::mktime begin at zero. As such, the determination of $then_time in your post should alternatively read:
my $then_time = POSIX::mktime
(
0, # Seconds
0, # Minutes
0, # Hours
$date_parts[1], # Day (of month)
$date_parts[0] - 1, # Month
$date_parts[2] - 1900 # Year
);
This behaviour is documented on the POSIX module man page.
perl -le 'print+unpack"N",pack"B32","00000000000000000000001001100100"'
| [reply] [d/l] [select] |
Re: help on getting date
by Mr. Muskrat (Canon) on Jun 02, 2003 at 16:20 UTC
|
Take a look at Date::Calc for working with dates. The documentation for it has a recipe for comparing dates.
As far as formatting to mm-dd-yy, you can use printf or sprintf to do that depending on what you want to do with it once it is formatted.
| [reply] |
Re: help on getting date
by fglock (Vicar) on Jun 02, 2003 at 17:26 UTC
|
use DateTime::Format::Strptime;
my $pat = DateTime::Format::Strptime->new(
pattern => '%m-%d-%Y' );
my $dt1 = $pat->parse_datetime( "06-07-2003" );
my $dt2 = $pat->parse_datetime( "07-06-2003" );
print $dt1 < $dt2 ? "smaller" : "equal or bigger";
| [reply] [d/l] |
Re: help on getting date
by fglock (Vicar) on Jun 02, 2003 at 17:00 UTC
|
my $dt = DateTime->now;
print "system date ", $dt->strftime( "%m-%d-%Y" );
my $compare = $dt <=> $other_date;
Note: $other_date must be a DateTime object
| [reply] [d/l] |
Re: help on getting date
by data64 (Chaplain) on Jun 03, 2003 at 03:24 UTC
|
| [reply] |