Hi all,

I have been struggling with time/date in perl for several hours now and for some reason i get a quirky error. I have an array called "date" 44 elements in length and "time" 44 elements in length. I created this code to convert UTC to localtime (Alaska). But whenever I run this code, my $date[0] (which is 09/12/2007) gets replaced with (09/14/2007). I did all kinds of debugging on this and can't seem to find what the matter is. One thing i noticed is that the value of $date[0] changes from 09/12/2007 to 09/13/2007 to 09/14/2007 !! I want it to stay at 09/12/2007... argh!!!

Does anyone know of an easier way to convert the time? The code to backtrack to the previous day, I got from another perl message board(start date and end date function) and I tweaked it ($md-8) until I saw that it went back a day.

dave

foreach $x (0...$#date) 
{

	if ($isdst) 
	{
		print "we are in daylight saving time, so subtract 9";
	}
	else 
	{	
		#print " we are not in daylight saving time, so subtract 8 </br>";

		if ($time[$x]<8)
		{

			$time[$x]=($time[$x]+24-8);
			$time[$x]= join(':',$time[$x], "00");
			
			# have to use the previous days, so convert the 
			# current date to EPOCH then subtract a day
			# then change back to regular date 00/00/0000
			$date[$x]= get_prevday($date[$x]);	
			sub get_prevday { $date[$x] = shift || return(0);
  			my ($m,$md,$y) = $date[$x] =~ m|(\d+)/(\d+)/(\d+)|;
   			 my $epochtime = timelocal_nocheck(0, 0, 0, $md-8, $m-1, $y);
   	            return strftime("%m/%d/20%y",0,0,0,(localtime($epochtime+604800))[3,4,5]);}		
	
		}
		else
		{
			if ($time[$x]<18)
			{

				$time[$x]="0".($time[$x]-8).":00";

			}
			else
			{

				$time[$x]=($time[$x]-8).":00";

			}


		}

	}

Your question is very confusing. I can not understand what date you are trying to convert into another date. But this makes no sense to me: ($md-8)

Why are you subtracting 8 from the day of the month? Then later you add 7 days worth of seconds to the date: $epochtime+604800

All of your adjustments to the date should only be made after converting to epoch seconds, except for the month, you have to subtract one to coincide with the 0-11 format of localtime() before converting to epoch seconds. Then you add or subtract seconds from the epoch time as needed and then convert back into a human readable time format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.