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

[MacPerl] newbie alert :-) question on the date thing...



Up till now I've done most of my coding using a language called PHP/fi
(vex.net/php)

Anyway, php has a function where you can format a date string by passing a
format string to it, like date("Y, m d H:i:s",$date) (where $date is a unix
time stamp).

I looked around but couldn't find a similar thing for perl (to my limited
knowledge). Time::Local didn't seem to be what I wanted (am I wrong
there?).

Anyway, based on the example posted by David Turley I came up with the
following:

#first variable passed is the format string,
#optional second variable is a unix time stamp, otherwise
#we assume the current time.

sub get_date($;$) {
  my $string=shift or return("error: wasn't passed a format string");
  my $time=shift;
  $time||=time;
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$daylight) = localtime($time);
  my %parse=();
  $parse{"y"}=$year;
  if($year>67) {$parse{"Y"}="19$year"} else {$parse{"Y"}="20$year"};
  #get day
  $parse{"I"} =
('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[$wd
ay];
  $parse{"D"} = ('Sun','Mon','Tue','Wed','Thur','Fri','Sat')[$wday];
  $parse{"d"}=$mday;

  #get month
  $parse{"M"} = ('Jan','Feb','Mar','Apr','May','Jun','Jul',
    'Aug','Sep','Oct','Nov','Dec')[$mon];
  $parse{"m"}=$mon+1;
  $parse{"F"}=('January','February','March','April','May','June','July',
    'August','September','October','November','December')[$mon];


  #determine AM or PM so we can use 12 hour clock
  if ($hour > 11) {
          $parse{"A"}='PM';
          $parse{"a"}='pm';
  }
  else {
          $parse{"A"}='AM';
          $parse{"a"}='am';
  }
  $parse{"H"}=$hour;
  #convert to 12 hour clock
  if ($hour > 12) {
    $parse{"h"}=($hour-12);
  } else {
    $parse{"h"}=$hour;
  }
  #add 0 where needed to minutes and seconds
  ($min < 10) ? $parse{"i"} = "0$min" : $parse{"i"}=$min;
  ($sec < 10) ? $parse{"s"} = "0$sec" : $parse{"s"}=$sec;
  foreach $key (sort keys %parse) {
     $string =~ s/$key/$parse{$key}/;
  }
  #$tmp=$string;
  return($string);
}
#test it
print &get_date("Y-m-d"), "\n";
print &get_date("F d, Y h:i:s"), "\n";
print &get_date("m/d/y"), "\n";
print &get_date("F d, Y D H:i:s"), "\n";

Now the only problem I have is, when the foreach statment goes through and
does a substitution, it might find a letter that has been put in by the
Month substitution (D,I,M,F) and screw things up...

Otherwise, it works fine for simple statements.

Anyone have any idea's on how I can 'skip' over a part that has already
been substituted?

- Paul


-------------------Kudosnet Communication Services--------------------
webmaster@kudosnet.com                                www.kudosnet.com


         FileMaker Pro ISO magazine: http://www.iso-ezine.com/
           BC Renter's Guide: http://bc-renters-guide.bc.ca/

Looking for any easy to use, and very functional web forum?
http://kudosnet.com/forum/about.phtml
-------------879 View Rd. Qualicum Beach, Canada V9K 1N3--------------



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch