opto / Expression-Search-NG

update of Thunderbird addon ExpressionSearch/Gmail UI for TB 78 and later
61 stars 8 forks source link

Date format #9

Open fpierrat opened 3 years ago

fpierrat commented 3 years ago

Hi, Locale date format is accepted for date: , d: => d:15/04/2021 (french locale) works perfectly Unfortunately, it is not for before: , after: => after:15/04/2021 does not (but after:2021/04/15 is fine)

Maybe it has something to do with lines 674 and 683 in ExpressionSearchFilter.js , seeming to handle dates differently ?

674: else if (e.tok == 'date') attr = { type:nsMsgSearchAttrib.Custom, customId: 'expressionsearch#dateMatch' };

vs 683: else if (e.tok == 'before' || e.tok == 'after') attr = nsMsgSearchAttrib.Date;

Edit: I found a little time to dig a tiny little bit deeper. The magic seems to be done in the definition of dateMatch, line 180 of same file. There should be a way to define a dateGreaterThan and a dateLessThan inspired from dateMatch. So line 683 above could become (if I somehow got the logic) : else if (e.tok == 'before') attr = { type:nsMsgSearchAttrib.Custom, customId: 'expressionsearch#dateLessThan' }; else if (e.tok == 'after') attr = { type:nsMsgSearchAttrib.Custom, customId: 'expressionsearch#dateGreaterThan' };

opto commented 3 years ago

thanks for looking at that. Right now, I am in a workshop but I will look into this

thanks,

Klaus

Am 22.04.2021 um 12:14 schrieb fpierrat2:

Hi, Locale date format is accepted for |date:| , |d:| => |d:15/04/2021| (french locale) works perfectly Unfortunately, it is /not/ for |before:| , |after:| => |after:15/04/2021| does /not/ (but |after:2021/04/15| is fine)

Maybe it has something to do with lines 674 and 683 in ExpressionSearchFilter.js , seeming to handle dates differently ?

674: |else if (e.tok == 'date') attr = { type:nsMsgSearchAttrib.Custom, customId: 'expressionsearch#dateMatch' };|

vs 683: |else if (e.tok == 'before' || e.tok == 'after') attr = nsMsgSearchAttrib.Date;|

Sorry, it's the best i can do to try and help right now.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/opto/Expression-Search-NG/issues/9, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABR3YE7K34TYVCYSY3PBCLTJ7ZJ5ANCNFSM43MESY3A.

-- Dr. K. Bücher/Optosolar GmbH Tel. +49/7668 902699

Optosolar Gesellschaft für optoelektronische und solare Lösungen mbH, Hinterhofen 21, D 79291 Merdingen Geschäftsführer: Dr. Klaus Bücher, Amtsgericht Freiburg, HRB 290390, St. Nr. 07019/45002, USt.-IdNr. DE 209704999, Finanzamt Freiburg Land

fpierrat commented 3 years ago

I finally found a way. But not for all locales (couldn't find out how to know about the user's locale). I had to assume (like in msgToLocaleFormat() function somewhere else in ExpressionSearchFilter.js) that the format MUST be yyyy/mm/dd or dd/mm/yyyy (with one or 2 figures for day and month, but 4 figures mandatory for year)

So I just added a conversion for the case where a "dd/mm/yyyy" date were detected (1 regex test + 1 assignment). Also added the awaited formats in the warning message.

Thus, I replaced:

        } else try { // normal date
          let date = new Date(inValue);
          e.left.tok = date.getTime()*1000; // why need *1000, I don't know ;-)
          if ( isNaN(e.left.tok) ) {
            ExpressionSearchLog.log('Expression Search: date '+ inValue + " is not valid",1);
            return;
          }
        } catch (err) {

with:

        } else try { // normal date
          //BEG added by fpierrat, 2021/04/26
          // no idea how to get the user's locale, so I had a try considering (like in preexisting function msgToLocaleFormat() above) that
          // there can be only 2 formats: yyyy/mm/dd or dd/mm/yyyy
          let originalValue = inValue,
            match = inValue.match(/^\s*(\d{1,2})\/(\d{1,2})\/(\d{4})\s*$/);
          if (match){ inValue=[ match[3],match[2],match[1] ].join('/'); } //assume given is dd/mm/yyyy -> reorder to awaited yyyy/mm/dd
          //END  added by fpierrat, 2021/04/26
          let date = new Date(inValue);
          e.left.tok = date.getTime()*1000; // why need *1000, I don't know ;-) 
          if ( isNaN(e.left.tok) ) {
            ExpressionSearchLog.log('Expression Search: date '+ originalValue + ' is not valid, use "yyyy/mm/dd" or "dd/mm/yyyy" format.',1);
            return;
          }
        } catch (err) {

I'm not familiar with pull requests and so on, hope you can test and integrate if you're ok with the proposed modification. I'm using the modified version, seems to work like a charm so far ;-)

opto commented 3 years ago

thanks for working that out, will consider for the next version!