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. | [reply] [d/l] [select] |
|
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.)
| [reply] |
|
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.
| [reply] [d/l] |
|
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
|
|
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;
}
}
| [reply] [d/l] |
|
This doesn't look like what the original poster was after, at all.
| [reply] |
|
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
| [reply] [d/l] |
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? | [reply] |
|
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!
| [reply] |
|
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.)
| [reply] [d/l] |