CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
need script for time stamp for EST (USA)



Jan 21, 2000, 3:33 PM

Post #1 of 6 (2665 views)
need script for time stamp for EST (USA) Can't Post

Hi there,

I have a timestamp that currently shows Mountain Time and I wish to have it read for EST (USA)'s the current script. Any suggestions of how to change it? Thanks!


####### get the time #######

($min, $hour, $day, $mon, $year) =
(localtime (time))[1,2,3,4,5];
if (length($min) < 2) {
$min = "0" . $min;
$ampm = "am";
$ampm = "pm" if ($hour > 11);
$hour = $hour - 12 if ($hour > 12);
$current_date_time =
"$hour:$min $ampm";

$now_string = localtime;
@thetime = split(/ +/,$now_string);
@theclock = split(/:/,$thetime[3]);
$ampm = 'am';
if ($theclock[0] > 11)
$ampm = 'pm';
if ($theclock[0] == 0)
$theclock[0] = 12;
if ($theclock[0] > 12)
$theclock[0] -= 12;
$theclock[0] += 0;



Jan 21, 2000, 7:04 PM

Post #2 of 6 (2665 views)
Re: need script for time stamp for EST (USA) [In reply to] Can't Post

Is there any reason why you can just subtract the amount of seconds from the return of time() equal to what ever time zone you want relative to the servers localtime?
Something like
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

# Offset is +3 hours from the servers
# localtime
$time = time()+3*3600;
print scalar localtime($time);



Jan 22, 2000, 1:30 AM

Post #3 of 6 (2665 views)
Re: need script for time stamp for EST (USA) [In reply to] Can't Post


Thanks for your reply. The script looks so simple, and I can follow the logic of it, but where do I "place it" into my script? I'm not sure where it goes! Frown(

Maybe you could enlighten me.



Jan 22, 2000, 2:04 AM

Post #4 of 6 (2665 views)
Re: need script for time stamp for EST (USA) [In reply to] Can't Post


Sure no problem. Here is a shortened version of what it looks to me like you are trying to do.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $offset = -3;

my ($min,$hour,$mday,$isdst) = (localtime(time()+$offset*3600))[1,2,3,8];

my $time = sprintf("%2d:%02d %2s",($hour % 12 &#0124; &#0124; 12)+$isdst,$min,$hour>11?"PM":"AM");

print $time;
This is also taking into account Day Lights Savings Time.

Let me know if this helps


Jan 22, 2000, 2:54 AM

Post #5 of 6 (2665 views)
Re: need script for time stamp for EST (USA) [In reply to] Can't Post

No luck at all Scott,sorry!

I have replaced my script entirely/partly, swaped things around etc etc etc with your does not want to work.

What I still can't fathom is WHERE within MY SCRIPT does YOUR SCRIPT FIT??? This is not clear to me at all.

You will need to take it slowly as I am fairly new to this.


Jan 22, 2000, 3:19 AM

Post #6 of 6 (2665 views)
Re: need script for time stamp for EST (USA) [In reply to] Can't Post


I am sorry I did not explain it better. This block of code should replace what you have totaly. I was assuming you wanted the time in this format
6:04 AM
Let me know if this is not correct. Also I am not sure where your time code fits into your program as a whole so I will write an entire program here to show you how this works.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

# Here I am calling a subroutine with the
# parameter -3. -3 is the offset from the
# machine that is running it to the time
# zone I want the time returned in. With the
# print statement before it it will just print
# the return value.
print getTime(-3);

# Here is the subroutine getTime. It takes
# the -3 I passed to it and stores it in
# $offset and returns the time formated
sub getTime {

# When you pass a variable to a
# subroutine it is stored in the
# global array @_. shift defaults
# to shifting elements from @_ if
# no array is given
my $offset = shift;

# Here I am using the offset times
# the number of seconds in an hour
# plus the return from the time()
# function call and then passing the
# whole thing to localtime.
# localtime returns a 9 element array
# I just want certain elements so I
# specifing element 1,2,3,8 and then
# setting those into the coresponding
# variables
my ($min,$hour,$mday,$isdst) = (localtime(time()+$offset*3600))[1,2,3,8];

# This part is a little complicated
# but it basicaly puts it in the format
# 6:17 AM
my $time = sprintf("%2d:%02d %2s",($hour % 12 or 12)+$isdst,$min,$hour>11?"PM":"AM");

# now we just return the value in $time
# which is formated like we want it
return $time;
If you copy and past this into a file and
run perl on it you will get the output of time formated like 6:23 AM

I really hope this helps you. If you are still confused post your whole program here so I can look at what you want to do.


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

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