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

[MacPerl] answer(?) on the date thing...



OK, I think I figured out a workaround. Would appreciate comments though
(if any).

I split the text and numerical substitution into two operations (two
foreach statments), doing the numbers first.

Then I also change the 'keys' used for the text based variables to letter's
that aren't used in the resulting strings...

The only thing I don't like about this solution is using Q for full day
name, W and w for month names etc...

Comments?

- Paul

print "Content-type: text/html\n\n";
# pass a string like
#Y eg, 1997 or 2000
#y eg 97 or 00
#w eg Nov
#m eg 11
#W eg November
#q eg Fri
#Q eg Friday
#d eg 27
#z day of year eg 299
#H hours in 24 hour format eg 13
#h hours in 12 hour format eg 1
#i minutes eg 5
#s seconds eg 40
#U seconds since epoch eg. 814807830
#X AM/PM
#x am/pm

#foreach $parm (@params) {
#$string =~ s/{$i}/$parm/g ;
#$i++;
#}

#@blah=split/ */,
'januaryfebuaraymarchaprilmayjunejulyaugustseptemberoctobernovemberdecemberJ
ANUARYFEBUARAYMARCHAPRILMAYJUNEJULYAUGUSTSEPTEMBEROCTOBERNOVEMBERDECEMBER';

#$fum='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
#foreach $blah (@blah){
#  $fum=~s/$blah//;
#}


#print "not used: $fum\n";
#kqwxzKQWXZ

#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 %parsenum=();
  my %parsetxt=();
  #TEXT
  $parsetxt{"Q"} =
('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[$wd
ay];
  $parsetxt{"q"} = ('Sun','Mon','Tue','Wed','Thur','Fri','Sat')[$wday];
  $parsetxt{"w"} = ('Jan','Feb','Mar','Apr','May','Jun','Jul',
    'Aug','Sep','Oct','Nov','Dec')[$mon];
  $parsetxt{"W"}=('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) {
          $parsetxt{"X"}='PM';
          $parsetxt{"x"}='pm';
  }
  else {
          $parsetxt{"X"}='AM';
          $parsetxt{"x"}='am';
  }

  #NUMBERS
  $parsenum{"d"}=$mday;
  $parsenum{"y"}=$year;
  if($year>67) {$parsenum{"Y"}="19$year"} else {$parsenum{"Y"}="20$year"};

  #get month
  $parsenum{"m"}=$mon+1;


  $parsenum{"H"}=$hour;
  #convert to 12 hour clock
  if ($hour > 12) {
    $parsenum{"h"}=($hour-12);
  } else {
    $parsenum{"h"}=$hour;
  }
  #add 0 where needed to minutes and seconds
  ($min < 10) ? $parsenum{"i"} = "0$min" : $parsenum{"i"}=$min;
  ($sec < 10) ? $parsenum{"s"} = "0$sec" : $parsenum{"s"}=$sec;

  #do numbers first
  foreach $key (sort  keys %parsenum) {
     $string =~ s/$key/$parsenum{$key}/;
  }
  foreach $key (sort  keys %parsetxt) {
     $string =~ s/$key/$parsetxt{$key}/;
  }


  return($string);
}
#test it
print &get_date("Y-m-d"), "\n";
print &get_date("Q d, Y h:i:s"), "\n";
print &get_date("m/d/y"), "\n";
print &get_date("w d, Y q H:i:s"), "\n";

print "done!";
#{$parse{$b} <=> {$parse{$a} }


-------------------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