Skip to content

Commit

Permalink
v1.23.0: add method DateTime.exists() to validate a date in its time …
Browse files Browse the repository at this point in the history
…zone.
  • Loading branch information
Rogier Schouten committed Oct 8, 2015
1 parent 0319e7c commit 4a7b380
Show file tree
Hide file tree
Showing 49 changed files with 747 additions and 477 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,28 @@ var newCopy = amsterdamDate.clone();
// Truncate a DateTime to a date @ 00:00:00.000
var newDateTime = d.startOfDay();

// Check if a date exists

// leap years (2012 is a leap year)
tc.DateTime.exists(2012, 2, 29); // true
tc.DateTime.exists(2013, 2, 29); // false

// # days in month (April has 30 days)
tc.DateTime.exists(2012, 4, 30); // true
tc.DateTime.exists(2012, 4, 31); // false

// Daylight saving time skips hours
tc.DateTime.exists(2015, 3, 29, 2, 0, 0, 0, TimeZone.zone("Europe/Amsterdam")); // false
tc.DateTime.exists(2015, 3, 29, 1, 59, 59, 999, TimeZone.zone("Europe/Amsterdam")); // true
tc.DateTime.exists(2015, 3, 29, 3, 0, 0, 0, TimeZone.zone("Europe/Amsterdam")); // true

// Pre-1970 dates: you can allow or disallow them with the last boolean parameter
// as the IANA time zone database is not reliable prior to 1970
tc.DateTime.exists(1969, 12, 31, 23, 59, 59, 999, null, false); // false
tc.DateTime.exists(1969, 12, 31, 23, 59, 59, 999, null, true); // true
tc.DateTime.exists(1969, 12, 31, 23, 59, 59, 999, TimeZone.zone("Europe/Amsterdam"), false); // false
tc.DateTime.exists(1969, 12, 31, 23, 59, 59, 999, TimeZone.zone("Europe/Amsterdam"), true); // true

```

### Date Arithmetic
Expand Down Expand Up @@ -649,6 +671,9 @@ The version of the included IANA time zone database is 2015g.

## Changelog

### 1.23.0 (2015-10-02)
* Add a static method DateTime.exists() to see whether a given date exists in its time zone

### 1.22.2 (2015-10-02)
* Upgrade TZ database to 2015g

Expand Down
35 changes: 35 additions & 0 deletions dist/timezonecomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,41 @@ var DateTime = (function () {
var unixTimestamp = Math.round((n - 25569) * 24 * 60 * 60 * 1000);
return new DateTime(unixTimestamp, timeZone);
};
/**
* Check whether a given date exists in the given time zone.
* E.g. 2015-02-29 returns false (not a leap year)
* and 2015-03-29T02:30:00 returns false (daylight saving time missing hour)
* and 2015-04-31 returns false (April has 30 days).
* By default, pre-1970 dates also return false since the time zone database does not contain accurate info
* before that. You can change that with the allowPre1970 flag.
*
* @param allowPre1970 (optional, default false): return true for pre-1970 dates
*/
DateTime.exists = function (year, month, day, hour, minute, second, millisecond, zone, allowPre1970) {
if (month === void 0) { month = 1; }
if (day === void 0) { day = 1; }
if (hour === void 0) { hour = 0; }
if (minute === void 0) { minute = 0; }
if (second === void 0) { second = 0; }
if (millisecond === void 0) { millisecond = 0; }
if (zone === void 0) { zone = null; }
if (allowPre1970 === void 0) { allowPre1970 = false; }
if (!isFinite(year) || !isFinite(month) || !isFinite(day)
|| !isFinite(hour) || !isFinite(minute) || !isFinite(second) || !isFinite(millisecond)) {
return false;
}
if (!allowPre1970 && year < 1970) {
return false;
}
try {
var dt = new DateTime(year, month, day, hour, minute, second, millisecond, zone);
return (year === dt.year() && month === dt.month() && day === dt.day()
&& hour === dt.hour() && minute === dt.minute() && second === dt.second() && millisecond === dt.millisecond());
}
catch (e) {
return false;
}
};
/**
* @return a copy of this object
*/
Expand Down
2 changes: 1 addition & 1 deletion doc/assets/js/search.js

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions doc/classes/_basics_.timestruct.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ <h3>constructor</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:647</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L647">basics.ts:647</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -219,7 +219,7 @@ <h3>day</h3>
<div class="tsd-signature tsd-kind-icon">day<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:674</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L674">basics.ts:674</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -234,7 +234,7 @@ <h3>hour</h3>
<div class="tsd-signature tsd-kind-icon">hour<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:679</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L679">basics.ts:679</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -249,7 +249,7 @@ <h3>milli</h3>
<div class="tsd-signature tsd-kind-icon">milli<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:694</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L694">basics.ts:694</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -264,7 +264,7 @@ <h3>minute</h3>
<div class="tsd-signature tsd-kind-icon">minute<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:684</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L684">basics.ts:684</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -279,7 +279,7 @@ <h3>month</h3>
<div class="tsd-signature tsd-kind-icon">month<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:669</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L669">basics.ts:669</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -294,7 +294,7 @@ <h3>second</h3>
<div class="tsd-signature tsd-kind-icon">second<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:689</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L689">basics.ts:689</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -309,7 +309,7 @@ <h3>year</h3>
<div class="tsd-signature tsd-kind-icon">year<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:664</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L664">basics.ts:664</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -331,7 +331,7 @@ <h3>clone</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:752</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L752">basics.ts:752</a></li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns <a href="_basics_.timestruct.html" class="tsd-signature-type">TimeStruct</a></h4>
Expand All @@ -348,7 +348,7 @@ <h3>equals</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:735</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L735">basics.ts:735</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -376,7 +376,7 @@ <h3>inspect</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:773</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L773">basics.ts:773</a></li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">string</span></h4>
Expand All @@ -393,7 +393,7 @@ <h3>less<wbr>Than</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:748</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L748">basics.ts:748</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -421,7 +421,7 @@ <h3>to<wbr>String</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:763</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L763">basics.ts:763</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -443,7 +443,7 @@ <h3>to<wbr>Unix<wbr>NoLeap<wbr>Secs</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:727</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L727">basics.ts:727</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -466,7 +466,7 @@ <h3>validate</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:702</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L702">basics.ts:702</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -488,7 +488,7 @@ <h3>value<wbr>Of</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:756</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L756">basics.ts:756</a></li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">number</span></h4>
Expand All @@ -505,7 +505,7 @@ <h3>year<wbr>Day</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:718</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L718">basics.ts:718</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -527,7 +527,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> from<wbr>Date</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:515</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L515">basics.ts:515</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -564,7 +564,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> from<wbr>String</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:528</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L528">basics.ts:528</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -592,7 +592,7 @@ <h3><span class="tsd-flag ts-flagStatic">Static</span> from<wbr>Unix</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:505</li>
<li>Defined in <a href="https://github.com/SpiritIT/timezonecomplete/blob/master/lib/basics.ts#L505">basics.ts:505</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
Loading

0 comments on commit 4a7b380

Please sign in to comment.