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)
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.8
So 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!
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) orsprintf(returns a string).The best way to control the format of a date is to use one of the date modules or
POSIX‘sstrftimefunction.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
localtimereturns: