Date comparisons, EU vs US vs rest of the world

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Date comparisons, EU vs US vs rest of the world

Post by Marco »

In the past hours I tried very hard to write a code able to compare a date provided by the OS (read: styled by OS locale rules) with another "fixed" date, the latter in a fixed dd/mm/yyyy hh:nn:ss format.
This proves difficult because datediff() assumes inputs styled according to the locale rules, as well as formatdate().
Which means that in US running

Code: Select all

formatdate("fixed date", "dd/mm/yyyy hh:nn:ss")
swap days and months, even if they're already in the correct place.

Probably the whole problem can be solved with a long code rich in if-loops and gettoken, which would be something against my philosophy of compactness and linearity.

I was thinking about a formatdate function a little enhanced, maybe together with a variable that return how the OS styles the dates.
Something like

Code: Select all

<date style> --- dd/mm/yyyy in EU, mm/dd/yyyy in US, yyyy/mm/dd in Japan

formatdate("input date", "input format", "output format", "shift unit", "shift num")
Of course I'm open to other, creative suggestions.

PS: the real application of this problem can be found at line #264 of the script published here, http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=8744
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Channing
Posts: 12
Joined: 25 Oct 2012 14:13

Re: Date comparisons, EU vs US vs rest of the world

Post by Channing »

Marco wrote:This proves difficult because datediff() assumes inputs styled according to the locale rules, as well as formatdate().
It seems that datediff() can recognize ISO date (YYYY-MM-DD e.g. "2012-11-05") correctly (i.e. ignoring the locale). Format your "date provided by the OS" and the fixed date both in "yyyy-mm-dd" may solve the problem.

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Date comparisons, EU vs US vs rest of the world

Post by PeterH »

I think that all functions allowing use of a variable containing a date [i.e. Format(), Formatdate(), Datediff()] should document in Help, that the input date must be specified in locale format - and maybe others like ISO.

And for script writers who supply scripts for multi-locale use (nice word, isn't it :roll: ) there should be an "official" method to specify a date and let it transform to the current locale format.
Win11 Pro 223H2 Gerrman

admin
Site Admin
Posts: 60567
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Date comparisons, EU vs US vs rest of the world

Post by admin »

PeterH wrote:I think that all functions allowing use of a variable containing a date [i.e. Format(), Formatdate(), Datediff()] should document in Help, that the input date must be specified in locale format - and maybe others like ISO.

And for script writers who supply scripts for multi-locale use (nice word, isn't it :roll: ) there should be an "official" method to specify a date and let it transform to the current locale format.
Yes, as Channing said, ISO works universally (well, not sure about Andromeda).

I'll update the Help.

PeterH
Posts: 2785
Joined: 21 Nov 2005 20:39
Location: Germany

Re: Date comparisons, EU vs US vs rest of the world

Post by PeterH »

So just add "(Format: locale or ISO)" to the description of the input variables for above mentioned commands?

And maybe as a functional expansion: if you allow "locale" as output-format for formatdate(), one would be able to translate "global ISO" to the locale format - for thouse who might prefer to work this way?
Win11 Pro 223H2 Gerrman

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: Date comparisons, EU vs US vs rest of the world

Post by Marco »

Mission accomplished!

My original problem was comparing a date as it appears in an HTTP response, like

Code: Select all

Mon, 04 Nov 2012 11:25:28 GMT
which follows the standards https://tools.ietf.org/html/rfc2616#page-20 , https://tools.ietf.org/html/rfc1123#page-55 and https://tools.ietf.org/html/rfc822#section-5 ,
with a date provided by the OS, specifically with this command

Code: Select all

property("Write", "<xy>")
whose format can vary.

Now, with what I learned here today, I just need to convert both dates to ISO and then run the comparison.

Code: Select all

 $first_date  = "Mon, 05 Nov 2012 11:25:28 GMT";
 $second_date = property("Write", "<xy>");

 //CONVERTING THE FIRST DATE TO ISO 8601
 //strip the name of the day and the GMT
 $first_date  = substr("$first_date", "5", "-4");

 //reorder the date in the yyyy-mm-dd format
 $first_date  = regexreplace("$first_date", "(\d{2}) ([a-z]{3}) (\d{4})", "$3-$2-$1");

 //replace the literal month with its numerical value
 $first_date  = replacelist("$first_date", "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec", "01,02,03,04,05,06,07,08,09,10,11,12", ",");

 //CONVERTING THE SECOND DATE TO ISO 8601
 $second_date = formatdate ("$second_date", "yyyy-mm-dd hh:nn:ss");

 //COMPARISON (IN SECONDS)
 $delay       = datediff("$first_date", "$second_date", "s");
With XY 11.70.0106 always returns 86176 seconds.
Thank you Channing for the suggestion! Looks like I already had whatever I needed.

0.02 € time...

@Don
Thank you, knowing that ISO works in all those functions is a great starting point. As PeterH says, saying it so in the Help will save a lot of headaches.
I would write that format() , formatdate () and datediff() work correctly when input is in local format and ISO format.
Also, would be great if you can shed light on the accepted separators. For local Windows offers ".", "/" and "-", while ISO 8601 ( https://en.wikipedia.org/wiki/ISO_8601 ) is more complex.

I also support PeterH's suggestion: you could add "local" and "ISO" as format shorthands to quickly convert between them.

However, I still believe there's a missing part, i.e. converting from an user defined format to either local or ISO quickly. An inverse formatdate(), so to speak, where you specify the input and it's structure.
Last edited by Marco on 05 Nov 2012 18:36, edited 1 time in total.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: Date comparisons, EU vs US vs rest of the world

Post by tiago »

Correct me if I'm wrong Marco but your system is not universal enough if it relies on a replacelist which doesnt cover

French:
Jan Feb Mar Apr May Jun Jul Aou Sep Oct Nov Dec

Spanish:
En Feb Mar Ab May Jun Jul Ag Set Oct Nov Dic

and so on.

As far as I can see you are totally relying on the fact that a given HTTP source follows the standards, am I right? Please notice I'm NOT following your project as a whole, just wondering mainly because of the OT: "Date comparisons, EU vs US vs rest of the world", so it may not be entirely or exclusively related to the XY updater script.
Power-hungry user!!!

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

Re: Date comparisons, EU vs US vs rest of the world

Post by Marco »

tiago wrote:Correct me if I'm wrong Marco but your system is not universal enough if it relies on a replacelist which doesnt cover

French:
Jan Feb Mar Apr May Jun Jul Aou Sep Oct Nov Dec

Spanish:
En Feb Mar Ab May Jun Jul Ag Set Oct Nov Dic

and so on.

As far as I can see you are totally relying on the fact that a given HTTP source follows the standards, am I right? Please notice I'm NOT following your project as a whole, just wondering mainly because of the OT: "Date comparisons, EU vs US vs rest of the world", so it may not be entirely or exclusively related to the XY updater script.
That kind of date can contain only the English months, as per standard, you can check the last link. And indeed they are the second thing I get rid of!
Yes, I'm relying on the standard.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

Post Reply