Skip to content

Commit

Permalink
Make robust wrt fractional numbers.
Browse files Browse the repository at this point in the history
### 1.16.1 (2015-03-26)
* Make DateTime constructor robust with respect to fractional numbers (it rounds to nearest millisecond)
* Make DateTime#add() / addLocal() / sub() / subLocal() robust to fractional amounts (works for millis through weeks, throws for months/years)
  • Loading branch information
Rogier Schouten committed Mar 26, 2015
1 parent 4f4cfc0 commit e6bc36d
Show file tree
Hide file tree
Showing 19 changed files with 383 additions and 125 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ The version of the included IANA time zone database is 2015b.
## Changelog


### 1.16.1 (2015-03-26)
* Make DateTime constructor robust with respect to fractional numbers (it rounds to nearest millisecond)
* Make DateTime#add() / addLocal() / sub() / subLocal() robust to fractional amounts (works for millis through weeks, throws for months/years)

### 1.16.0 (2015-03-26)
* Add DateTime#withZone() method to add/replace the time zone of an existing datetime

Expand Down
50 changes: 39 additions & 11 deletions dist/timezonecomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ exports.weekNumber = weekNumber;
function assertUnixTimestamp(unixMillis) {
assert(typeof (unixMillis) === "number", "number input expected");
assert(!isNaN(unixMillis), "NaN not expected as input");
assert(math.isInt(unixMillis), "integer number expected");
assert(math.isInt(unixMillis), "Expect integer number for unix UTC timestamp");
}
/**
* Convert a unix milli timestamp into a TimeT structure.
Expand Down Expand Up @@ -666,8 +666,14 @@ var TimeStruct = (function () {
}
}
// combine main and fractional part
year = math.roundSym(year);
month = math.roundSym(month);
day = math.roundSym(day);
hour = math.roundSym(hour);
minute = math.roundSym(minute);
second = math.roundSym(second);
var unixMillis = timeToUnixNoLeapSecs(year, month, day, hour, minute, second);
unixMillis = Math.floor(unixMillis + fractionMillis);
unixMillis = math.roundSym(unixMillis + fractionMillis);
return unixToTimeNoLeapSecs(unixMillis);
}
catch (e) {
Expand Down Expand Up @@ -797,10 +803,10 @@ var DateTime = (function () {
this._zone = (typeof (a2) === "object" && a2 instanceof TimeZone ? a2 : null);
var normalizedUnixTimestamp;
if (this._zone) {
normalizedUnixTimestamp = this._zone.normalizeZoneTime(a1);
normalizedUnixTimestamp = this._zone.normalizeZoneTime(math.roundSym(a1));
}
else {
normalizedUnixTimestamp = a1;
normalizedUnixTimestamp = math.roundSym(a1);
}
this._zoneDate = TimeStruct.fromUnix(normalizedUnixTimestamp);
this._zoneDateToUtcDate();
Expand All @@ -823,6 +829,13 @@ var DateTime = (function () {
assert(minute >= 0 && minute < 60, "DateTime.DateTime(): minute out of range.");
assert(second >= 0 && second < 60, "DateTime.DateTime(): second out of range.");
assert(millisecond >= 0 && millisecond < 1000, "DateTime.DateTime(): millisecond out of range.");
year = math.roundSym(year);
month = math.roundSym(month);
day = math.roundSym(day);
hour = math.roundSym(hour);
minute = math.roundSym(minute);
second = math.roundSym(second);
millisecond = math.roundSym(millisecond);
this._zone = (typeof (timeZone) === "object" && timeZone instanceof TimeZone ? timeZone : null);
// normalize local time (remove non-existing local time)
if (this._zone) {
Expand Down Expand Up @@ -1137,7 +1150,7 @@ var DateTime = (function () {
return basics.secondOfDay(this.utcHour(), this.utcMinute(), this.utcSecond());
};
/**
* UNSAFE: returns a new DateTime which is the date+time reinterpreted as
* Returns a new DateTime which is the date+time reinterpreted as
* in the new zone. So e.g. 08:00 America/Chicago can be set to 08:00 Europe/Brussels.
* No conversion is done, the value is just assumed to be in a different zone.
* Works for naive and aware dates. The new zone may be null.
Expand Down Expand Up @@ -1262,28 +1275,29 @@ var DateTime = (function () {
var targetMilliseconds;
switch (unit) {
case 0 /* Millisecond */: {
return TimeStruct.fromUnix(tm.toUnixNoLeapSecs() + amount);
return TimeStruct.fromUnix(math.roundSym(tm.toUnixNoLeapSecs() + amount));
}
case 1 /* Second */: {
return TimeStruct.fromUnix(tm.toUnixNoLeapSecs() + amount * 1000);
return TimeStruct.fromUnix(math.roundSym(tm.toUnixNoLeapSecs() + amount * 1000));
}
case 2 /* Minute */: {
// todo more intelligent approach needed when implementing leap seconds
return TimeStruct.fromUnix(tm.toUnixNoLeapSecs() + amount * 60000);
return TimeStruct.fromUnix(math.roundSym(tm.toUnixNoLeapSecs() + amount * 60000));
}
case 3 /* Hour */: {
// todo more intelligent approach needed when implementing leap seconds
return TimeStruct.fromUnix(tm.toUnixNoLeapSecs() + amount * 3600000);
return TimeStruct.fromUnix(math.roundSym(tm.toUnixNoLeapSecs() + amount * 3600000));
}
case 4 /* Day */: {
// todo more intelligent approach needed when implementing leap seconds
return TimeStruct.fromUnix(tm.toUnixNoLeapSecs() + amount * 86400000);
return TimeStruct.fromUnix(math.roundSym(tm.toUnixNoLeapSecs() + amount * 86400000));
}
case 5 /* Week */: {
// todo more intelligent approach needed when implementing leap seconds
return TimeStruct.fromUnix(tm.toUnixNoLeapSecs() + amount * 7 * 86400000);
return TimeStruct.fromUnix(math.roundSym(tm.toUnixNoLeapSecs() + amount * 7 * 86400000));
}
case 6 /* Month */: {
assert(math.isInt(amount), "Cannot add/sub a non-integer amount of months");
// keep the day-of-month the same (clamp to end-of-month)
if (amount >= 0) {
targetYear = tm.year + Math.ceil((amount - (12 - tm.month)) / 12);
Expand All @@ -1301,6 +1315,7 @@ var DateTime = (function () {
return new TimeStruct(targetYear, targetMonth, targetDay, targetHours, targetMinutes, targetSeconds, targetMilliseconds);
}
case 7 /* Year */: {
assert(math.isInt(amount), "Cannot add/sub a non-integer amount of years");
targetYear = tm.year + amount;
targetMonth = tm.month;
targetDay = Math.min(tm.day, basics.daysInMonth(targetYear, targetMonth));
Expand Down Expand Up @@ -2796,6 +2811,19 @@ function isInt(n) {
return (Math.floor(n) === n);
}
exports.isInt = isInt;
/**
* Rounds -1.5 to -2 instead of -1
* Rounds +1.5 to +2
*/
function roundSym(n) {
if (n < 0) {
return -1 * Math.round(-1 * n);
}
else {
return Math.round(n);
}
}
exports.roundSym = roundSym;
/**
* Stricter variant of parseFloat().
* @param value Input string
Expand Down
2 changes: 1 addition & 1 deletion doc/assets/js/search.js

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions doc/classes/_basics_.timestruct.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h3>constructor</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:653</li>
<li>Defined in basics.ts:659</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -204,7 +204,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:667</li>
<li>Defined in basics.ts:673</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -219,7 +219,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:672</li>
<li>Defined in basics.ts:678</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -234,7 +234,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:687</li>
<li>Defined in basics.ts:693</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -249,7 +249,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:677</li>
<li>Defined in basics.ts:683</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -264,7 +264,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:662</li>
<li>Defined in basics.ts:668</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -279,7 +279,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:682</li>
<li>Defined in basics.ts:688</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -294,7 +294,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:657</li>
<li>Defined in basics.ts:663</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -315,7 +315,7 @@ <h3>clone</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:745</li>
<li>Defined in basics.ts:751</li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns
Expand All @@ -333,7 +333,7 @@ <h3>equals</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:728</li>
<li>Defined in basics.ts:734</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -462,7 +462,7 @@ <h3>inspect</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:766</li>
<li>Defined in basics.ts:772</li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns
Expand All @@ -480,7 +480,7 @@ <h3>less<wbr>Than</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:741</li>
<li>Defined in basics.ts:747</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down Expand Up @@ -509,7 +509,7 @@ <h3>to<wbr>String</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:756</li>
<li>Defined in basics.ts:762</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -532,7 +532,7 @@ <h3>to<wbr>Unix<wbr>NoLeap<wbr>Secs</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:720</li>
<li>Defined in basics.ts:726</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -556,7 +556,7 @@ <h3>validate</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:695</li>
<li>Defined in basics.ts:701</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand All @@ -579,7 +579,7 @@ <h3>value<wbr>Of</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:749</li>
<li>Defined in basics.ts:755</li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns
Expand All @@ -597,7 +597,7 @@ <h3>year<wbr>Day</h3>
<ul class="tsd-descriptions">
<li class="tsd-description"><aside class="tsd-sources">
<ul>
<li>Defined in basics.ts:711</li>
<li>Defined in basics.ts:717</li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
Loading

0 comments on commit e6bc36d

Please sign in to comment.