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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Time zone

 



ke1122
Deleted

Aug 20, 2000, 5:19 PM

Post #1 of 15 (2749 views)
Time zone Can't Post

My web server is located in a different time zone than I, and when I receive emails from my perl programs, the time stamp in the email (not the header) is always off by one hour. How do I change it?

Here is the current code:

($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d 02d 02d",$hour,$min,$sec);
$year += 1900;

# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";

Any help is much appreciated.


------------------
Kenny Eaton
indywebdp.com


LeoF
Novice

Aug 23, 2000, 10:30 AM

Post #2 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Hi ke1122,

I'm in the same situation, here's what I use for a solution:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# use your time zone here
$my_time_zone = -4;
$time_dif = $my_time_zone * (60 * 60);
$sec,$min,$hour,$mday,$mon,$year,$wday) = (gmtime(time + $time_dif))[0,1,2,3,4,5,6];
$time = sprintf("%02d 02d 02d",$hour,$min,$sec);
</pre><HR></BLOCKQUOTE>

gmtime is the same as localtime but it returns the time for Greenwich, $my_time_zone is your time zone + or - Greenwich and $time_dif converts that diference to seconds to be used by gmtime.

Hope that helps

Be Well


flanman
Novice

Aug 26, 2000, 1:18 PM

Post #3 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Leof,
Thanks for posting above. I have been trying to figure this out for 2 weeks, and it worked like a charm.



------------------
flanman


ke1122
Deleted

Aug 30, 2000, 5:24 PM

Post #4 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

LeoF-

Thanks for the code, but unfortunately it returned a HTTP 500 Internal Server error.

I've emailed my web hosting company to see if they can help.

If you have any other ideas, I'd be more than happy to try them.

Thanks


------------------
Kenny Eaton
indywebdp.com


ke1122
Deleted

Sep 7, 2000, 10:17 PM

Post #5 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Thank you all for your help. For now my problem has been solved without modifying the code. I have made notes of everything discussed here and will refer back to it if needed.

Thanks again to all

------------------
Kenny Eaton
indywebdp.com


Kanji
User / Moderator

Sep 7, 2000, 10:49 PM

Post #6 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Jasmine--

Can't say I've seen any boards that I've really liked so I'd personally be inclined to write my own.

Ideally, any board you look for will have the ability to stash code seperately so it doesn't have to clutter up the posts themselves.

Rating posters by quality of answers (a la karma) instead of # of posts could be nice to, but I have reservations about its usefulness. ;)

For the interim, I'd suggest patching UBB. With all the perl knowledge available here, I can't see that being too daunting a task. ;)

Alternately, upgrade to one of the newer (or commercial?) versions of UBB that allow you to disable smilies, etc. on a post-by-post basis.


ke1122
Deleted

Sep 8, 2000, 5:25 AM

Post #7 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

LeoF-

No problem...here is the whole time/date code:

sub get_date {

# Define arrays for the day of the week and month of the year. #
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');

# Get the current time and format the hour, minutes and seconds. Add #
# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d 02d 02d",$hour,$min,$sec);
$year += 1900;

# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";

}

I did talk to my hosting company and they gave me some UNIX Telnet commands to change the time on my virtual server. That worked, so I'm not in a big hurry to fix the code above. If you have any suggestions, please let me know. I'll probably need it sometime in the future.

Thanks


------------------
Kenny Eaton
indywebdp.com


japhy
Enthusiast

Sep 8, 2000, 6:24 AM

Post #8 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Hmm, I might end up writing an article on what I'm about to explain. It's about programming more intelligently, more or less.

Take a simple function:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# DOW($N) -- returns day of week for numerical week day (0 - 6)
sub DOW {
my @days = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
return $days[shift];
}
</pre><HR></BLOCKQUOTE>

That's all well and good. It does what I ask of it -- returns the string for the day of the week. But let's look at something a bit more involving:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub getDate {
my @months = qw(
January February March April May June
July August September October November December
);
my @days = qw(
Sunday Monday Tuesday Wednesday
Thursday Friday Saturday
);

# get abbreviated names...
my @smon = map substr($_,0,3), @months;
my @sday = map substr($_,0,3), @days;

my ($mode,$time) = @_;
$mode = 'l' if not $mode; # default to long format

# get date information
# default to current time
my ($sec,$min,$hour,$day,$mon,$year,$wday) = localtime(defined $time ? $time : time);

# short date
if (lc($mode) eq 's') {
return sprintf "%s, %s %d, %d", $sday[$wday], $smon[$mon], $day, $year+1900;
}

# long date (good kisser?) (that was a joke)
if (lc($mode) eq 'l') {
return sprintf "%s, %s %d, %d, at %02d 02d 02d",
$days[$wday], $months[$mon], $day, $year + 1900, $hr, $min, $sec;
}

die "bad mode ($mode), expected 's' or 'l'";
}
</pre><HR></BLOCKQUOTE>

By the way, those angry icons (WHICH I WISH I COULD SHUT OFF) are a colon followed by a percent sign.

