CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need a script to increment date and time by 30 minutes

 



cuboidgraphix
User

Dec 26, 2012, 1:17 PM

Post #1 of 5 (1709 views)
Need a script to increment date and time by 30 minutes Can't Post

Hello,
I am trying to write a script to increment my time by 30 minutes for a period of 3 days. Currently I have tried installing DateTime without any success. I was wondering if there is an easier or more efficient way of writing this.

For example. I have a starting date and time such as this. 17/12/2012 00:00:00

I need increments of 30 minutes.. to output as the following.

17/12/2012 00:30:00
17/12/2012 01:00:00
.
.
.
17/12/2012 23:30:00

then start again with a new day.

18/12/2012 00:00:00
.
.
.
18/12/2012 23:30:00


And so on ... for 3 to 4 days.

This is what I have so far...


Code
 
#!/usr/bin/perl

# This is a script that will increment time by 30 minutes.

use strict;
use warnings;
use POSIX qw/strftime/;


my $Start = "17/12/2012 00:00:00";
my $End = "20/12/2012 23:00:00";
my $Now = $Start;

while($Now > $End){

$Now = Some code that will add 30 minutes.

}



(This post was edited by cuboidgraphix on Dec 26, 2012, 1:22 PM)


FishMonger
Veteran / Moderator

Dec 26, 2012, 1:48 PM

Post #2 of 5 (1704 views)
Re: [cuboidgraphix] Need a script to increment date and time by 30 minutes [In reply to] Can't Post

Start by using the POSIX mktime() function to calculate the start and end time values. Then create a loop that increments the start time by the required amount as it outputs it as formatted string via strftime.


cuboidgraphix
User

Dec 26, 2012, 2:01 PM

Post #3 of 5 (1700 views)
Re: [FishMonger] Need a script to increment date and time by 30 minutes [In reply to] Can't Post

Thanks FishMonger. I will look into it now.


FishMonger
Veteran / Moderator

Dec 26, 2012, 2:13 PM

Post #4 of 5 (1696 views)
Re: [cuboidgraphix] Need a script to increment date and time by 30 minutes [In reply to] Can't Post

If you use the Mktime function from the Date::Calc module instead of the POSIX function, you won't need to adjust the values to conform to what the POSIX mktime function expects.

Here's a short test script example;

Code
#!/usr/bin/perl 

use v5.10.1;
use strict;
use warnings;
use POSIX qw(strftime);
use Date::Calc qw(Mktime);
use Data::Dumper;

my $start = '17/12/2012 00:30:00';
my ($day,$mon,$yr,$hr,$min,$sec) = split(/[\/ :]/, $start);

my $time = Mktime($yr,$mon,$day, $hr,$min,$sec);

for (1..10) {
say scalar localtime($time);
$time += 60*30;
}


Output:
c:\testing>perl-1.pl

Quote
Mon Dec 17 00:30:00 2012
Mon Dec 17 01:00:00 2012
Mon Dec 17 01:30:00 2012
Mon Dec 17 02:00:00 2012
Mon Dec 17 02:30:00 2012
Mon Dec 17 03:00:00 2012
Mon Dec 17 03:30:00 2012
Mon Dec 17 04:00:00 2012
Mon Dec 17 04:30:00 2012
Mon Dec 17 05:00:00 2012



cuboidgraphix
User

Dec 26, 2012, 3:23 PM

Post #5 of 5 (1692 views)
Re: [FishMonger] Need a script to increment date and time by 30 minutes [In reply to] Can't Post

FishMonger it worked perfect. Thanks very much! Laugh

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives