Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Local Date Format

by SmokeyB (Scribe)
on Feb 22, 2005 at 15:50 UTC ( [id://433364]=perlquestion: print w/replies, xml ) Need Help??

SmokeyB has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks

I've searched around, but couldn't quite find the right answer to my question. Is it possible to determine the OS date format (depending which country you are in) and use that format within all of your scripts?

Any help would be great, thanks!

Replies are listed 'Best First'.
Re: Local Date Format
by gaal (Parson) on Feb 22, 2005 at 15:54 UTC
    Use POSIX::strftime for that purpose. If you want to find out the template to treat it itself as data, the best I can think of is to look at LC_TIME and do with it what strftime did.
      Thanks for the help. I'm still a little confused on how I can dertermine the date format according to the OS settings. (is it mm/dd/yyyy or yyyy/mm/dd, etc.)

        The POSIX::strftime routine has several format specifiers (%B, %c, et al) which will behave correctly for whatever the current locale is. You don't need to worry about the components, just use one of these (%x for your particular question) and let it worry about it. See your platform's man strftime for more details on the specifiers.

        This really depends on the OS - on this here system I would probably start by grovelling around in the output from locale LC_TIME however this is not going to be portable and as you don't say what OS you are on you are unlikely to get a more useful response.

        /J\

        How portable do you need this to be? On unix, this oughta work, but is dirty:

        my $date_fmt = grep s/^date_fmt="(.*)"$/$1/, `locale -k LC_TIME`;

        You could do this in c (one hopes) on any POSIX system — possibly inlining it in Perl with Inline::C — with the setlocale() function; make the second paramater NULL to query the current locale.

      Hope this helps, just pass the subroutine a 1 to have it return month/day/year ex: 02/22/2005 or pass it nothing to return $Year$RealMonth${Day}_$Hour${Minute} ex:20050222_122. You can of course play around with the output to return diff formats
      sub getdate{ my $ret = "$_[0]"; # Get the all the values for current time my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOf +Year, $IsDST) = localtime(time); # In Perl, you'll need to increment the month by 1 my $RealMonth = $Month + 1; # Months of the year are not zero-base +d # Perl code will need to be adjusted for one-digit months if($RealMonth < 10) { $RealMonth = "0" . $RealMonth; # add a leading zero to one-digi +t months } # How to add a leading zero in Perl if($Day < 10) { $Day = "0" . $Day; # add a leading zero to one-digit days } # How to use modulo arithmetic in Perl if($Year >= 100) { $Fixed_Year = ($Year % 100); } else { $Fixed_Year = $Year; } # 1900 is subtracted from the value returned from localtime # Add it back in to get a 4-digit year $Year += 1900; # Format the string the way we want if ($ret == "1"){ $today = "$RealMonth\/$Day\/$Year"; return $today; } else{ $today = "$Year$RealMonth${Day}_$Hour${Minute}"; return $today; } }
        This doesn't look like what the original poster was after, at all.

        You should check out POSIX::strftime, too. :)

        use POSIX qw(strftime); print "mm/dd/yyyy:\t". strftime('%d/%m/%Y', localtime time) . $/; print "yyyymmdd_hhmm:\t". strftime('%Y%m%d_%H%M', localtime time) . $/ +;

        These also have the benefit of zero-padding the date and time components, so they will sort correctly if used in a filename (for example).

        mhoward - at - hattmoward.org
Re: Local Date Format
by boat73 (Scribe) on Feb 22, 2005 at 16:57 UTC
    Could you give an example of what you want the output to look like?
      Sorry if this is causing confusion, I guess I wasn't too clear in my request.

      I'm curious to know if it is possible to detect the OS's (either window, unix, etc.) current date format (From region to region these settings will change) and apply that format to display within my scripts.

      I hope this is clear, thanks!
        This oneliner might help you find what you need... Just try it!

        perl -e "use POSIX qw(strftime setlocale LC_ALL LC_CTYPE);my ($os)=str +ftime(\"%a [%c]\", localtime); my ($loc) = POSIX::setlocale( &POSIX:: +LC_ALL,'en' ); my ($en)= strftime(\"%a [%c]\", localtime);print \"$os + \n$en\n\";" Mar [22/02/2005 21:41:46] Tue [2/22/2005 9:41:46 PM]

        %c prints the default date accepted by the local OS. Or by the LC_ALL requested. (like in 'C' programing, I have heard. So this is obvious for those programers.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://433364]
Approved by Corion
Front-paged by holli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found