datestr bug

John W. Eaton jwe at bevo.che.wisc.edu
Thu Jul 24 20:34:04 CDT 2008


On 24-Jul-2008, Ben Abbott wrote:

| hmmm ... perhaps strrep.m could be modified to allow for a mask as a  
| fourth argument?
| 
|   For example, it might be modified to work as below ...
| 
| 	octave:#> str = "d == dddd";
| 	octave:#> mask = zeros (size (str));
| 	octave:#> [str, mask] = strrep (str, "dddd", "Wednesday", mask)
| 
| 	str = "d == Wednesday"
| 	mask = [0 0 0 0 0 1 1 1 1 1 1 1 1 1]
| 
| 	octave:#> [str, mask] = strrep (str, "d", "W", mask)
| 
| 	str = "W = Wednesday"
| 	mask = [1 0 0 0 0 1 1 1 1 1 1 1 1 1]
| 
| The new version of strrep might replace the original, a new function  
| (__strrep__.m ?) or a subfunction of datestr.
| 
| Comments/Suggestions?

Wouldn't it be better to transform the (strange) Matlab datestr format
string to an strftime format string and just use that (I think that
was already suggested)?  Here's a start:

  %C%y       yyyy    Full year                                       2005
  %y         yy      Two-digit year                                  2005
  %B         mmmm    Full month name                                 December
  %b         mmm     Abbreviated month name                          Dec
  %m         mm      Numeric month number (padded with zeros)        01, 08, 12
  ?          m       First letter of month name (capitalized)        D
  %A         dddd    Full weekday name                               Sunday
  %a         ddd     Abbreviated weekday name                        Sun
  %e         dd      Numeric day of month (padded with zeros)        11
  ?          d       First letter of weekday name (capitalized)      S
  %I or %H?  HH      Hour of day, padded with zeros if PM is set     09:00
   and %p            and not padded with zeros otherwise             9:00 AM
  %M         MM      Minute of hour (padded with zeros)              10:05
  %S         SS      Second of minute (padded with zeros)            10:05:03
	     PM      Use 12-hour time format                         11:30 PM
  ?          QQ      Quarter

You'd still need to

   handle "m", "d", and "QQ" before passing the string to strftime

   recognize PM early in order to determine what should be done with HH

   use some care to avoid incorrect substitutions (for exmaple,
   translating "mm" -> "%m" followed by translating "m", would cause
   trouble, but perhaps you could use another character for that
   temporarily, folowed by another translation?)

but I think you could avoid the problem we are having now.

Or, instead of doing a series of global substitutions on the entire
string, just walk through the string once, looking for matches and
generating the output.  That way you don't ever perform substitutions
on things that have already been substituted.  But that would probably
require nested loops, so might best be done in C++.

Ugh, what a crazy function.

jwe


More information about the Bug-octave mailing list