tags:

views:

81

answers:

3

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!

+1  A: 

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";
Chas. Owens
I have edited my post and added code
Bex
Did any perl versions have this global formatting option then? as it seems strange I've had to add it to the script when it wasn;t there before..
Bex
You have been using Perl 5 the entire time unless you used Perl back in the late Eighties or early Nineties. The reason I say Perl 5 is that Perl 6 is now out (but still very experimental) and I like to be precise.
Chas. Owens
Oh.. ok! These scripts come from around 1999/2000.. :)
Bex
Then they were most likely written with Perl 5, the first version (non-development version) of Perl 5 was released in 1994. You can find out more history in the [`perlhist`](http://perldoc.perl.org/perlhist.html) documentation. Given that you are dealing with very old code (it probably predates the common usage of the [`strict`](http://perldoc.perl.org/strict.html) pragma), it would be a very good idea for you to read [Perl Best Practices](http://oreilly.com/catalog/9780596001735). Many things that were done back then are now considered dangerous and better methods have been invented.
Chas. Owens
@Chas I have been attempting to format my dates manually as I have tried changing my pc's locality and formatting options which made no difference. When using your example above with my date from teh DB I get the error:Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year,..... I have visited here:http://perldoc.perl.org/POSIX.html but I am just missing something.. my date is currently in the format 2010-06-10 08:50:18.797 I need to get it to 6 Oct 2010 8:50AM some how, can you help?
Bex
Not quite the answer I needed.. but you have pointed me in the right direction. Thanks
Bex
+3  A: 

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 PanelRegional 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?

dma_k
Yes $r_detail is from DBI, does that make any difference? I will check the regional date settings..
Bex
+2  A: 

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.

kriss
That's a thought.. The DB before was SqlServer2000 now it's SqlServer2008, although it doesn't explain the rounding of the numbers I don't think
Bex
The rounding is standard perl behavior, when a string looks like a number it is changed to a number (expecially when you ask for float formatting with %f as in your example).
kriss
@Bex unless you are doing math with them, it does. A number that comes back from the database is seen as a string by Perl 5 until you do math with it.
Chas. Owens