Why did the date and number formats change when I moved my Perl program from Linux to Windows?
I am moving some scripts from a Linux box to a Windows box, but now any numbers or dates printed have lost their formatting (ie, numbers were rounded to 2 decimal places and dates as June 1 2010..).
I am quite new to Perl and cannot work out where they got their formatting from in the first place as there doesn't appear to be anything in the script.
I have also been searching Google for global settings and environment variables, but I can't find any reference to anything that would do this?
So far I have managed to add number formatting manually but am still a bit bewildered by how to make my dates format correctly, but both these appears to have been done else where before..
Examples
Currently I have this line:
$text->text("$r_detail->{distance}");
where "$r_detail->{distance}")
is a number and it's coming from a database, I have used sprintf('%.1f',$r_detail->{distance})
to format it.
now it is 12.0988790
once formatted it is 12.10
The date is also coming from the database and the line it's used on is similar to:
&wh($th,$td,$hrow,30,"SurveyDate:","$r_survey->{sdate}");
I can't work out how to format that at all.
The dates now look like: !2003-06-23 09:40:00!
before they were !Jun 23 2002 09:40AM!
(it's creating a PDF at the end)开发者_Python百科
Database details & possible conclusion
Dates are stored in the database in the format:'2003-06-23 09:40:00' they are in a "smalldatetime" column. The numbers are stored as floats and are to 1 or 2 decimal places. ie 2.10 or 15.8So as suggested below (and as I have also just remembered it was originally connecting to the DB with DBD::Sybase but I had to change this to DBI) it is the database connectivity that has changed my formatting.
How I eventually formatted the date
use Date::Parse;
use POSIX;
my $date ="2002-03-18 10:05:00";
my $parsedDate = str2time($date);
my $formattedDate=strftime "%d %b %Y %I:%M%p", localtime $parsedDate;
I got my answer else where as I was completely confused! I didn't realise I had to parse the date first!
As strftime
always has a format defined, I believe you take a date from some external source (e.g. the date is not formatted in perl script).
The date format of system utilities is controlled by LC_TIME
environment variable. So if you parse in your perl scripts the output of some utility, you may wish to set this property first (for example, make it system-wide setting):
# export LC_TIME=de_DE
# date
Mi 6. Okt 14:54:22 CEST 2010
# export LC_TIME=POSIX
# date
Wed Oct 6 14:54:26 CEST 2010
Try to change your settings in Control Panel → Regional and Language Settings. If it impacts the format of the date, you have environment-dependent date formatting. Then you need to check your DB client: how it handles date fields fetched from the table. Do you get $r_detail
from DBI?
Did anything else changed, maybe the database also migrated from Linux to Windows ? It may be that what you see is just the way the database returns it's date values. If so you should look for a solution in database configuration, or at least in the database client library configuration.
This citation from DBI documentation is probably relevant
Dates and times are returned as character strings in the current default format of the corresponding database engine. Time zone effects are database/driver dependent.
Perl 5 doesn't have any default formats for numbers or dates. Can you post an example of the code and the output on Linux and MS Windows?
The best way to control the format of numbers is to use printf
(prints to a file) or sprintf
(returns a string).
#!/usr/bin/perl
use strict;
use warnings;
my $n = 16;
printf "%08b %03o %02x %2.5f %010d\n", $n, $n, $n, $n, $n;
The best way to control the format of a date is to use one of the date modules or POSIX
's strftime
function.
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
my @date = localtime;
for my $format (qw(%Y-%m-%d %m/%d/%Y %d/%m/%Y)) {
print strftime($format, @date), "\n";
}
Given your update, I don't think the problem is with Perl 5, it is with your database. You either changed which database you are talking to, or the environment that controls the database is different between Linux and MS Windows. This is definitely the problem with the date, since Perl 5 doesn't know that it is a date (it just sees it as a string). And it is also true for the numbers unless you are doing math with them. A number that comes back from the database is really a string, not a number. It stays a string until you do math with it (at which time Perl 5 automagically transforms it into a number).
Since your date is not in a date format, you will need to munge it into a date format. Luckily, the format you have it in is easy to convert into the same format localtime
returns:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
my $input = "2010-06-10 08:50:18.797";
#convert input string into a tm structure like localtime returns
my @localtime = reverse split /[- :]/, $input;
$localtime[5] -= 1900;
$localtime[4]--;
print strftime("%d %b %Y %I:%M%p", @localtime), "\n";
精彩评论