diff scripts/time/datenum.m @ 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 f3d52523cde1
children 94a8366f9f89
line wrap: on
line diff
--- a/scripts/time/datenum.m
+++ b/scripts/time/datenum.m
@@ -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);