datestr bug

Ben Abbott bpabbott at mac.com
Wed Jul 23 19:15:40 CDT 2008


On Wednesday, July 23, 2008, at 07:46PM, "Rob Mahurin" <rob at utk.edu> wrote:
>On Jul 23, 2008, at 5:47 PM, John W. Eaton wrote:
>> On 23-Jul-2008, Rob Mahurin wrote:
>>
>> | Hello, bug-octave.
>> |
>> | This statement
>> |
>> | > str = datestr(day, "yyyy-mm-dd\ndddd\n");
>> | > printf(str);
>> |
>> | seems to do some extra substituting:
>> |
>> | > 2009-03-14
>> | > SaturSay
>> |
>> | > 2008-11-26
>> | > WeWnesWay
>> |
>> |
>> | I'm running 3.0.1
>>
>> I think this has been reported at least once before.  See for example
>>
>>   http://www.nabble.com/datestr-weekday-strptime%2C-2-bugs-- 
>> to7650360.html#a7650360
>>
>> I'm not sure what the best fix is.  I would consider a patch.
>
>It seems like the offending line is
>
>> str = strrep (str, "d", names_d{weekday (datenum (v(i,1), v(i,2), v 
>> (i,3)))});
>
>
>in datestr.m, which appears after the "dddd" in-place substitution.   
>A possibly-good-enough patch would be to follow this with
>
>> # incomplete fix for greedy matching
>> str = strrep (str, "SunSay", "Sunday");
>> str = strrep (str, "MonMay", "Monday");
>> ...
>
>
>I think the "most correct" way to do this would be a set of regular  
>expressions to identify the location and type of each format  
>specifier, followed by a single-pass substitution.
>
>If there were a utility function with the right magic numbers in it I  
>could use strftime, which has more robust formatting anyway.  This:
>
>> printf(strftime ("%A %F %H:%M:%S %Z\n", localtime((now - 719529) 
>> *24*3600)))
>
>
>(based on the hint from "help now") seems to go the wrong way from UTC.
>
>Cheers,
>Rob

Another possible solution (hack) is to replace the offending line with

 nd  = strfind (str, "d");
 nd  = setdiff (nd, strfind (str,"day"));
 nd  = setdiff (nd, strfind (str,"dnesday"));
 str(nd) = names_d{weekday (datenum (v(i,1), v(i,2), v(i,3)))};

In either event, an additional test should be added.

%!assert(datestr(testtime+[0 0 3 0 0 0],"dddd"),"Wednesday")

Comments?

Ben




More information about the Bug-octave mailing list