Skip to content

Commit

Permalink
Merge pull request #237 from spencermountain/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
spencermountain authored Nov 5, 2020
2 parents c5ef07e + d1a5082 commit 9a2c57a
Show file tree
Hide file tree
Showing 25 changed files with 622 additions and 96 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,7 @@ let d = spacetime.now()
d.format('nice')
//'Apr 1st, 4:32pm'
```

make sure to add this to your `tsconfig.json`:

```json
{
"compilerOptions": {
"esModuleInterop": true
}
}
```
<a href="https://github.com/spencermountain/spacetime/wiki/Typescript">typescript docs</a>

<div align="center">
<h3>
Expand Down Expand Up @@ -288,6 +279,14 @@ ISO-formatting is different, so keep on your toes.
see [more considerations and gotchas](https://github.com/spencermountain/spacetime/wiki)
#### Daylight-savings gotchas
We've written in detail about how spacetime handles Daylight-savings changes [here](https://observablehq.com/@spencermountain/spacetime-daylight-savings-time?collection=@spencermountain/spacetime)
Fall DST changes have an hour that is repeated twice. There are a lot of tricky situations that come from this.
Add 10 minutes at `1:55am`, and a spacetime diff may show `-50mins`. Within an hour of this change, some spacetime methods may be off-by-one hour.
Springtime DST changes are generally smoother than Fall ones.
## Options
#### Ambiguity warnings:
Expand Down Expand Up @@ -426,7 +425,7 @@ s.dayName()
Thank you to the amazing [timeanddate.com](https://www.timeanddate.com/)
<div align="center">
<div>Made with caution + great-patience,</div>
<div>Made with caution and patience</div>
<div>by <a href="https://spencermountain.github.io/">Spencer Kelly</a></div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion _version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = '6.8.0'
module.exports = '6.9.0'
74 changes: 64 additions & 10 deletions builds/spacetime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* spencermountain/spacetime 6.8.0 Apache 2.0 */
/* spencermountain/spacetime 6.9.0 Apache 2.0 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
Expand Down Expand Up @@ -78,15 +78,16 @@
}; // compare epoch with dst change events (in utc)


var shouldChange = function shouldChange(epoch, start, end, summerOffset, winterOffset) {
var inSummerTime = function inSummerTime(epoch, start, end, summerOffset, winterOffset) {
var year = new Date(epoch).getUTCFullYear();
var startUtc = toUtc(start, winterOffset, year);
var endUtc = toUtc(end, summerOffset, year); // simple number comparison now
var endUtc = toUtc(end, summerOffset, year); // console.log(epoch, endUtc)
// simple number comparison now

return epoch >= startUtc && epoch < endUtc;
};

var summerTime = shouldChange;
var summerTime = inSummerTime;

// it reproduces some things in ./index.js, but speeds up spacetime considerably

Expand Down Expand Up @@ -1147,6 +1148,36 @@
return s;
}
}, {
// 'q2 2002'
reg: /^(q[0-9])( of)?( [0-9]{4})?/i,
parse: function parse(s, arr) {
var quarter = arr[1] || '';
s = s.quarter(quarter);
var year = arr[3] || '';

if (year) {
year = year.trim();
s = s.year(year);
}

return s;
}
}, {
// 'summer 2002'
reg: /^(spring|summer|winter|fall|autumn)( of)?( [0-9]{4})?/i,
parse: function parse(s, arr) {
var season = arr[1] || '';
s = s.season(season);
var year = arr[3] || '';

if (year) {
year = year.trim();
s = s.year(year);
}

return s;
}
}, {
// '200bc'
reg: /^[0-9,]+ ?b\.?c\.?$/i,
parse: function parse(s, arr) {
Expand Down Expand Up @@ -1410,7 +1441,7 @@
if (m) {
var _res = strParse[i].parse(s, m, givenTz);

if (_res !== null) {
if (_res !== null && _res.isValid()) {
return _res;
}
}
Expand Down Expand Up @@ -2873,7 +2904,23 @@
var old = s.clone();
var diff = s.hour() - n;
var shift = diff * milliseconds.hour;
s.epoch -= shift;
s.epoch -= shift; // oops, did we change the day?

if (s.date() !== old.date()) {
s = old.clone();

if (diff > 1) {
diff -= 1;
}

if (diff < 1) {
diff += 1;
}

shift = diff * milliseconds.hour;
s.epoch -= shift;
}

walk_1(s, {
hour: n
});
Expand Down Expand Up @@ -3230,7 +3277,7 @@

var day = this.d.getDay();
var diff = day - want;
var s = this.subtract(diff * 24, 'hours'); //tighten it back up
var s = this.subtract(diff, 'days'); //tighten it back up

walk_1(s, {
hour: original.hour(),
Expand Down Expand Up @@ -3795,8 +3842,15 @@
want.date = old.date() + num;
}
} //ensure year has changed (leap-years)
else if (unit === 'year' && s.year() === old.year()) {
s.epoch += milliseconds.week;
else if (unit === 'year') {
var wantYear = old.year() + num;
var haveYear = s.year();

if (haveYear < wantYear) {
s.epoch += milliseconds.day;
} else if (haveYear > wantYear) {
s.epoch += milliseconds.day;
}
} //these are easier
else if (unit === 'decade') {
want.year = s.year() + 10;
Expand Down Expand Up @@ -4105,7 +4159,7 @@

var whereIts_1 = whereIts;

var _version = '6.8.0';
var _version = '6.9.0';

var main$1 = function main(input, tz, options) {
return new spacetime(input, tz, options);
Expand Down
2 changes: 1 addition & 1 deletion builds/spacetime.min.js

Large diffs are not rendered by default.

74 changes: 64 additions & 10 deletions builds/spacetime.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* spencermountain/spacetime 6.8.0 Apache 2.0 */
/* spencermountain/spacetime 6.9.0 Apache 2.0 */
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
Expand Down Expand Up @@ -72,15 +72,16 @@ var toUtc = function toUtc(dstChange, offset, year) {
}; // compare epoch with dst change events (in utc)


var shouldChange = function shouldChange(epoch, start, end, summerOffset, winterOffset) {
var inSummerTime = function inSummerTime(epoch, start, end, summerOffset, winterOffset) {
var year = new Date(epoch).getUTCFullYear();
var startUtc = toUtc(start, winterOffset, year);
var endUtc = toUtc(end, summerOffset, year); // simple number comparison now
var endUtc = toUtc(end, summerOffset, year); // console.log(epoch, endUtc)
// simple number comparison now

return epoch >= startUtc && epoch < endUtc;
};

var summerTime = shouldChange;
var summerTime = inSummerTime;

// it reproduces some things in ./index.js, but speeds up spacetime considerably

Expand Down Expand Up @@ -1141,6 +1142,36 @@ var strFmt = [//iso-this 1998-05-30T22:00:00:000Z, iso-that 2017-04-03T08:00:00-
return s;
}
}, {
// 'q2 2002'
reg: /^(q[0-9])( of)?( [0-9]{4})?/i,
parse: function parse(s, arr) {
var quarter = arr[1] || '';
s = s.quarter(quarter);
var year = arr[3] || '';

if (year) {
year = year.trim();
s = s.year(year);
}

return s;
}
}, {
// 'summer 2002'
reg: /^(spring|summer|winter|fall|autumn)( of)?( [0-9]{4})?/i,
parse: function parse(s, arr) {
var season = arr[1] || '';
s = s.season(season);
var year = arr[3] || '';

if (year) {
year = year.trim();
s = s.year(year);
}

return s;
}
}, {
// '200bc'
reg: /^[0-9,]+ ?b\.?c\.?$/i,
parse: function parse(s, arr) {
Expand Down Expand Up @@ -1404,7 +1435,7 @@ var parseInput = function parseInput(s, input, givenTz) {
if (m) {
var _res = strParse[i].parse(s, m, givenTz);

if (_res !== null) {
if (_res !== null && _res.isValid()) {
return _res;
}
}
Expand Down Expand Up @@ -2867,7 +2898,23 @@ var set = {
var old = s.clone();
var diff = s.hour() - n;
var shift = diff * milliseconds.hour;
s.epoch -= shift;
s.epoch -= shift; // oops, did we change the day?

if (s.date() !== old.date()) {
s = old.clone();

if (diff > 1) {
diff -= 1;
}

if (diff < 1) {
diff += 1;
}

shift = diff * milliseconds.hour;
s.epoch -= shift;
}

walk_1(s, {
hour: n
});
Expand Down Expand Up @@ -3224,7 +3271,7 @@ var methods$2 = {

var day = this.d.getDay();
var diff = day - want;
var s = this.subtract(diff * 24, 'hours'); //tighten it back up
var s = this.subtract(diff, 'days'); //tighten it back up

walk_1(s, {
hour: original.hour(),
Expand Down Expand Up @@ -3789,8 +3836,15 @@ var addMethods$1 = function addMethods(SpaceTime) {
want.date = old.date() + num;
}
} //ensure year has changed (leap-years)
else if (unit === 'year' && s.year() === old.year()) {
s.epoch += milliseconds.week;
else if (unit === 'year') {
var wantYear = old.year() + num;
var haveYear = s.year();

if (haveYear < wantYear) {
s.epoch += milliseconds.day;
} else if (haveYear > wantYear) {
s.epoch += milliseconds.day;
}
} //these are easier
else if (unit === 'decade') {
want.year = s.year() + 10;
Expand Down Expand Up @@ -4099,7 +4153,7 @@ var whereIts = function whereIts(a, b) {

var whereIts_1 = whereIts;

var _version = '6.8.0';
var _version = '6.9.0';

var main$1 = function main(input, tz, options) {
return new spacetime(input, tz, options);
Expand Down
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ This project follows semVer, where:
- **[patch]** is a bugfix

<!-- [planned] -->
<!-- [unreleased]
-->


### v6.9.0
- **[fix]** - dst-change issues like #236
- **[fix]** - inc/dec year issue on exact nye millisecond
- **[change]** support parsing quarter-names as input - 'q2 2001'
- **[change]** support parsing season-names as input - 'fall 2001'

### v6.8.0
- **[fix]** major DST issue #182 (thanks Boris!)
Expand Down
Loading

0 comments on commit 9a2c57a

Please sign in to comment.