in reply to Local Date Format

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.

Replies are listed 'Best First'.
Re^2: Local Date Format
by SmokeyB (Scribe) on Feb 22, 2005 at 16:17 UTC
    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.

        Thanks gaal, this is a good start and I'll definately be exploring it!
Re^2: Local Date Format
by boat73 (Scribe) on Feb 22, 2005 at 17:04 UTC
    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