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: Intermediate:
incrimenting the date.

 



mtber
Novice

Nov 11, 2008, 8:12 PM

Post #1 of 10 (1361 views)
incrimenting the date. Can't Post

I am trying to get my last function in my perl program to work where it will take the timestamp and will incriment it my a value so if i want to incriment the day value by 7 and it starts at 10 then the day will be 17 but everything else in the timestamp will remain the same. This also works if they month or week value is to be incrimented by a value. any help would be appreciated


Code
#!/usr/bin/perl -w 

# FILE : timefuncs.pl
# LAST REVISION : 2008-11-10
# DESCRIPTION :
# PROGRAMMER : kyle beaulieu

use strict;# -- Enforce naming of variables (limits typos).
use warnings; # -- Warn for potential errors.



########
# MAIN #
########
system("cls");


#get some timestamps;
my $timestamp = Get_Timestamp();
my $later_timestamp = Get_Timestamp();
# print out some messages so we can see what happened.
print "old timestamp = \"$timestamp\"\n";
print "new timestamp = \"$later_timestamp\"\n\n";

# add some stuff to the later timestamp
my ($years, $months, $weeks, $days, $hours, $mins, $seconds)
=(2,1,3,37,52,20,100);
my @diffs = ($years, $months, $weeks, $days, $hours, $mins, $seconds);
$later_timestamp = Add_to_Timestamp($timestamp, @diffs);

# print out some messages so we can see what happened.
print "old timestamp = \"$timestamp\"\n";
print "new timestamp = \"$later_timestamp\"\n\n";
# add some stuff (but not as much) to the first timestamp
($years, $months, $weeks, $days, $hours, $mins, $seconds)
=(1,1,3,37,20,10,100);
@diffs = ($years, $months, $weeks, $days, $hours, $mins, $seconds);
$timestamp = Add_to_Timestamp($timestamp, @diffs);

# print out some messages so we can see what happened.
print "old timestamp = \"$timestamp\"\n";
print "new timestamp = \"$later_timestamp\"\n\n";

# find the diffrence
@diffs = Calc_Diff_Timestamps($timestamp, $later_timestamp);


print "diffs:\n";
foreach my $value ( @diffs) {
print "$value\t";
}
print "\n";





system("pause");


###################
# FUNCTION : Get_Timestamp
###################
# PARAMETERS : none
###################
# RETURNS : a string containing a timestamp in a human readable form
# : Weekday, Month day, year @ HH:MM:SS
# : monday, october 10, 2008 @ 22:20:58
###################
# LAST REVISION : 2008-11-10
###################
# PROGRAMMER : Kyle Beaulieu
###################
sub Get_Timestamp {
# intialize return value
my $timestamp = "";

### YOUR CODE HERE ###
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(time);
my $year = 1900 + $yearOffset;
$timestamp = "$weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year, @ $hour:$minute:$second" ;
### END YOUR CODE ###

return $timestamp;
}

###################
# FUNCTION : Calc_Diff_Timestamps
###################
# PARAMETERS : two timestamps, the starting point and the ending point.
###################
# RETURNS : an array containing values for the total diffrenc between these two timestamps.
# : for instance, the diffrence between:
# : monday, october 10, 2008 @ 20:00:05
# : and monday, october 10, 2008 @ 21:15:10
# : would be 1 for $hour, 75 for $minutes, and 4505 for $seconds, 0 for the rest of them.
###################
# LAST REVISION : 2008-11-10
###################
# PROGRAMMER : Kyle Beaulieu
###################
sub Calc_Diff_Timestamps {
# unpack parameters to named values
my ($timestamp_start_time, $timestamp_end_time) = @_;

# intialize return values
my ($years, $months, $weeks, $days, $hours, $mins, $seconds)
=(0,0,0,0,0,0,0);

### YOUR CODE HERE ###


### END YOUR CODE ###

my @diffs = ($years, $months, $weeks, $days, $hours, $mins, $seconds);
return @diffs;
}

