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


Groups > comp.lang.javascript > #24262

Re: ISO 8601 Code Review Wanted

Newsgroups comp.lang.javascript
Date 2014-05-19 11:56 -0700
References <8eb3b763-31e1-47b3-9d26-5acbc8662216@googlegroups.com> <+qXJxDObKodTFwb+@invalid.uk.co.demon.merlyn.invalid>
Message-ID <ee22e470-d76e-4edd-aece-4886b6e031a1@googlegroups.com> (permalink)
Subject Re: ISO 8601 Code Review Wanted
From dhtml <dhtmlkitchen@gmail.com>

Show all headers | View raw


On Friday, May 16, 2014 2:26:51 PM UTC-7, Dr J R Stockton wrote:
> In comp.lang.javascript message <8eb3b763-31e1-47b3-9d26-5acbc8662216@go
> 
> oglegroups.com>, Wed, 14 May 2014 09:59:23, garrett.smith@acxiom.com
> 
> posted:
> 
> 
> 
> >// EcmaScript 5 introduced incorrect ISO8601 parsing:
> 
> 
> 
> US coders do not read international standards.
> 
Hardly anybody does. Most of the people I'm working with are actually Chinese.


> 
> 
> >function getDuration(startDate, endDate) {
> 
> 
> 
> A duration can be positive, zero, or negative.
> 
> 

Not here. We're not allowing negative durations. 



> 
> >function parseISO8601(dateStringInRange) {
> 
> 
> 
> >    month = +parts[2];
> 
> 
> 
> I would use    month = parts[2] - 1    with subsequent adjustments.
> 


Okay, so now I have:

function DateRange(start, end) {

  var startDate = parseISO8601(start),
      endDate = parseISO8601(end);

  this.duration = getDuration(startDate, endDate);

  this.startDate = startDate;
  this.endDate = endDate;
}

function getDuration(startDate, endDate) {
  var duration = endDate - startDate,
      isValid = duration >= 0;
  if(!isValid) {
    throw new RangeError("endDate, " + endDate
     + " is not greater than startDate, " + startDate);
  }
  return duration;
}

function parseISO8601(dateStringInRange) {
  var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)(?:[T|\u0020]([0-2][0-9])(?:(?:\:([0-5][0-9]))?(?:\:([0-5][0-9]))?))?\s*$/,
      date = new Date(NaN), month,
      parts = isoExp.exec(dateStringInRange);

  if(parts) {
    month = parts[2]-1;
    date.setFullYear(parts[1], month, parts[3]);
    if(month !== date.getMonth()) {
      date.setTime(NaN);
    } else if(parts[3]) {
      setTime(date, parts[4], parts[5], parts[6]);
    }
  }
  return date;
}

var undef0 = /^(undefined|00)$/;
/**
 * @param date Date object to be modified.
 * @param hh 00-24
 * @param mm 00-59
 * @param ss 00-59 (no leap seconds)
 */
function setTime(date, hh, mm, ss) {
  if((hh > 24 || mm > 59 || ss > 59)
   || hh === "24" &&
     !(undef0.test(mm) && undef0.test(ss))) {
       date.setTime(NaN);
  } else {
    date.setHours(hh||0, mm||0, ss||0);
  }
}

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


Thread

ISO 8601 Code Review Wanted garrett.smith@acxiom.com - 2014-05-14 09:59 -0700
  Re: ISO 8601 Code Review Wanted John Harris <niam@jghnorth.org.uk.invalid> - 2014-05-15 10:15 +0100
    Re: ISO 8601 Code Review Wanted Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 21:31 +0200
      Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-15 12:53 -0700
        Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-15 13:12 -0700
        Re: ISO 8601 Code Review Wanted Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-15 22:21 +0200
          Re: ISO 8601 Code Review Wanted "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-05-16 00:44 +0200
            Re: ISO 8601 Code Review Wanted Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-16 01:26 +0200
              Re: ISO 8601 Code Review Wanted "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-05-16 09:39 +0200
                bitwise vs. logical OR operator (was: ISO 8601 Code Review Wanted) Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-16 12:50 +0200
                Re: bitwise vs. logical OR operator (was: ISO 8601 Code Review Wanted) dhtml <dhtmlkitchen@gmail.com> - 2014-05-16 11:21 -0700
  Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-16 22:26 +0100
    Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-19 11:56 -0700
      Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-19 17:03 -0700
        Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-21 17:30 +0100
          Re: ISO 8601 Code Review Wanted John Harris <niam@jghnorth.org.uk.invalid> - 2014-05-22 10:20 +0100
            Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-23 20:44 +0100
              Re: ISO 8601 Code Review Wanted John Harris <niam@jghnorth.org.uk.invalid> - 2014-05-24 15:54 +0100
          Re: ISO 8601 Code Review Wanted Tim Streater <timstreater@greenbee.net> - 2014-05-22 18:46 +0100
            Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-25 22:18 -0700
              Re: ISO 8601 Code Review Wanted Tim Streater <timstreater@greenbee.net> - 2014-05-26 11:41 +0100
                Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-28 09:54 -0700
          Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-23 10:37 -0700
            Re: ISO 8601 Code Review Wanted Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-05-24 19:09 +0100
  Re: ISO 8601 Code Review Wanted dhtml <dhtmlkitchen@gmail.com> - 2014-05-23 13:14 -0700

csiph-web