Skip to content

Commit

Permalink
Merge pull request #41 from smallwins/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
spencermountain authored Feb 2, 2018
2 parents 04d8e75 + 79d897b commit a1a8008
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 77 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
* add `.era()` get/set method
* found 6 or 7 wrong offsets

## v3.0.0
## v3.0.1
* fallback to UTC, instead of PST if no `Intl` is present
* support passing-in offsets as ISO_8601 date-strings
* add epoch-seconds warning msg
* allow getting/setting new timezones
### v3.1.0
* dramatic speedup by optimizing walkTo method
4 changes: 2 additions & 2 deletions immutable.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spacetime",
"version": "3.0.1",
"version": "3.1.0",
"description": "represent dates in remote timezones",
"main": "./spacetime.js",
"license": "Apache-2.0",
Expand Down
25 changes: 9 additions & 16 deletions scratch.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const spacetime = require('./src/index')

// const s = spacetime(1517509494000, 'UTC')
// s.timezones = {
// 'UTC/fun': {
// o: 9,
// h: 'n'
// }
// }
// s.goto('UTC/fun')
// s.debug()
let s = spacetime([1990, 2, 2], 'UTC')
s.log()


// let s = spacetime([1970, 1, 1], 'UTC')
// console.log(new Date(s.epoch * 1000))
Expand All @@ -19,10 +13,9 @@ const spacetime = require('./src/index')
// new handy warning in spacetime@3.0
// Fun fact: we're only subject-to these 'off-by-thousand' date errors between Sept 2001 and Jan 2057.

console.time('test')
for (let i = 0; i < 10000; i += 1) {
let r = parseInt(Math.random() * 5, 10)
let s = spacetime([1980 + r, r, 20 + r], 'UTC')
s.format('date')
}
console.timeEnd('test')
// console.time('test')
// for (let i = 0; i < 20000; i += 1) {
// let r = parseInt(Math.random() * 5, 10)
// let s = spacetime([1980 + r, r, 20 + r], 'UTC')
// }
// console.timeEnd('test')
6 changes: 3 additions & 3 deletions spacetime.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const handleObject = function(s, obj) {
};

