Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Localtime CDT to PST 3

Status
Not open for further replies.

kennygadams

Programmer
Jan 2, 2005
94
0
0
US
Hi,

I'm trying to display my server's CDT as PST but I can't get the minutes right. The minutes are not matching the actual PST time but the hours do.

Here's the code I'm using and you can see this function at work in the top-right sidebar at
Code:
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(time()-3*3600);
$year = 1900 + $yearOffset;
$theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";

if($hour >= 12){
	$hour = ($hour-12);
	print "$hour:$minute p.m.";
	}
else{
	print "$hour:$minute a.m.";
	}

Kenny
 
I updated the code because the hour processor was all messed up. I'm still having issues with the minutes not matching PST time.


Code:
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(time()-3*3600);
$year = 1900 + $yearOffset;
$theTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";


if($hour == 24){
	$hour = ($hour-12);
	print "$hour:$minute a.m.";
	}
elsif($hour >= 13){
	$hour = ($hour-12);
	print "$hour:$minute p.m.";
	}
elsif($hour == 12){
	print "$hour:$minute p.m.";
	}
else{
	print "$hour:$minute a.m.";
	}

Kenny
 
This might help get you going.
Code:
my $time = time;
my $pattern = "%12s: %s\n";
printf $pattern, 'gmtime', format_time(gmtime($time));
printf $pattern, 'localtime', format_time(localtime($time));
printf $pattern, 'localtime-2h', format_time(localtime($time - 2*3600));

sub format_time {
	my ($second, $minute, $hour, $dayOfMonth, $month, $year, $dayOfWeek, $dayOfYear, $daylightSavings) = @_;
	$month++;		#Adjust Month
	$year += 1900;	# Adjust year
	my $time_pattern = '%02i:%2i %4s';
	if ($hour == 0) {
		return sprintf $time_pattern, 12, $minute, 'a.m.';
	} elsif ($hour == 12) {
		return sprintf $time_pattern, $hour, $minute, 'p.m.';
	} elsif ($hour > 12) {
		return sprintf $time_pattern, $hour-12, $minute, 'p.m.';
	} else {
		return sprintf $time_pattern, $hour, $minute, 'a.m.';
	}
}
 
Maybe for future reference, in a 24 hour clock, the time never equals 24:

if($hour == 24){

it goes from 23:59:59 to 00:00:00

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
How about using the OS to worry about the time zone differences for you?

Code:
$ENV{"TZ"}="PST8PDT";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf "%02d:%02d %4s\n",
        ($hour % 12 == 0) ? 12 : $hour % 12,
        $min,
        ($hour < 12) ? "a.m." : "p.m.";

I'm assuming a Unix-like OS here, not sure if this works on other platforms.

Annihilannic.
 
Hi Annihilannic,

I'm running this on a Linux machine. Your code worked but it is ahead by 1 hour and the minutes are running behind by 15.

The one thing in common between your code and rharsh's code is that they are both displaying 15 minutes behind the actual PST time. Is this fixable?

rharsh's and Annihilannics, code at work:

Kenny
 
The clock on your web server is 45 minutes fast. It shows UTC 03:52 when the real time is UTC 03:07.

Annihilannic.
 
Thanks Annihilannic!

I ran the following code in SSH and it worked.

date -s "03/18/09 15:37:00"

Kenny
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top