Gossamer Forum
Home : General : Perl Programming :

Time/Date

Quote Reply
Time/Date
Can someone please recommend the best way to convert time() into a nice date/time. It must include AM/PM and show the localtime for each visitor. A standard perl function/module would be ideal - I don't want something I need to download from CPAN. eg...

Wed Dec 5 2001 21:12:56 PM

Im currently using

sub date {

return localtime shift

}

...but that doesn't do what I want.

LOL good job I double checked that, I had:

sub date {

return localtime shit

}

Last edited by:

PaulW: Dec 5, 2001, 2:08 PM
Quote Reply
Re: [PaulW] Time/Date In reply to
No-one can think of a way?
Quote Reply
Re: [PaulW] Time/Date In reply to
return scalar(localtime(time));

will return:
Thu Dec 6 21:15:19 2001

or you can do:
use POSIX qw(strftime);
return strftime "%a %b %d %Y %H:%M:%S", localtime;

will return:
Thu Dec 06 2001 21:18:10

then you could have some If then action to add a AM/PM

Not exactly what you said, but something.
Quote Reply
Re: [paulmach] Time/Date In reply to
>>return scalar(localtime(time));<<

Thanks, but thats already what Im doing with return localtime shift and only returns the server localtime, I need the localtime for each visitor..
Quote Reply
Re: [PaulW] Time/Date In reply to
You can use Javascript to get the vistors Localtime. You won't be able to get it using Perl.
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] Time/Date In reply to
>>You won't be able to get it using Perl.
<<

There must be a way. Although thinking about it, maybe not as with forums like vb/gForum you need to enter your timezone.

Hm javascript may be ok as the routine that calls the date routine outputs html.

Last edited by:

PaulW: Dec 7, 2001, 8:00 AM
Quote Reply
Re: [PaulW] Time/Date In reply to
The only way you could sort of get close is to use some code like Gossamer-Threads used on there main page before which displayed a message saying how is it in $yourcountry - and it would show the country where the person is from. If you modified this to have the time difference of those countries and adjusted the date accordingly you could improve the accuracy.

The only problem with that is that a lot of countries have more then 1 time zone, like the East Coast of Australia is 2 hours in front of the west coast... Javascript is basically the only option you have. No server-side application will be able to tell.
Cheers,
Michael Bray
Quote Reply
Re: [Michael_Bray] Time/Date In reply to
Implementation would depend on exactly what you are trying to do, but what about getting the user's time with javascript, then passing that value to a perl script through either a hidden field or query string?

--
Matt G
Quote Reply
Re: [Matt Glaspie] Time/Date In reply to
Well basically the timestamp is being selected from mysql and displated on the main page of the script.

The script is a threaded chat script and so the timestamp relates to the time the thread was started. I'd like to show the times as the viewers localtime.

It is a perl routine that converts the stamp into a date but it is returned to a routine that outputs the html table and that is returned as a tag into the main template ..eg <%top10%>

If someone could give me some example JS I'll see if I can fit it in somewhere.

Thanks.
Quote Reply
Re: [Matt Glaspie] Time/Date In reply to
Actually don't worry about it, I'm using an authentication/member system anyway so I'll just let people set their timezone and then calculate it in perl

Thanks.
Quote Reply
Re: [PaulW] Time/Date In reply to
Paul,

I was going to suggest something like that as a possibility.

However, if you want to automate it, you can give this a try as a start:

Code:
<script language="JavaScript">
<!--

//set time variables
var usertime = new Date()
var gmtoffset = usertime.getTimezoneOffset()
var gmthours = -(gmtoffset / 60)

// Get cookies
var allcookies = document.cookie;

// Look for cookie named "GMThours"
var posgmt = allcookies.indexOf("GMThours=");

// set cookie "GMThours" to gmthours if it does not exist
if (posgmt == -1) {
document.cookie = "GMThours=" + gmthours;
}

//-->
</script>

This will set a cookie "GMThours" with the value of the users offset (in hours) to GMT. So, EST, for example is set to -5. Then you should be able to pull this into a perl script and make the appropriate calculations to adjust dates/times to local for each user...

Hope that helps.

Smile

PS: The above is not extensively tested...

--
Matt G
Quote Reply
Re: [Matt Glaspie] Time/Date In reply to
Ah thanks, I'll try that. Although I guess it wont work until they refresh the page on their first visit or if they delete the cookie.

Last edited by:

PaulW: Dec 8, 2001, 12:24 PM
Quote Reply
Re: [PaulW] Time/Date In reply to
True, but you could put scripting like this on a page before they get to your chat area (i.e. like your home page)...

And, if you're concerned about people not using cookies or deleting them, you could pass the GMThours offset parameter through a query string instead of storing it as a cookie... basically, you could add something like:

document.write("<a href=YourChatScript.cgi?gmt=" + gmthours + ">Click here</a><br>");

to the above code and place it on your home page ...

Of course, then there's the possibility some users won't be javascript-enabled...

--
Matt G