[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] time, date, and timezone



>So what I'm hearing is that:
>   1) timezone data (at least, delta from GMT) is in there
>   2) at present, I can't get at it :-(
>   3) If someone would write the XS code to get at it, I could use that
>      in my MacPerl scripts
>
>Sounds like something that would be nice in the next version of MacPerl :-)
>While we're at it (;-) we could add the TZ "environment" variable, right?

This discussion makes me curious. I use the difference between time() and
gmtime() in the Mail::Sendmail module, to get the timezone. 

Would this not work on the Mac? Or did I misunderstand the problem?

Anyway, here is the code I use, and I would really like to know if it
doesn't work as expected on Macs or not (can't test it myself for now). It
should print the local time and the offset from GMT.

-------------------------------
  use Time::Local;

	$TZ = '';
  sub time_to_date {
      # convert a time() value to a date-time string according to RFC 822

      my $time = $_[0] || time(); # default to now if no argument

      my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
      my @wdays  = qw(Sun Mon Tue Wed Thu Fri Sat);

      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
          = localtime($time);

      if ($TZ eq "") {
          # offset in hours
          my $offset  = sprintf "%.1f", (timegm(localtime) - time) / 3600;
          my $minutes = sprintf "%02d", ( $offset - int($offset) ) * 60;
          $TZ  = sprintf("%+03d", int($offset)) . $minutes;
      }
      return join(" ",
                      ($wdays[$wday] . ','),
                       $mday,
                       $months[$mon],
                       $year+1900,
                       sprintf("%02d", $hour) . ":" . sprintf("%02d", $min),
                       $TZ
                 );
  } # end sub time_to_date

  print "\ntime_to_date gives: ", time_to_date;

----------------------------------

Milivoj