Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26141

Re: Date arithmetic

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Date arithmetic
Date 2014-11-15 07:13 +0100
Organization PointedEars Software (PES)
Message-ID <4665324.AV9EZNQcsx@PointedEars.de> (permalink)
References (4 earlier) <m3udpq$m9v$1@solani.org> <XnsA3E354F8EA950eejj99@194.109.133.133> <m42f3g$d4d$1@solani.org> <6814026.8sujcrue1K@PointedEars.de> <m44vbl$fdu$1@solani.org>

Show all headers | View raw


Christoph M. Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Christoph M. Becker wrote:
>>>         parts = isoExp.exec(isoDay1);
>> 
>> I find
>> 
>>   parts = isoDay1.match(isoExp);
>> 
>> easier to read.  I use RegExp.prototype.exec() only in loops, such as
>> with parsers, because with global matching there is no other way to get
>> at the submatches.
> 
> ACK.  Frankly, I had copied the regexp handling from the FAQ[1].  It
> might be appropriate to change the code there, or at least adding a
> respective comment.
> […]
> [1] <http://pointedears.de/scripts/faq/cljs/#parseDate>

Several things could be and need to be said about that code:

,----
| /**Parses string formatted as YYYY-MM-DD to a Date object.

Unusual commenting.  The “/**” of a multi-line JSDoc comment should be on 
its own line:

<http://usejsdoc.org/about-getting-started.html>

|  * If the supplied string does not match the format, an 
|  * invalid Date (value NaN) is returned.
|  * @param {string} dateStringInRange format YYYY-MM-DD, with year in

I made it a rule for myself to refer to primitive values by lowercase type 
name and object values by first-letter-uppercase type name (but for types,
I write, for example, “String type”, as it is in the ECMAScript Language 
Specification).  For formal parameters, when the object variant of a value 
of a primitive type is supported, I use the latter (a future revision of the 
ECMAScript Support Matrix [1] will explain the type system and this use of 
type names in the Matrix).

The function as it is written is capable of handling String instances as 
well (however useless they may usually seem; but you can derive a prototype 
from String.prototype, see jsx.string.unicode.WideString [2]), so I would 
write “{String}” instead to indicate that possibility.

|  * range of 0000-9999, inclusive.
|  * @return {Date} Date object representing the string.
|  */
| function parseISO8601(dateStringInRange) {
|   var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
|       date = new Date(NaN), month,

Here a Date instance is being created even before it as been ascertained 
that there is a date to represent.  This is apparently done to ensure that 
the return value always is a reference to a Date instance.

This is questionable: What is the additional value of returning a Date 
instance in that case?  If you wanted to check that the value is invalid, 
you would have to write

  if (isNaN(parseISO8601(foo)))

or

  if (isNaN(parseISO8601(foo).getTime()))

to avoid the rather inefficient [3] roundtrip ToNumber → [[DefaultValue]]
(PreferredType=Number) → RegExp.prototype.valueOf() → time value. [4]

If instead you simply returned “null” for an invalid date, then

  if (parseISO8601(foo) === null)

or

  if (parseISO8601(foo) == null)

or even

  if (!parseISO8601(foo))

would suffice.

|       parts = isoExp.exec(dateStringInRange);

This is _not_ wrong, and it might even be more efficient than 
dateStringInRange.match(isoExp), because String.prototype.match() when 
passed a reference to a RegExp instance whose global flag is not set, is a 
wrapper for RegExp.prototype.exec() per Specification. [5a]  I just find it 
less intuitive.  YMMV.

It could be considered an advantage that this does not break if 
“dateStringInRange” does not hold a string because RegExp.prototype.exec() 
does the conversion [5b].  However, from a different point of view that 
could also be considered a disadvantage – should such code not break to show 
the caller that they have called the function wrong?  The expected type of 
the parameter is documented after all.  And third,

  parts = String(dateStringInRange).match(isoExp);

fixes even that problem if it would be determined that it requires fixing.

Fourth, considering compatibility, the ECMAScript Support Matrix shows
no significant difference between RegExp.prototype.exec() and 
String.prototype.match() [5c] but for now one must keep in mind that such 
might have been be due to the fact that implementations older than 
JavaScript 1.5 have not been tested in version 2.  In this case, neither
MDN nor MSDN show difference in support [6a][6b][7], but if for some reason 
one needed to support older implementations, further research would be 
necessary.

|   if(parts) {
|     month = +parts[2];

The hard-coded type conversion here *could* be considered unnecessary …

|     date.setFullYear(parts[1], month - 1, parts[3]);
                                      ^^^^
… because it would be done here …

|     if(month != date.getMonth() + 1) {
         ^^^^^^^^
… and here implicitly anyway.

|       date.setTime(NaN);

So first we set the time value and when it does not fit, we set it to NaN, 
just to return a reference to a Date instance that we need to convert again 
to find out something went wrong?  That does not look very efficient.

|     }
|   }
|   return date;
| }
| 
`----

> Hopefully, that is possible without a formal vote. :)

AFAIK, the FAQ first needs to be written that is being voted on.
 
>> However, you may want to skip the Math.abs() to give an indication to the
>> caller which date is in the future of the other.  Rounding should also be
>> considered because those values are internally represented as
>> floating-point values.
> 
> Omitting Math.abs() may or may not be desirable, depending on the usage.

Code becomes better reusable if you make fewer assumptions.  The more it 
does that could be optional, either the less flexible (hard-coded handling) 
or the more complex (handling of optional parameters/configuration or 
different types), and less efficient as a result, it becomes.

For example, if you are simply not using Math.abs() here, the caller is 
provided with the opportunity to handle negative return values as they see 
fit in each specific use-case.

>  In pratice I would round to be on the safe side, but for demonstration
> purposes I deliberately omitted the rounding to clarify that this
> calculation is supposed to be exact, opposed to the Evertjan's suggestion.
> 
>> JFTR, JSX:date.js (see sig) contains jsx.date.dayOfYear() and optionally
>> provides Date.prototype.getDayOfYear() which can also be used to compute
>> the difference.
> 
> The calculation in jsx.date.dateOfYear() is rather intriguing; I wasn't

s/date/day/ deliberately.

> aware of that formula. Thanks.

It is an implementation of a description in a Wikipedia article, as the 
JSdoc comment shows (hence “easy-to-be found”).  You’re welcome.

_______
[1] <http://PointedEars.de/es-matrix>
[2] <http://PointedEars.de/wsvn/JSX/trunk/string/unicode.js>
[3] <http://jsperf.com/timestamp-in-javascript>
[4] <http://ecma-international.org/ecma-262/5.1/#sec-15.1.2.4>
[5a] <http://ecma-international.org/ecma-262/5.1/#sec-15.5.4.10>
[5b] <http://ecma-international.org/ecma-262/5.1/#sec-15.10.6.2>
[5c] <http://PointedEars.de/es-matrix/?filter=RegExp%5C.prototype%5C.exec%7CString%5C.prototype%5C.match>
[6a] <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Specifications>
[6b] <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Specifications>
[7] <http://msdn.microsoft.com/en-us/library/s4esdbwz(v=vs.84).aspx>
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Date arithmetic Swifty <steve.j.swift@gmail.com> - 2014-11-11 12:38 +0000
  Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-11-11 15:10 +0100
    Re: Date arithmetic Swifty <steve.j.swift@gmail.com> - 2014-11-11 22:07 +0000
      Re: Date arithmetic Tim Streater <timstreater@greenbee.net> - 2014-11-11 22:37 +0000
        Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-12 02:40 +0100
          Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-11-12 08:21 +0100
            Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 15:27 +0100
              Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 21:26 +0100
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-14 14:16 +0100
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-15 07:13 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-15 19:36 +0000
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-16 17:24 +0100
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-16 18:49 +0100
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-17 00:14 +0100
                Re: Date arithmetic Dr J R Stockton <J.R.Stockton@physics.org> - 2014-11-16 10:51 -0800
                Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-11-16 20:17 +0100
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-17 00:52 +0100
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-17 01:11 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-18 16:18 +0000
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-19 11:00 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-21 22:58 +0000
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-22 18:33 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-23 19:38 +0000
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-24 01:14 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-25 21:17 +0000
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-26 18:25 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-27 19:34 +0000
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-28 01:25 +0100
                Re: Date arithmetic Dr J R Stockton <J.R.Stockton@physics.org> - 2014-11-28 05:29 -0800
                Re: Date arithmetic Andrew Poulos <ap_prog@hotmail.com> - 2014-11-28 11:44 +1100
                Re: Date arithmetic Dr J R Stockton <J.R.Stockton@physics.org> - 2014-11-28 04:52 -0800
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-28 14:24 +0100
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-28 14:47 +0100
                Re: Date arithmetic Dr J R Stockton <J.R.Stockton@physics.org> - 2014-11-28 06:12 -0800
                Re: Date arithmetic Andrew Poulos <ap_prog@hotmail.com> - 2014-11-29 16:59 +1100
                Re: Date arithmetic John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-29 20:05 +0000
                Re: Date arithmetic Joao Rodrigues <groups_jr-1@yahoo.com> - 2014-11-29 23:47 -0200
                Re: Date arithmetic Andrew Poulos <ap_prog@hotmail.com> - 2014-12-01 10:44 +1100
                Re: Date arithmetic Tim Streater <timstreater@greenbee.net> - 2014-11-30 23:54 +0000
                Re: Date arithmetic Andrew Poulos <ap_prog@hotmail.com> - 2014-12-01 13:01 +1100
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-17 01:34 +0100
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-17 01:41 +0100
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-18 19:06 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-18 16:26 +0000
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-19 17:52 +0100
                Re: Date arithmetic Gene Wirchenko <genew@telus.net> - 2014-11-19 09:44 -0800
                Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-11-19 19:34 +0100
                Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-19 19:13 +0000
                Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-11-19 22:18 +0100
                Re: Date arithmetic Gene Wirchenko <genew@telus.net> - 2014-11-19 13:43 -0800
                Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-19 22:40 +0000
                Re: Date arithmetic Tim Streater <timstreater@greenbee.net> - 2014-11-19 22:51 +0000
                Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-19 23:35 +0000
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-19 23:10 +0100
                Re: Date arithmetic Tim Streater <timstreater@greenbee.net> - 2014-11-19 22:32 +0000
                Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-19 23:26 +0000
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-20 00:47 +0100
                Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-20 00:17 +0000
                Re: Date arithmetic Dr J R Stockton <J.R.Stockton@physics.org> - 2014-11-20 01:18 -0800
                Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-20 13:47 +0000
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-21 22:29 +0000
                Re: Date arithmetic Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-11-19 19:24 -0200
                Re: Date arithmetic Gene Wirchenko <genew@telus.net> - 2014-11-19 13:40 -0800
                Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-19 23:40 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-18 16:44 +0000
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-19 11:20 +0100
                Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-14 20:45 +0000
                Re: Date arithmetic "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-11-15 14:13 -0800
                Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-15 23:42 +0100
      Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-11-12 00:23 +0100
      Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-12 13:24 +0000
        Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-12 14:43 +0100
          Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-12 19:48 +0100
            Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 01:18 +0100
          Re: Date arithmetic Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-12 20:19 +0000
            Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 03:42 +0100
    Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-12 20:06 +0000
    Re: Date arithmetic RobG <rgqld@iinet.net.au> - 2014-11-23 20:51 -0800
      Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-25 20:08 +0000
        Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-26 01:31 +0100
        Re: Date arithmetic RobG <rgqld@iinet.net.au> - 2014-12-08 19:42 -0800
      Re: Date arithmetic "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-26 01:40 +0100
      Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-26 22:07 +0100
        Re: Date arithmetic RobG <rgqld@iinet.net.au> - 2014-12-08 21:41 -0800
          Re: Date arithmetic "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-12-09 09:47 +0100
          Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-12-09 14:17 +0100
          Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-12-10 23:34 +0000
            Re: Date arithmetic RobG <rgqld@iinet.net.au> - 2014-12-10 17:21 -0800
              Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-12-13 17:19 +0000
          Re: Date arithmetic RobG <rgqld@iinet.net.au> - 2014-12-10 18:35 -0800
  Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-11 22:29 +0100
    Re: Date arithmetic Swifty <steve.j.swift@gmail.com> - 2014-11-11 21:53 +0000
      Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-11 23:43 +0100
        Re: Date arithmetic John Harris <niam@jghnorth.org.uk.invalid> - 2014-11-12 15:49 +0000
  Re: Date arithmetic Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-11-12 19:58 +0000
  Re: Date arithmetic Dr.Kral@nyc.rr.com - 2014-11-13 20:24 -0500
    Re: Date arithmetic Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-14 04:34 +0100

csiph-web