###################
# FUNCTION : Add_to_Timestamp
###################
# PARAMETERS : a timestamp, and a list of values
###################
# RETURNS : a timestamp in a human readable form, but adjusted forward by the values.
# : for instance, if given the timestamp "monday, october 10, 2008 @ 22:20:58"
# : and a "7" for the day value (0 for the rest),
# : it would be incremented forward by one week (october 17th).
###################
# LAST REVISION : 2008-11-10
###################
# PROGRAMMER : Kyle Beaulieu
###################
sub Add_to_Timestamp {
# unpack parameters to named values;
my ($old_timestamp,
$years, $months, $weeks, $days, $hours, $mins, $seconds) = @_;
# intialize return value
my $new_timestamp = "initialized";

### YOUR CODE HERE ###


### END YOUR CODE ###

return scalar $new_timestamp;
}



KevinR
Veteran


Nov 11, 2008, 8:47 PM

Post #2 of 10 (1360 views)
Re: [mtber] incrimenting the date. [In reply to] Can't Post

Almost anytime you are doing something with dates you want to convert the date into epoch seconds and then do your calculations and convert back to a human readable date if necessary.
-------------------------------------------------


mtber
Novice

Nov 11, 2008, 9:32 PM

Post #3 of 10 (1358 views)
Re: [KevinR] incrimenting the date. [In reply to] Can't Post

I have been trying to figure out how to do the calculations but i am still having trouble. Below i have made some modifications to my last sub function but when i try to run the file it just hangs and does nothing.


Code
#!/usr/bin/perl -w 

# FILE : timefuncs.pl
# LAST REVISION : 2008-11-10
# DESCRIPTION :
# PROGRAMMER : kyle beaulieu

use strict;# -- Enforce naming of variables (limits typos).
use warnings; # -- Warn for potential errors.



########
# MAIN #
########
system("clear");


#get some timestamps;
my $timestamp = Get_Timestamp();
my $later_timestamp = Get_Timestamp();
# print out some messages so we can see what happened.
print "old timestamp = \"$timestamp\"\n";
print "new timestamp = \"$later_timestamp\"\n\n";

# add some stuff to the later timestamp
my ($years, $months, $weeks, $days, $hours, $mins, $seconds)
=(2,1,3,37,52,20,100);
my @diffs = ($years, $months, $weeks, $days, $hours, $mins, $seconds);
$later_timestamp = Add_to_Timestamp($timestamp, @diffs);

# print out some messages so we can see what happened.
print "old timestamp = \"$timestamp\"\n";
print "new timestamp = \"$later_timestamp\"\n\n";
# add some stuff (but not as much) to the first timestamp
($years, $months, $weeks, $days, $hours, $mins, $seconds)
=(1,1,3,37,20,10,100);
@diffs = ($years, $months, $weeks, $days, $hours, $mins, $seconds);
$timestamp = Add_to_Timestamp($timestamp, @diffs);

# print out some messages so we can see what happened.
print "old timestamp = \"$timestamp\"\n";
print "new timestamp = \"$later_timestamp\"\n\n";

# find the diffrence
@diffs = Calc_Diff_Timestamps($timestamp, $later_timestamp);


print "diffs:\n";
foreach my $value ( @diffs) {
print "$value\t";
}
print "\n";





system("pause");


###################
# FUNCTION : Get_Timestamp
###################
# PARAMETERS : none
###################
# RETURNS : a string containing a timestamp in a human readable form
# : Weekday, Month day, year @ HH:MM:SS
# : monday, october 10, 2008 @ 22:20:58
###################
# LAST REVISION : 2008-11-10
###################
# PROGRAMMER : Kyle Beaulieu
###################
sub Get_Timestamp {
# intialize return value
my $timestamp = "";

### YOUR CODE HERE ###
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(time);
my $year = 1900 + $yearOffset;
$timestamp = "$weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year, @ $hour:$minute:$second" ;
### END YOUR CODE ###

return $timestamp;
}

