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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing time/dates in different countries? 1

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

I'm trying to write a script, that will show the times in different countres.

For example, UK, USA (maybe needs different "zones" ?), France, Germany, Iceland, etc)

I thought DateTime::Locale would do this - but I can't seem to figure out how :/

Any ideas/suggestions?

TIA

Andy
 
I have this code that does the job:

Code:
sub timeFormat {
   my ($format,$epoch,$tz) = @_;

   # This is the default time zone.
   $tz ||= 'PST';

   use Time::Format qw(time_format);
   use Time::Local;
   use Time::Zone;

   # Get GMT Time.
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime ($epoch);
   my $gm = Time::Local::timelocal ($sec,$min,$hour,$mday,$mon,$year);
   $gm = $epoch unless defined $tz;

   # Offset the time.
   if (defined $tz) {
      my $offset = tz_offset ($tz);
      $gm += $offset;
   }

   # Format the time.
   return time_format ($format,$gm);
}

Usage:

Code:
print time_format('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');

If you're storing all your site's time stamps as values of time(), this will work just fine. Just send the saved time in as the second argument, and then their time zone code after that. If you're not saving times as values of time() but rather as time stamps, use a module to convert them back into epoch time and then send that value into this sub.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Hi,

Thanks for the reply + code - that looks exactly like what I need (I'm still waiting on my host to install the required Perl modules - but from the looks of it, this code will work great :))

Cheers

Andy
 
Hi,

Sorry, didn't work :/

Code:
#!/usr/bin/perl

  print "Content-Type: text/html \n\n";

 print time_format('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');


sub timeFormat {
   my ($format,$epoch,$tz) = @_;

   # This is the default time zone.
   $tz ||= 'PST';

   use Time::Format qw(time_format);
   use Time::Local;
   use Time::Zone;

   # Get GMT Time.
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime ($epoch);
   my $gm = Time::Local::timelocal ($sec,$min,$hour,$mday,$mon,$year);
   $gm = $epoch unless defined $tz;

   # Offset the time.
   if (defined $tz) {
      my $offset = tz_offset ($tz);
      $gm += $offset;
   }

   # Format the time.
   return time_format ($format,$gm);
}

..am I missing something?

TIA

Andy
 
First thing, you should be using the strict and warnings pragmas on all your scripts unless you have a specific reason not to.

To do that, put the two following lines immediately after your shebang line.
Code:
use warnings;
use strict;
As far as what you're missing, you'll need to change the name of the sub either where you're calling it or defining it.
print [red]time_format[/red]('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');

sub [red]timeFormat[/red] {
 
Hi,

Thanks for the reply. Here is what I have now:

Code:
#!/usr/bin/perl

use warnings;
use strict;

  print "Content-Type: text/html \n\n";

# print time_format('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');
print timeFormat('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');


sub timeFormat {
   my ($format,$epoch,$tz) = @_;

   # This is the default time zone.
   $tz ||= 'PST';

   use Time::Format qw(time_format);
   use Time::Local;
   use Time::Zone;

   # Get GMT Time.
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime ($epoch);
   my $gm = Time::Local::timelocal ($sec,$min,$hour,$mday,$mon,$year);
   $gm = $epoch unless defined $tz;

   # Offset the time.
   if (defined $tz) {
      my $offset = tz_offset ($tz);
      $gm += $offset;
   }

   # Format the time.
   return time_format ($format,$gm);
}

..but when run, I get:

linkssql@undevmac cgi-bin $ perl test.cgi
Content-Type: text/html

Segmentation fault
linkssql@undevmac cgi-bin $


Any ideas?

TIA

Andy
 
Code:
[kirsle@fonality ~]$ perl timetest.pl 
Content-Type: text/html 

Monday, June 23 @ 08:41:22 PM

works for me...

put some more prints in the subroutine and find out where the segfault is occurring... like,

Code:
#!/usr/bin/perl

use warnings;
use strict;

  print "Content-Type: text/html \n\n";

# print time_format('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');
print timeFormat('Weekday, Month dd @ HH:mm:ss AM', time(), 'EDT');


sub timeFormat {
   my ($format,$epoch,$tz) = @_;

   # This is the default time zone.
   $tz ||= 'PST';

   print "including modules\n";
   use Time::Format qw(time_format);
   use Time::Local;
   use Time::Zone;

   # Get GMT Time.
   print "getting gmtime()\n";
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime ($epoch);
   print "getting timelocal()\n";
   my $gm = Time::Local::timelocal ($sec,$min,$hour,$mday,$mon,$year);
   $gm = $epoch unless defined $tz;

   # Offset the time.
   if (defined $tz) {
      print "getting tz offset\n";
      my $offset = tz_offset ($tz);
      $gm += $offset;
   }

   # Format the time.
   print "formatting time\n";
   return time_format ($format,$gm);
}

If the return line, Time::Format::time_format(), is causing the segfault, that part is optional anyway. By that point in the code, $gm is an epoch time() value, so you could do

Code:
my @time = localtime($gm);

and do the timestamp formatting yourself (see perlfunc localtime).

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top