changeset 14665:4f458e882516

Allow fractional months to datenum and correct a couple of typos in datetick (bug #36482)
author David Bateman <dbateman@free.fr>
date Mon, 21 May 2012 00:40:44 +0200
parents 161d06a52360
children 1e77f6078692
files scripts/time/datenum.m scripts/time/datetick.m
diffstat 2 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/time/datenum.m	Sun May 20 14:18:53 2012 -0400
+++ b/scripts/time/datenum.m	Mon May 21 00:40:44 2012 +0200
@@ -85,6 +85,7 @@
 
   ## Days until start of month assuming year starts March 1.
   persistent monthstart = [306; 337; 0; 31; 61; 92; 122; 153; 184; 214; 245; 275];
+  persistent monthlength = [31; 28; 31; 30; 31; 30; 31; 31; 30; 31; 30; 31];
 
   if (nargin == 0 || nargin > 6 || 
      (nargin > 2 && (ischar (year) || iscellstr (year))))
@@ -110,6 +111,19 @@
 
   month(month<1) = 1; ## For compatibility.  Otherwise allow negative months.
 
+  ## Treat fractional months, by converting the fraction to days
+  if (floor (month) != month)
+    fracmonth = month - floor (month);
+    month = floor (month);
+    if ((mod (month-1,12) + 1) == 2 && 
+        (floor (year/4) - floor (year/100) + floor (year/400)) != 0)
+      ## leap year
+      day += fracmonth * 29;
+    else
+      day += fracmonth * monthlength ((mod (month-1,12) + 1));
+    endif
+  endif
+
   ## Set start of year to March by moving Jan. and Feb. to previous year.
   ## Correct for months > 12 by moving to subsequent years.
   year += fix ((month-14)/12);
--- a/scripts/time/datetick.m	Sun May 20 14:18:53 2012 -0400
+++ b/scripts/time/datetick.m	Mon May 21 00:40:44 2012 +0200
@@ -159,7 +159,7 @@
       ## Day scale or less
       if (xmax - xmin < N / 24 / 60 / 60)
         scl = 1 / 24 / 60 / 60;
-      elseif (xmax - xmin < N / 24 / 6)
+      elseif (xmax - xmin < N / 24 / 60)
         scl = 1 / 24 / 60;
       else
         scl = 1 / 24;
@@ -186,7 +186,7 @@
       elseif (maxyear - minyear < N)
         sep = __calc_tick_sep__ (minmonth , maxmonth);
         xmin = datenum (ymin, sep * floor (minmonth / sep), 1);
-        xmax = datenum (ymin, sep * ceil (maxmonth / sep), 1);
+        xmax = datenum (ymax, sep * ceil (maxmonth / sep), 1);
         nticks = ceil (maxmonth / sep) - floor (minmonth / sep) + 1;
       else
         sep = __calc_tick_sep__ (minyear , maxyear);