//find the epoch from different input styles
const parseInput = (s, input) => {
const parseInput = (s, input, givenTz) => {
//if we've been given a epoch number, it's easy
if (typeof input === 'number') {
s.epoch = input;
Expand Down Expand Up @@ -81,7 +81,7 @@ const parseInput = (s, input) => {
for (let i = 0; i < strFmt.length; i++) {
let m = input.match(strFmt[i].reg);
if (m) {
strFmt[i].parse(s, m);
strFmt[i].parse(s, m, givenTz);
return;
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/input/strParse.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const parseHour = function(s, str) {
}
}
};
const parseOffset = function(s, offset) {
const parseOffset = function(s, offset, givenTz) {
if (!offset) {
return s
}
Expand Down Expand Up @@ -52,6 +52,15 @@ const parseOffset = function(s, offset) {
let zones = s.timezones
if (zones[tz]) {
// console.log('changing timezone to: ' + tz)
//log a warning if we're over-writing a given timezone
if (givenTz && zones[givenTz] && zones[givenTz].o !== zones[tz].o) {
//don't log during our tests, either..
if (typeof process !== 'undefined' && process.env && !process.env.TESTENV) {
console.warn(' - Setting timezone to: \'' + tz + '\'')
console.warn(' from ISO string \'' + offset + '\'')
console.warn(' overwriting given timezone: \'' + givenTz + '\'\n')
}
}
s.tz = tz
}
return s
Expand All @@ -61,9 +70,9 @@ const strFmt = [
//iso-this 1998-05-30T22:00:00:000Z, iso-that 2017-04-03T08:00:00-0700
{
reg: /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})T([0-9.:]+)(Z|[0-9\-\+:]+)?$/,
parse: (s, arr) => {
parse: (s, arr, givenTz) => {
let month = parseInt(arr[2], 10) - 1;
parseOffset(s, arr[5]);
parseOffset(s, arr[5], givenTz);
walkTo(s, {
year: arr[1],
month: month,
Expand Down
6 changes: 3 additions & 3 deletions src/methods/format/unixFmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const mapping = {
QQQQ: (s) => s.quarter(),

//month
M: (s) => s.month(),
MM: (s) => s.format('month-short'),
MMM: (s) => s.format('month'),
M: (s) => s.month() + 1,
MM: (s) => pad(s.month() + 1),
MMM: (s) => s.format('month-short'),
MMMM: (s) => s.format('month'),

//week
Expand Down
74 changes: 32 additions & 42 deletions src/methods/set/walk.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,63 @@
'use strict';
const ms = require('../../data/milliseconds');

//basically, step-forward/backward until js Date object says we're there.
const walk = function(s, n, fn, unit) {
let current = s.d[fn]()
if (current === n) {
return
}
//try to get it as close as we can
let diff = (n - current)
s.epoch += ms[unit] * diff

while (s.d[fn]() < n) {
s.epoch += ms[unit];
}
while (s.d[fn]() > n) {
s.epoch -= ms[unit];
}
}
//find the desired date by a increment/check while loop
const units = {
year: {
valid: n => n > -4000 && n < 4000,
walkTo: (s, n) => {
while (s.year() < n) {
s.epoch += ms.year;
}
while (s.year() > n) {
s.epoch -= ms.year;
}
},
walkTo: (s, n) => walk(s, n, 'getFullYear', 'year')
},
month: {
valid: n => n >= 0 && n <= 11,
walkTo: (s, n) => {
while (s.month() < n) {
let current = s.d.getMonth()
if (current === n) {
return
}
//try to get it as close as we can..
let diff = (n - current)
s.epoch += ms.day * (diff * 28)
//incriment by day
while (s.d.getMonth() < n) {
s.epoch += ms.day;
}
while (s.month() > n) {
while (s.d.getMonth() > n) {
s.epoch -= ms.day;
}
},
},
date: {
valid: n => n > 0 && n <= 31,
walkTo: (s, n) => {
while (s.date() < n) {
s.epoch += ms.day;
}
while (s.date() > n) {
s.epoch -= ms.day;
}
},
walkTo: (s, n) => walk(s, n, 'getDate', 'day')
},
hour: {
valid: n => n >= 0 && n < 24,
walkTo: (s, n) => {
while (s.hour() < n) {
s.epoch += ms.hour;
}
while (s.hour() > n) {
s.epoch -= ms.hour;
}
},
walkTo: (s, n) => walk(s, n, 'getHours', 'hour')
},
minute: {
valid: n => n >= 0 && n < 60,
walkTo: (s, n) => {
while (s.minute() < n) {
s.epoch += ms.minute;
}
while (s.minute() > n) {
s.epoch -= ms.minute;
}
},
walkTo: (s, n) => walk(s, n, 'getMinutes', 'minute')
},
second: {
valid: n => n >= 0 && n < 60,
walkTo: (s, n) => {
while (s.second() < n) {
s.epoch += ms.second;
}
while (s.second() > n) {
s.epoch -= ms.second;
}
},
walkTo: (s, n) => walk(s, n, 'getSeconds', 'second')
},
millisecond: {
valid: n => n >= 0 && n < 1000,
Expand Down
2 changes: 1 addition & 1 deletion src/spacetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const SpaceTime = function(input, tz) {
}
})
//parse the various formats
handleInput(this, input)
handleInput(this, input, tz)
}

//(add instance methods to prototype)
Expand Down
18 changes: 15 additions & 3 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@ test('unix-formatting', t => {
//examples from http://unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
let arr = [
['h:mm a', '11:34 AM'],
['LL', 'Nov'],
[`yyyy.MM.dd G 'at' HH:mm:ss zzz`, '2017.Nov.16 AD at 11:34:25 Canada/Eastern'],
[`EEE, MMM d, ''yy`, 'Thu, November 16, \'17'],
['LLL', 'Nov'],
[`yyyy.MM.dd G 'at' HH:mm:ss zzz`, '2017.11.16 AD at 11:34:25 Canada/Eastern'],
[`EEE, MMM d, ''yy`, 'Thu, Nov 16, \'17'],
[`hh 'o''clock' a`, '11 oclock AM'],
['yyyyy.MMMM.dd GGG hh:mm aaa', '02017.November.16 AD 11:34 AM'],
]
arr.forEach((a) => {
t.equal(s.format(a[0]), a[1], a[0])
})

//test another date
s = spacetime([2018, 'February', 20], 'Canada/Eastern')
arr = [
['M', '2'],
['MM', '02'],
['MMM', 'Feb'],
['MMMM', 'February']
]
arr.forEach((a) => {
t.equal(s.format(a[0]), a[1], a[0])
})
t.end();
});

Expand Down

0 comments on commit a1a8008

Please sign in to comment.