Now, let's say I have to call this 10,000 times. Here's a benchmark:

timethis 10000: 15 wallclock secs (15.49 usr + 0.00 sys = 15.49 CPU)

Now, I'm going to make a magical adjustment to this function... done. Now let's check the running time:

timethis 10000: 3 wallclock secs ( 4.51 usr + 0.00 sys = 4.51 CPU)

WOW! What could I possibly have done to get nearly a 350% speed increase? I only declared the @months, @days, @smon, and @sday arrays ONCE. They won't ever CHANGE, will they? Of course not, so why declare them EVERY time the function gets called?

But how did I do that? I didn't create global variables (since that would interfere if I wanted another array called @days later). I didn't make my() variables in the main program, pretty much for the same reason. But I didn't make my() variables IN the subroutine either.

I declared them in a block, and defined the subroutine in that block:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
my $x = 1;
sub next_x { return $x++ }
}
</pre><HR></BLOCKQUOTE>

The next_x() function is the ONLY function that can see that $x variable, and the ONLY function that can modify it. So I intelligently rewrote my function as:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
my @months = qw(
January February March April May June
July August September October November December
);
my @days = qw(
Sunday Monday Tuesday Wednesday
Thursday Friday Saturday
);

# get abbreviated names...
my @smon = map substr($_,0,3), @months;
my @sday = map substr($_,0,3), @days;

sub getDate {
my ($mode,$time) = @_;
# rest of function....
}
}
</pre><HR></BLOCKQUOTE>

Defining those arrays over and over again was a silly thing to do, so I fixed that.

Oh, and there might be VERY little need to use this getDate() function anyway. Calling localtime() in scalar context returns a highly usable string-formatted date. Look into it.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$date = localtime; # or localtime $seconds;
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



[This message has been edited by japhy (edited 09-08-2000).]


Kanji
User / Moderator

Sep 8, 2000, 6:34 AM

Post #9 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

 <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>By the way, those angry icons (WHICH I WISH I COULD SHUT OFF) are a colon followed by a percent sign.<HR></BLOCKQUOTE>

You can acheive such by employing a kludge such as ...

[i]:[/i]%

I really wish someone would look into replacing UBB for this board as it sucks royally.


Jasmine
Administrator / Moderator

Sep 8, 2000, 8:14 AM

Post #10 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Kanji:

Actually, we're actively looking for another board.

The problem we're finding is that we can't migrate any of the existing posts and userlists to the new board with any degree of accuracy. That's what's slowing down shifting UBB to a board that doesn't mess with anything between the

Code
 and

.

Any suggestions?


LeoF
Novice

Sep 8, 2000, 8:26 AM

Post #11 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Thanks Jeff for showing us different and more efficient ways of doing the same thing, I appreciate it.

I have posted a new topic on this forum for optimizing a function and I really wish you could help me with it, I appreciate efficient ways of programming.

I wish I could use your suggestion for $date = localtime; but my site is not in English.

Be Well


LeoF
Novice

Sep 8, 2000, 8:30 AM

Post #12 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Ok, ke1122, after Jeff's comments I can only tell you that if you still want to use my code, replace your line:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];</pre><HR></BLOCKQUOTE>

with

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

# use your time zone here
$my_time_zone = -4;
$time_dif = $my_time_zone * (60 * 60);
$sec,$min,$hour,$mday,$mon,$year,$wday) = (gmtime(defined $time ? $time : time + $time_dif))[0,1,2,3,4,5,6];</pre><HR></BLOCKQUOTE>

The above code takes into account Jeff's suggestion, and assumes that if $time exists, it has already been adjusted.

I hope this helps

Be Well

[This message has been edited by LeoF (edited 09-08-2000).]


LeoF
Novice

Sep 8, 2000, 8:39 AM

Post #13 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Hi Jasmine,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>Actually, we're actively looking for another board.

The problem we're finding is that we can't migrate any of the existing posts and userlists to the new board with any degree of accuracy. That's what's slowing down shifting UBB to a board that doesn't mess with anything between the

Code
 and

.

Any suggestions?<HR></BLOCKQUOTE>

Have you looked into Boardmaster?
http://www.beholder.net/forums/forums.shtml

it claims it has "UBB conversion scripts available (users & messages)" and the user license allows you to modify it to your heart's content as long as you leave the copyright line alone.

Support is pretty good, I'm currently modifying it to integrate it to my site, I really like it.

I hope this helps.

Be Well


[This message has been edited by LeoF (edited 09-08-2000).]


LeoF
Novice

Sep 8, 2000, 10:11 AM

Post #14 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Glad I could help flanman

Be Well


[This message has been edited by LeoF (edited 09-08-2000).]


LeoF
Novice

Sep 8, 2000, 10:15 AM

Post #15 of 15 (2748 views)
Re: Time zone [In reply to] Can't Post

Hi ke1122

Could you post that section of your code here so I can take a look at it?

It worked for flanman, and I have used it for months without any trouble

Be Well

 
 


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

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