The DateTime Project aims to solve all date/time processing problems in a single (tho' large!) consistant set of modules.
Here's how to solve your problem with the DateTime modules.
#!/usr/bin/perl
use strict;
use warnings;
use DateTime::Format::Strptime;
my $strp = DateTime::Format::Strptime->new(pattern => '%H:%M%P');
my $time = $strp->parse_datetime('8:35am');
$time->add(hours => 1, minutes => 23);
print $strp->format_datetime($time), "\n";
--
< http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [d/l] |
Dates and times are notoriously the most difficult aspect, primarily from an interface POV. So, you should be warned that you will spend some time reading before you can do what you want. That time, imho, will be well-spent. (It was, for me.)
I personally like Date::Calc, but many people like Date::Manip. There's also DateTime, but it's still under development.
As for snippets, the POD for all the distros I mention have examples for exactly what you're looking for.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] |
DateTime.pm is quite usable. It's "under development" in the same sense that any reasonably large city's road system is "under development". Yeah, there might be some major road construction going on, but that doesn't stop it from being useful. In fact, it's various interfaces (with both the application developer, and with the various formatting and calendar modules) are quite well defined, so we can expect there to be only minor annoyances in comparison to road construction.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] |
I shall get to reading. Thank you!
| [reply] |
Certainly follow up on dragonchild's list of relevant modules. Just in case you're looking for a "maximally portable" approach -- i.e. one the depends solely on core modules, and so does not presume/require installation of optional modules -- then here is an example of how you might use Time::Local for this sort of job:
use strict;
use Time::Local;
my ( $dy, $mo, $yr ) = ( localtime )[3..5];
my $ref_time = "08:35:00"; # (pm would be 20:35:00)
my ( $hr, $mi, $sc ) = split( /:/, $ref_time );
my $ref_sec = timelocal( $sc, $mi, $hr, $dy, $mo, $yr );
my $delta_time = "01:23:00";
my ( $add_hr, $add_mi, $add_sc ) = split( /:/, $delta_time );
my $delta_sec = (($add_hr * 60) + $add_mi) * 60 + $add_sc;
( $sc, $mi, $hr, $dy, $mo, $yr ) = localtime( $ref_sec + $delta_sec );
# don't forget to add 1 to $mo, and 1900 to $yr if/when you use them;
# one way to check results:
print scalar( localtime( $ref_sec + $delta_sec )), $/;
Basically, the idea is to do delta arithmetic in seconds, and to convert between component values (second, minute, hour, day, month, year) and "seconds since the epoch" as needed. Time::Local::timelocal() converts efficiently from component values to "seconds since the epoch", and localtime goes the other way.
(As an interesting side note, timelocal() will do the right thing if you feed it "out-of-range" component values, e.g. $dy = 31 and $mo = 1 (i.e. February), or $hr = 28, etc. | [reply] [d/l] |
use Time;
$time = timelocal($sec,$min,$hours,$mday,$mon,$year);
$time += (1*60 + 23)*60;
(...)=localtime($time);
| [reply] [d/l] |