This is one of the first Perl scripts I wrote, fooling around with localtime(), timelocal() and timegm().
# the functions timelocal() and timegm(), which are contained in the module Time::Local, are inverses of # the built-in Perl function localtime(). use Time::Local; # Populate a list with the values returned from localtime(). ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # localtime returns the number of years since 1900, so to get the calendar year we add 1900. $calendar_year = $year + 1900; print "\n\n***************************************************************\n\n"; print "The current time and date is:\n\n$hour:$min:$sec $calendar_month $mday, $calendar_year\n\n"; # Set the calendar month based on the return value from localtime(). $calendar_month = calendar_month($mon); # The timelocal() function takes seconds, minutes, and so forth and returns the number of seconds since the epoch. $seconds_since_epoch = timelocal($sec,$min,$hour,$mday,$mon,$year); print "The number of seconds since the epoch is:\n\n$seconds_since_epoch\n\n"; # To verify that localtime() and timelocal() are inverses, populate a second list with the return value of localtime() # with the scalar $seconds_since_epoch as an input. ($my_sec,$my_min,$my_hour,$my_mday,$my_mon,$my_year,$my_wday,$my_yday,$my_is dst) = localtime($seconds_since_epoch); # Set the calendar month based on the return value from localtime(). $my_calendar_month = calendar_month($mon); # localtime returns the number of years since 1900, so to get the calendar year we add 1900. $my_calendar_year = $my_year + 1900; print "Taking the value $seconds_since_epoch as the input, the locatime() function\n"; print "returns a current time and date of:\n\n"; print "$my_hour:$my_min:$my_sec $my_calendar_month $my_mday, $my_calendar_year\n\n"; # The timegm() function takes seconds, minutes, and so forth and returns the number of seconds since the epoch # in Greenwich Mean Time. $gm_seconds_since_epoch = timegm($my_sec,$my_min,$my_hour,$my_mday,$my_mon,$my_year); print "The number of seconds since the epoch at Greenwich is:\n\n$gm_seconds_since_epoch\n\n"; print "The local time should be Greenwich time plus 7 hours (60 * 60 * 7 seconds).\n\n"; $local_time_from_greenwich_time = $gm_seconds_since_epoch + 60*60*7; print "The seconds since the epoch locally is therefore:\n\n$local_time_from_greenwich_time.\n\n"; # So how to take a return value which is the number of seconds since the epoch and determine if that represents # sometime today? You have to calculate the number of seconds since the epoch at midnight and subtract that value. # To get the number of seconds since the epoch at the last occurrence of midnight, take the return values from # localtime(time), change $sec, $min and $hour to zero, then use those values as agruments to the timelocal() function. $mn_seconds_since_epoch = timelocal(0,0,0,$mday,$mon,$year); print "At the last stroke of midnight, the number of seconds since the epoch was:\n\n$mn_seconds_since_epoch\n\n"; # Verify that is correct by using that value as input to the localtime function. ($mn_sec,$mn_min,$mn_hour,$mn_mday,$mn_mon,$mn_year,$mn_wday,$mn_yday,$mn_is dst) = localtime($mn_seconds_since_epoch); # Set the calendar month based on the return value from localtime(). $mn_calendar_month = calendar_month($mn_mon); # localtime returns the number of years since 1900, so to get the calendar year we add 1900. $mn_calendar_year = $mn_year + 1900; print "Taking that number of seconds as an input to the localtime function returns\n"; print "a time and date of:\n\n$mn_hour:$mn_min:$mn_sec $mn_calendar_month $mn_mday, $mn_calendar_year\n\n"; sub calendar_month { # input is a number from 0 to 11, which is the range returned by localtime() for the $mon scalar, and # return value is the name of the corresponding calendar month. @month_name = qw( January February March April May June July August September October November December ); return $month_name[$_[0]]; } sub print_formatted_date { # take input of $sec, $min, $hour, $mday, $mon, $year as returned by the localtime() function and # print a date and time in the hh:mm:ss month day, year format. $fd_calendar_month = calendar_month($_[4]); $fd_calendar_year = $_[5] + 1900; print "$_[2]:$_[1]:$_[0] $fd_calendar_month $_[3], $fd_calendar_year\n\n"; } print "Testing my formatted_date function:\n\n"; print_formatted_date($mn_sec,$mn_min,$mn_hour,$mn_mday,$mn_mon,$mn_year); -----Original Message----- From: Joe Slaven [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 1:47 PM To: [EMAIL PROTECTED] Subject: Using Time::Localtime module Has anyone an example of using Time::Localtime module to print out the date and time to the nearest second in the format: : sec:min:hour (am/pm) Thurs 2nd Dec 2001 Thanks, Joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]