#!/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;
}