Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POSIX::strftime 1

Status
Not open for further replies.

andy98

Programmer
Jul 7, 2000
120
GB
Hi

I am trying to configure a Bug Tracking program that is written in Perl and want to get the dates to format differently.

These are the lines of code in one of the PERL Modules that I want to change...


sub now {

my $now = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime);

return $now;
}


What I want to return is a date in the format of (dd-mm-yyyy)..


my $now = POSIX::strftime("%d-%m-%Y %H:%M:%S", localtime);


But this just fails. Can anyone help?
 
What do you mean by "just fails"? Your code worked perfectly for me.
Code:
$ perl -MPOSIX -e 'print POSIX::strftime("%d-%m-%Y %H:%M:%S", localtime),"\n"'

03-02-2004 11:21:07
 
Try this instead of using POSIX functions

Code:
(@date)=localtime;
$date[5]+=1900;$date[4]++;
$now=sprintf("%0.2d-%0.2d-%0.4d %0.2d:%0.2d:%0.2d",$date[3],$date[4]++, $date[5], $date[2],$date[1],$date[0]);
print "$now";

HTH
--Paul
 
When I try

sub now {

my $now = POSIX::strftime("%d-%m-%Y %H:%M:%S", localtime);

return $now;
}


All i get is.......

CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:


DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver]Invalid character value for cast specification (SQL-22018)(DBD: st_execute/SQLExecute err=-1) at lib/dbase.pm line 78.
DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server Driver]Invalid character value for cast specification (SQL-22018)(DBD: st_execute/SQLExecute err=-1) at lib/dbase.pm line 78.

 
I don't see why ....

This will work:

my $now = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime);

But this throws an error!

my $now = POSIX::strftime("%d-%m-%Y %H:%M:%S", localtime);
 
I would guess that you're using
Code:
$now
to insert into the database, but it's not in a date format that your SQL server understands.
 
So how do I configure SQL Server to understand my date format?
 
The Language settings on the Database Server are set to English (United States). This is probably the reason why I am getting this error, as rosenk has suggested.

Unfortunately, I cannot verify this by testing it as I cannot reboot the server.
 
Most (if not all) SQL DBs behave like that, i.e. accepting dates in yyyy-mm-dd format. This is because of the ambiguities involved in doing it any other way, for example 02/03/2004 is the second of March in Europe, while it's the third of February in the US. The yyyy-mm-dd format is the international standard for representing dates, since nobody would agree to use the *other* od.
 
I think we should all just adopt the British format. It would save us a lot of fuss over here in the UK. ;O)
 
You're probably best off keeping two variables, one for display to your users and one for inserting into the database. If all of the database interaction code is isolated, I'd make
Code:
$now
be for display and create a new variable for use with the database -- whichever way would involve the fewest changes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top