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

Re: [MacPerl] time functions with MacPerl 5.1.3.r2



On 3/3/45; 7:00:00 PM, David Turley wrote:

>&get_date;
>print $date;
>
>sub get_date {

[snip]

>}

>>David Beck wrote:
>>I have read the several recent postings about the time formatting functions
>>in timelocal.pl (warned not to use!) and Time::Local. However, I can't even
>>do something as simple as getting the current system time, forget
>>formatting.

why not just:

print scalar localtime;

[on my Mac, scalar localtime prints out like this, for the example 
below...]

Thu Aug  14 22:48:09 1997

to output in a different format, split on /\s+/, then manipulate the 
parts:

print &get_date;

sub get_date {
     $ampm = "AM";
     @now = split /\s+/, scalar localtime;
     
     ## the next two lines work unless the above call was made
     ## right before midnight/end of month and the next after
     ## I include the next lines to show how easy it can be
     ## to get what you want with minimal code

     $now[0] = ( Sunday, Monday, Tuesday, Wednesday,
          Thursday, Friday, Saturday )[(localtime)[6]] . ',';

     $now[1] = ( January, February, March, April, May, June,
          July, August, September, October, November, December )
          [(localtime)[4]];

     @hour = split ':', $now[3];
     pop @hour; ## drop seconds
     if ( $hour[0] >= 12 ){ $hour[0] -= 12; $ampm = "PM" }
          elsif ( $hour[0] == 0 ){ $hour[0] = 12 }
     $now[3] = ( join ':', @hour ) . ' ' . $ampm;
     $now[2] .= ', ' . ( pop @now ) . ',';
     return join ' ', @now;
}#endsub get_date

# prints out Thursday, August 14, 1997, 10:48 PM


This is a quick routine to use for, say, timestamping files on your 
machine.  It's not really portable unless you know the output format for 
scalar localtime on the other machine.

scalar localtime is portable everywhere, however (AFAIK).


cheers,

Dave Beverly

---
smash the control images...
smash the control machines...

just say no to Bill
(Gates or Clinton, take yer pick)


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