octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #50508] datenum() crashes for non-integer mont


From: Lars Kindermann
Subject: [Octave-bug-tracker] [bug #50508] datenum() crashes for non-integer month arrays
Date: Fri, 10 Mar 2017 12:44:34 -0500 (EST)
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0

URL:
  <http://savannah.gnu.org/bugs/?50508>

                 Summary: datenum() crashes for non-integer month arrays
                 Project: GNU Octave
            Submitted by: larskindermann
            Submitted on: Fri 10 Mar 2017 05:44:33 PM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Unexpected Error
                  Status: None
             Assigned to: None
         Originator Name: 
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
                 Release: dev
        Operating System: Any

    _______________________________________________________

Details:

datenum() allows integer and fractional month inputs:


>> datestr(datenum([2017 1 1]))
ans = 01-Jan-2017

>> datestr(datenum([2017 1.5 1]))
ans = 16-Jan-2017 12:00:00


However, if fed with an array of dates, it crashes when any month value is not
integer:


>> datestr(datenum([2017 1 1; 2017 1 1]))
ans =

01-Jan-2017
01-Jan-2017


>> datestr(datenum([2017 1 1; 2017 1.5 1]))
error: monthstart(1.5): subscripts must be either integers 1 to (2^63)-1 or
logicals
error: called from
    datenum at line 154 column 9


>> datestr(datenum([2017 1.5 1; 2017 1.5 1]))
error: datenum: operator *: nonconformant arguments (op1 is 2x1, op2 is 2x1)
error: called from
    datenum at line 137 column 11


This patch solves the problem:


diff -r 10a22466008e scripts/time/datenum.m
--- a/scripts/time/datenum.m    Fri Mar 10 13:15:23 2017 +0100
+++ b/scripts/time/datenum.m    Fri Mar 10 18:34:27 2017 +0100
@@ -126,7 +126,7 @@
   month(month < 1) = 1;  # For compatibility.  Otherwise allow negative
months.
 
   ## Treat fractional months, by converting the fraction to days
-  if (floor (month) != month)
+  if any(floor (month) != month)
     fracmonth = month - floor (month);
     month = floor (month);
     if ((mod (month-1,12) + 1) == 2
@@ -134,7 +134,7 @@
       ## leap year
       day += fracmonth * 29;
     else
-      day += fracmonth * monthlength ((mod (month-1,12) + 1));
+      day += fracmonth .* monthlength ((mod (month-1,12) + 1));
     endif
   endif


Remark: there is some incompatibility with matlab which simply trunctates
fractional month while octave adds respective days. See Bug #50493 comment #4
for details.



    _______________________________________________________

File Attachments:


-------------------------------------------------------
Date: Fri 10 Mar 2017 05:44:33 PM UTC  Name: datenum.diff  Size: 734B   By:
larskindermann
patch for datenum()
<http://savannah.gnu.org/bugs/download.php?file_id=39965>

    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?50508>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]