Solved

Time add in perl

Posted on 2004-09-30
4
1,829 Views
Last Modified: 2006-11-17
Say the current datetime is 17:46:12, I got the time by using localtime(), i want to add 1 hr 30 minutes 55 seconds.
Is there a function in perl to archieve that?
0
Comment
Question by:matchz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
gripe earned 50 total points
ID: 12190058
You can do this either by converting your time to seconds and adding the seconds to time's output. Such as:

my $future = "1:30:55";
my ($hours, $minutes, $seconds) = split(/:/, $future);

$hours *= 60 * 60;
$minutes *= 60;
$seconds += $hours + $minutes;

print scalar(localtime(time + $seconds));

Or there are also some modules available to do this for you. Notably there's Date::Manip:

You can do this either by converting your time to seconds and adding the seconds to time's output. Such as:

my $future = "1:30:55";
my ($hours, $minutes, $seconds) = split(/:/, $future);

$hours *= 60 * 60;
$minutes *= 60;
$seconds += $hours + $minutes;

print +(localtime(time + $seconds));

Or there are also some modules available to do this for you. Notably there's Date::Manip:

#!/usr/bin/perl

use warnings;
use strict;
use Date::Manip;

my $delta = "1:30:55";
my $date = DateCalc("now", "+ 1hour 30minutes 55seconds");

print $date;

Hope this helps.
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12190083
Calculating with Epoch seconds is easiest, disregarding the effort to get dates and times into and out of Epoch seconds. This code shows how to calculate an offset (55 days, 2 hours, 17 minutes, and 5 seconds, in this case) from a given base date and time:

$birthtime = 96176750;                  # 18/Jan/1973, 3:45:50 am
$interval = 5 +                         # 5 seconds
            17 * 60 +                   # 17 minutes
            2  * 60 * 60 +              # 2 hours
            55 * 60 * 60 * 24;          # and 55 days
$then = $birthtime + $interval;
print "Then is ", scalar(localtime($then)), "\n";
Then is Wed Mar 14 06:02:55 1973

Jay
0
 
LVL 13

Expert Comment

by:gripe
ID: 12190084
Note that the author of Date::Manip himself indicates that it's a big, slow, pure perl module and may not be suitable to all tasks. There are a number of other date manipulation modules on CPAN, but Date::Manip is the most full featured and easy to use.

YMMV
0
 
LVL 8

Expert Comment

by:davorg
ID: 12209064
It's no longer true that Date::Manip is the most fully featured date manipulation module on CPAN. The Perl Date/Time Project (http://datetime.perl.org/) have created a set of useful modules which you should be looking at for all of your date manipulation needs.

Dave...
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 195
perl split by | 2 122
add a syntax to a csv file 8 101
Is it true to say that Python is successor of Perl? 7 233
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question