###################
# FUNCTION : Calc_Diff_Timestamps
###################
# PARAMETERS : two timestamps, the starting point and the ending point.
###################
# RETURNS : an array containing values for the total diffrenc between these two timestamps.
# : for instance, the diffrence between:
# : and monday, october 10, 2008 @ 21:15:10
# : would be 1 for $hour, 75 for $minutes, and 4505 for $seconds, 0 for the rest of them.
###################
# LAST REVISION : 2008-11-10
###################
# PROGRAMMER : Kyle Beaulieu
###################
sub Calc_Diff_Timestamps {
# unpack parameters to named values
my ($timestamp_start_time, $timestamp_end_time) = @_;

# intialize return values
my ($years, $months, $weeks, $days, $hours, $mins, $seconds)
=(0,0,0,0,0,0,0);

### YOUR CODE HERE ###


### END YOUR CODE ###

my @diffs = ($years, $months, $weeks, $days, $hours, $mins, $seconds);
return @diffs;
}

###################
# FUNCTION : Add_to_Timestamp
###################
# PARAMETERS : a timestamp, and a list of values
###################
# RETURNS : a timestamp in a human readable form, but adjusted forward by the values.
# : for instance, if given the timestamp "monday, october 10, 2008 @ 22:20:58"
# : and a "7" for the day value (0 for the rest),
# : it would be incremented forward by one week (october 17th).
###################
# LAST REVISION : 2008-11-10
###################
# PROGRAMMER : Kyle Beaulieu
###################
sub Add_to_Timestamp {
# unpack parameters to named values;
my ($old_timestamp,$years, $months, $weeks, $days, $hours, $mins, $seconds) = @_;
# intialize return value
my $new_timestamp = "initialized";
my $counter = 1;
### YOUR CODE HERE ###
while ($days != 2)
{
$days = $days + $counter;
$counter ++;
}


### END YOUR CODE ###

return scalar $new_timestamp;
}



KevinR
Veteran


Nov 11, 2008, 10:19 PM

Post #4 of 10 (1356 views)
Re: [mtber] incrimenting the date. [In reply to] Can't Post

I can't tell what you are actually trying to do. If you want to add 7 days to the current date:


Code
print scalar locatime(time+(60*60*24*7);

-------------------------------------------------


FishMonger
Veteran / Moderator

Nov 12, 2008, 3:22 AM

Post #5 of 10 (1353 views)
Re: [mtber] incrimenting the date. [In reply to] Can't Post

Use one of these:
http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod
http://search.cpan.org/author/SBECK/Date-Manip-5.54/lib/Date/Manip.pod
http://search.cpan.org/~drolsky/DateTime-0.45/lib/DateTime.pm


mtber
Novice

Nov 12, 2008, 6:36 AM

Post #6 of 10 (1343 views)
Re: [KevinR] incrimenting the date. [In reply to] Can't Post

Than you for the help that works for the day only is there a way to change the month,Day and so on.


FishMonger
Veteran / Moderator

Nov 12, 2008, 6:49 AM

Post #7 of 10 (1342 views)
Re: [mtber] incrimenting the date. [In reply to] Can't Post

What modules will your instructor allow you to use in your homework assignment?

Are you allowed to use the POSIX module, which is one of Perl's core modules? If so, then look at the strftime() and mktime() functions.

http://search.cpan.org/~tty/kurila-1.14_0/ext/POSIX/POSIX.pod


mtber
Novice

Nov 12, 2008, 7:18 AM

Post #8 of 10 (1339 views)
Re: [FishMonger] incrimenting the date. [In reply to] Can't Post

We are allowed to use anything we want.no restrictions.


FishMonger
Veteran / Moderator

Nov 12, 2008, 7:56 AM

Post #9 of 10 (1332 views)
Re: [mtber] incrimenting the date. [In reply to] Can't Post

Then read the documentation for the modules that I pointed you to in my prior posts and you'll see how to simplify your script.


Quote
when i try to run the file it just hangs and does nothing

That is most likely due to:

Code
system("pause");



FishMonger
Veteran / Moderator

Nov 12, 2008, 8:12 AM

Post #10 of 10 (1329 views)
Re: [mtber] incrimenting the date. [In reply to] Can't Post

Here's one hint for cleaning up the code. The Get_Timestamp() sub could be reduced to a single line, i.e. look at the strftime() function.

 
 


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

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