[LON-CAPA-cvs] cvs: loncom /interface lonhtmlcommon.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Sun, 01 Jun 2008 00:04:40 -0000


raeburn		Sat May 31 20:04:40 2008 EDT

  Modified files:              
    /loncom/interface	lonhtmlcommon.pm 
  Log:
  Bug 5710.
  - Values in date selectors are converted to appropriate values, as determined by timezone.
  - When retrieving new selections using get_date_from_form(), use a DateTime object which includes the timezone and the values the user chose to determine the new epoch-based UNIX time to store.     
  
  
Index: loncom/interface/lonhtmlcommon.pm
diff -u loncom/interface/lonhtmlcommon.pm:1.174 loncom/interface/lonhtmlcommon.pm:1.175
--- loncom/interface/lonhtmlcommon.pm:1.174	Wed May 28 20:19:30 2008
+++ loncom/interface/lonhtmlcommon.pm	Sat May 31 20:04:39 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.174 2008/05/29 00:19:30 raeburn Exp $
+# $Id: lonhtmlcommon.pm,v 1.175 2008/06/01 00:04:39 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -311,6 +311,7 @@
 sub date_setter {
     my ($formname,$dname,$currentvalue,$special,$includeempty,$state,
         $no_hh_mm_ss,$defhour,$defmin,$defsec,$nolink) = @_;
+    my $now = time;
     my $wasdefined=1;
     if (! defined($state) || $state ne 'disabled') {
         $state = '';
@@ -319,28 +320,25 @@
         $no_hh_mm_ss = 0;
     }
     if ($currentvalue eq 'now') {
-	$currentvalue=time;
+	$currentvalue = $now;
     }
     if ((!defined($currentvalue)) || ($currentvalue eq '')) {
 	$wasdefined=0;
 	if ($includeempty) {
 	    $currentvalue = 0;
 	} else {
-	    $currentvalue = time;
+	    $currentvalue = $now;
 	}
     }
     # other potentially useful values:     wkday,yrday,is_daylight_savings
+    my $tzname;
     my ($sec,$min,$hour,$mday,$month,$year)=('','',undef,'','','');
     if ($currentvalue) {
-	($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
-	    localtime($currentvalue);
-	$year += 1900;
+        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($currentvalue); 
     }
     unless ($wasdefined) {
+        ($tzname,$sec,$min,$hour,$mday,$month,$year) = &get_timedates($now);
 	if (($defhour) || ($defmin) || ($defsec)) {
-	    ($sec,$min,$hour,$mday,$month,$year,undef,undef,undef) = 
-		localtime(time);
-	    $year += 1900;
 	    $sec=($defsec?$defsec:0);
 	    $min=($defmin?$defmin:0);
 	    $hour=($defhour?$defhour:0);
@@ -463,9 +461,7 @@
         $cal_link = qq{<a href="javascript:$dname\_opencalendar()">};
     }
     #
-    my $dt = DateTime->from_epoch(epoch => $currentvalue)
-                     ->set_time_zone(&Apache::lonlocal::gettimezone());
-    my $tzone = ' '.$dt->time_zone_short_name().' ';
+    my $tzone = ' '.$tzname.' ';
     if ($no_hh_mm_ss) {
         $result .= &mt('[_1] [_2] [_3] ',
                        $monthselector,$dayselector,$yearselector).
@@ -486,6 +482,22 @@
     return $result;
 }
 
+sub get_timedates {
+    my ($epoch) = @_;
+    my $dt = DateTime->from_epoch(epoch => $epoch)
+                     ->set_time_zone(&Apache::lonlocal::gettimezone());
+    my $tzname = $dt->time_zone_short_name();
+    my $sec = $dt->second;
+    my $min = $dt->minute;
+    my $hour = $dt->hour;
+    my $mday = $dt->day;
+    my $month = $dt->month;
+    if ($month) {
+        $month --;
+    }
+    my $year = $dt->year;
+    return ($tzname,$sec,$min,$hour,$mday,$month,$year);
+}
 
 sub build_url {
     my ($base, $fields)=@_;
@@ -567,20 +579,33 @@
     if (defined($env{'form.'.$dname.'_month'})) {
         my $tmpmonth = $env{'form.'.$dname.'_month'};
         if (($tmpmonth =~ /^\d+$/) && ($tmpmonth > 0) && ($tmpmonth < 13)) {
-            $month = $tmpmonth - 1;
+            $month = $tmpmonth;
         }
     }
     if (defined($env{'form.'.$dname.'_year'})) {
         my $tmpyear = $env{'form.'.$dname.'_year'};
-        if (($tmpyear =~ /^\d+$/) && ($tmpyear > 1900)) {
-            $year = $tmpyear - 1900;
+        if (($tmpyear =~ /^\d+$/) && ($tmpyear >= 1970)) {
+            $year = $tmpyear;
         }
     }
-    if (($year<70) || ($year>137)) { return undef; }
+    if (($year<1970) || ($year>2037)) { return undef; }
     if (defined($sec) && defined($min)   && defined($hour) &&
-        defined($day) && defined($month) && defined($year) &&
-        eval('&timelocal($sec,$min,$hour,$day,$month,$year)')) {
-        return &timelocal($sec,$min,$hour,$day,$month,$year);
+        defined($day) && defined($month) && defined($year)) {
+        my $timezone = &Apache::lonlocal::gettimezone();
+        my $dt = DateTime->new( year   => $year,
+                                month  => $month,
+                                day    => $day,
+                                hour   => $hour,
+                                minute => $min,
+                                second => $sec,
+                                time_zone => $timezone,
+                              );
+        my $epoch_time  = $dt->epoch;
+        if ($epoch_time ne '') {
+            return $epoch_time;
+        } else {
+            return undef;
+        }
     } else {
         return undef;
     }