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

Re: [MacPerl-AnyPerl] Dates, times, and month names



Eric Albert wrote:
> 
> I *know* this is an FAQ.  I'm sure of it.  I just can't find the answer
> that I'm looking for in a rather exhaustive DejaNews search, or via perldoc
> -q, etc., and I'm certain that somebody here knows the answer.  I also know
> that this has zilch to do with the Mac, but, well, it has a better chance
> of being read here than in comp.lang.perl.*, and people here are probably
> friendlier about stupid questions. :)
> 
> Anyway, I'm stuck with a Sybase database here (blech!).  Sybase's datetime
> data type is returned from database calls as a string formatted like this:
> 'Apr 26 1999  4:21:00:000PM'  It's always in that format -- "mon dd yyyy
> hh:mm:sssPM" (or AM) if you read the field directly (yes, you can use
> concatenated converts...not pretty, and I'm looking for a Perl solution so
> I can abstract it better).

Your example string is not consistent with that format...

 4:21:00:000PM
hh:mm:sssPM

I'll assume it's a typo in the example string.


> Now, I'm looking to convert this to a formatted string like that of
> strftime, probably using Time::CTime, that looks like this "April 26, 1999
> 4:21 PM".  That's trivial, except in the conversion from 'Apr' to 'April'.

Because of the way this wrapped, I can't tell whether you want to
preserve the space padding for the hour...


> If I don't want to hardcode my months into a hash -- it'd be nice to avoid
> it, just for reasons of laziness and curiousity -- is there any way to do
> the translation?  It seems that timelocal and related functions all require
> month *numbers*; does anything allow a name?
> 

Well, the months have to be hardcoded somewhere, if not in your code then
in the module you use to do it.


With a hash of month abbreviations, you could just do a simple
substitution.

%months = qw(
    Jan January
    Feb February
    Mar March
    Apr April
    May May
    Jun June
    Jul July
    Aug August
    Sep September
    Oct October
    Nov November
    Dec December
);

$date =~
  s/
    ^
    ([a-z]{3})\ +        # month abbreviation
    (\d+)\ +             # day of month
    (\d+)\ +             # year
    (\d+:\d+)            # hours and minutes
    :(\d+)\ *            # seconds
    ([ap]m)              # am-pm
    $
   /
    ($months{$1} || $1) .    # month name
    " $2, $3 $4 $6"          # day, year hh:ss am-pm
   /xie;



For a more general solution, you might be interested in the Date::Parse
module.


Ronald

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org