[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1007] branches/1.2/LedgerSMB/Form.pm



Revision: 1007
          http://svn.sourceforge.net/ledger-smb/?rev=1007&view=rev
Author:   einhverfr
Date:     2007-03-24 17:33:10 -0700 (Sat, 24 Mar 2007)

Log Message:
-----------
Fixing date format issues with report timeframes

Modified Paths:
--------------
    branches/1.2/LedgerSMB/Form.pm

Modified: branches/1.2/LedgerSMB/Form.pm
===================================================================
--- branches/1.2/LedgerSMB/Form.pm	2007-03-24 22:35:43 UTC (rev 1006)
+++ branches/1.2/LedgerSMB/Form.pm	2007-03-25 00:33:10 UTC (rev 1007)
@@ -1362,7 +1362,17 @@
 sub db_init {
 	my ($self, $myconfig) = @_;
 	$self->{dbh} = $self->dbconnect_noauto($myconfig) || $self->dberror();
+	%date_query = (
+		'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
+		'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
+		'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
+		'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
+		'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
+	);
 
+	$self->{dbh}->do($date_query{$myconfig->{dateformat}});
+	$self->{db_dateformat} = $myconfig->{dateformat}; #shim
+
 	my $query = 
 		"SELECT t.extends, 
 			coalesce (t.table_name, 'custom_' || extends) 
@@ -2984,32 +2994,47 @@
 	($rv, $yy, $mm, $dd);
 }
 
+sub format_date {
+	# takes an iso date in, and converts it to the date for printing
+	my ($self, $date) = @_;
+	my $datestring;
+	if ($date =~ /^\d{4}\D/){ # is an ISO date
+		$datestring = $self->{db_dateformat};
+		my ($yyyy, $mm, $dd) = split(/\W/, $date);
+		$datestring =~ s/y+/$yyyy/;
+		$datestring =~ s/mm/$mm/;
+		$datestring =~ s/dd/$dd/;
+	} else { # return date
+		$datestring = $date;
+	}
+	$datestring;		
+}
 
 sub from_to {
 
-	my ($self, $yy, $mm, $interval) = @_;
+	my ($self, $yyyy, $mm, $interval) = @_;
 
 	my @t;
 	my $dd = 1;
-	my $fromdate = "$yy${mm}01";
+	my $fromdate = "$yyyy-${mm}-01";
 	my $bd = 1;
 
 	if (defined $interval) {
 
 		if ($interval == 12) {
-			$yy++;
+			$yyyy++;
 		} else {
 
 			if (($mm += $interval) > 12) {
 				$mm -= 12;
-				$yy++;
+				$yyyy++;
 			}
 
 			if ($interval == 0) {
 				@t = localtime(time);
 				$dd = $t[3];
 				$mm = $t[4] + 1;
-				$yy = $t[5] + 1900;
+				$yyyy = $t[5] + 1900;
 				$bd = 0;
 			}
 		}
@@ -3018,19 +3043,20 @@
 
 		if (++$mm > 12) {
 			$mm -= 12;
-			$yy++;
+			$yyyy++;
 		}
 	}
 
 	$mm--;
-	@t = localtime(Time::Local::timelocal(0,0,0,$dd,$mm,$yy) - $bd);
+	@t = localtime(Time::Local::timelocal(0,0,0,$dd,$mm,$yyyy) - $bd);
 
 	$t[4]++;
 	$t[4] = substr("0$t[4]",-2);
 	$t[3] = substr("0$t[3]",-2);
 	$t[5] += 1900;
 
-	($fromdate, "$t[5]$t[4]$t[3]");
+	($self->format_date($fromdate), 
+		$self->format_date("$t[5]-$t[4]-$t[3]"));
 }
 
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.