Skip to content

Commit

Permalink
Merge pull request #30 from smallwins/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
spencermountain authored Nov 17, 2017
2 parents 9e7c750 + 5d910c4 commit f19d89a
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 74 deletions.
35 changes: 20 additions & 15 deletions builds/spacetime.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* spacetime v2.1.0
/* spacetime v2.1.1
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.spacetime = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
Expand Down Expand Up @@ -376,7 +376,7 @@ module.exports={
"n"
],
"Caracas": [
-4.5,
-4,
"n"
],
"Catamarca": [
Expand Down Expand Up @@ -1022,6 +1022,10 @@ module.exports={
7,
"n"
],
"Barnaul":[
7,
"n"
],
"Beirut": [
3,
"n",
Expand Down Expand Up @@ -2117,7 +2121,7 @@ module.exports={
"n"
],
"Easter": [
-5,
-6,
"s",
"05/13:21->08/12:23"
],
Expand Down Expand Up @@ -2277,7 +2281,7 @@ module.exports={
},{}],3:[function(_dereq_,module,exports){
module.exports={
"name": "spacetime",
"version": "2.1.0",
"version": "2.1.1",
"description": "represent dates in remote timezones",
"main": "./builds/spacetime.js",
"license": "Apache-2.0",
Expand Down Expand Up @@ -2322,7 +2326,7 @@ module.exports={
"size-limit": [
{
"path": "builds/spacetime.min.js",
"limit": "12 KB"
"limit": "14 KB"
}
]
}
Expand Down Expand Up @@ -3784,11 +3788,13 @@ var methods = {
var sum = 0;
var month = this.d.getMonth();
var tmp = void 0;
for (var i = 0; i < month; i++) {
//count the num days in each month
for (var i = 1; i <= month; i++) {
tmp = new Date();
tmp.setYear(this.d.getFullYear()); //the year matters, because leap-years
tmp.setMonth(i);
tmp.setDate(1);
tmp.setHours(-2);
tmp.setHours(-2); //the last day of the month
sum += tmp.getDate();
}
return sum + this.d.getDate();
Expand Down Expand Up @@ -4397,17 +4403,17 @@ var SpaceTime = function SpaceTime(input, tz) {
this.tz = tz || guessTz();
//don't output anything if it's invalid
this.valid = true;
//every computer is somewhere- get this computer's built-in offset
this.bias = new Date().getTimezoneOffset() || 0;
//add getter/setters
Object.defineProperty(this, 'd', {
//return a js date object
get: function get() {
var meta = timezone(this) || {};
//movement in milliseconds
var shift = meta.current.epochShift;
//every computer is somewhere- get this computer's built-in offset
var bias = new Date(this.epoch).getTimezoneOffset() || 0;
//movement
var shift = bias + meta.current.offset * 60; //in minutes
shift = shift * 60 * 1000; //in ms
//remove this computer's offset
shift = shift + this.bias * 60 * 1000;
var epoch = this.epoch + shift;
var d = new Date(epoch);
return d;
Expand Down Expand Up @@ -4523,7 +4529,8 @@ var timezone = function timezone(s) {
m.current.offset = winter;
m.current.isDST = m.hemisphere === 'South'; //dst 'on' in summer in south
}
m.current.epochShift = m.current.offset * 60 * 60 * 1000;
// let minutes = m.current.offset * 60
// m.current.epochShift = minutes * 60 * 1000

return m;
};
Expand All @@ -4547,8 +4554,6 @@ var shouldChange = function shouldChange(s, m) {
//note: this has a order-of-operations issue
//we can't get the date, without knowing the timezone, and vice-versa
//it's possible that we can miss a dst-change by a few hours.
// let diff = (m.offset * 60) + s.bias
// let approx = s.epoch + (diff * 60 * 60 * 1000)
var d = new Date(s.epoch);
var current = toString(d);
//eg. is it after ~november?
Expand Down
6 changes: 3 additions & 3 deletions builds/spacetime.min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions data/zonefile.2017.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
"n"
],
"Caracas": [
-4.5,
-4,
"n"
],
"Catamarca": [
Expand Down Expand Up @@ -969,6 +969,10 @@
7,
"n"
],
"Barnaul":[
7,
"n"
],
"Beirut": [
3,
"n",
Expand Down Expand Up @@ -2064,7 +2068,7 @@
"n"
],
"Easter": [
-5,
-6,
"s",
"05/13:21->08/12:23"
],
Expand Down
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": "2.1.0",
"version": "2.1.1",
"description": "represent dates in remote timezones",
"main": "./builds/spacetime.js",
"license": "Apache-2.0",
Expand Down
20 changes: 9 additions & 11 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ const spacetime = require('./src');
// console.log(zones)


//23:55 eastern (11pm)
//03:55 utc
//23:55 belem
let epoch = 1520999750000
let d1 = spacetime(epoch, 'America/Toronto');
console.log(d1.timezone())
// let d = new Date(epoch)
// console.log('eastern: ', d.toLocaleTimeString())
// console.log('utc: ', d.toUTCString())
//incorrect one
let obj = {}
for (let i = 0; i < 15; i++) {
var s = spacetime({
year: 2014 + i
}).endOf('year');
console.log(2014 + i, s.dayOfYear());
}

console.log('\n')
console.log(d1.hour())
console.log(obj)
1 change: 1 addition & 0 deletions src/methods/format/unixFmt.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
//parse this insane unix-time-templating thing, from the 19th century
//http://unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

Expand Down
8 changes: 5 additions & 3 deletions src/methods/query/normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,16 @@ let methods = {
return this;
}
//days since newyears - jan 1st is 1, jan 2nd is 2...
let sum = 0;
let sum = 0
let month = this.d.getMonth();
let tmp;
for (let i = 0; i < month; i++) {
//count the num days in each month
for (let i = 1; i <= month; i++) {
tmp = new Date();
tmp.setYear(this.d.getFullYear()) //the year matters, because leap-years
tmp.setMonth(i);
tmp.setDate(1);
tmp.setHours(-2);
tmp.setHours(-2); //the last day of the month
sum += tmp.getDate();
}
return sum + this.d.getDate();
Expand Down
10 changes: 5 additions & 5 deletions src/spacetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ const SpaceTime = function(input, tz) {
this.tz = tz || guessTz()
//don't output anything if it's invalid
this.valid = true
//every computer is somewhere- get this computer's built-in offset
this.bias = new Date().getTimezoneOffset() || 0
//add getter/setters
Object.defineProperty(this, 'd', {
//return a js date object
get: function() {
let meta = timezone(this) || {}
//movement in milliseconds
let shift = meta.current.epochShift
//every computer is somewhere- get this computer's built-in offset
let bias = new Date(this.epoch).getTimezoneOffset() || 0
//movement
let shift = bias + (meta.current.offset * 60) //in minutes
shift = shift * 60 * 1000 //in ms
//remove this computer's offset
shift = shift + this.bias * 60 * 1000
let epoch = this.epoch + shift
let d = new Date(epoch)
return d
Expand Down
3 changes: 2 additions & 1 deletion src/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const timezone = s => {
m.current.offset = winter
m.current.isDST = m.hemisphere === 'South' //dst 'on' in summer in south
}
m.current.epochShift = m.current.offset * 60 * 60 * 1000
// let minutes = m.current.offset * 60
// m.current.epochShift = minutes * 60 * 1000

return m;
};
Expand Down
2 changes: 0 additions & 2 deletions src/timezone/summerTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const shouldChange = (s, m) => {
//note: this has a order-of-operations issue
//we can't get the date, without knowing the timezone, and vice-versa
//it's possible that we can miss a dst-change by a few hours.
// let diff = (m.offset * 60) + s.bias
// let approx = s.epoch + (diff * 60 * 60 * 1000)
let d = new Date(s.epoch);
let current = toString(d);
//eg. is it after ~november?
Expand Down
28 changes: 28 additions & 0 deletions test/leapYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,31 @@ test('feb-29th-doesnt-exist', t => {
});
t.end();
});

test('length of year', t => {
let right = {
'2014': 365,
'2015': 365,
'2016': 366,
'2017': 365,
'2018': 365,
'2019': 365,
'2020': 366,
'2021': 365,
'2022': 365,
'2023': 365,
'2024': 366,
'2025': 365,
'2026': 365,
'2027': 365,
'2028': 366
}
for (let i = 0; i < 15; i++) {
let year = 2014 + i
var s = spacetime({
year: year
}).endOf('year');
t.equal(right[year], s.dayOfYear(), 'year ' + year);
}
t.end();
});
93 changes: 93 additions & 0 deletions test/moment.ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const test = require('tape');
const spacetime = require('./lib');
test('add', t => {
t.equal(
spacetime('2012-10-28 00:00:00+01:00').goto('Europe/London').add(1, 'days').format('iso'),
'2012-10-29T00:00:00Z',
"adding 1 day while crossing a DST boundary should not affect time (BST -> GMT)."
);
t.equal(
spacetime('2013-11-03T00:00:00-07:00').goto('America/Los_Angeles').add(1, 'day').format('iso'),
"2013-11-04T00:00:00-08:00",
"adding 1 day while crossing a DST boundary should not affect time (PDT-> PST)."
);
t.equal(
spacetime("2014-03-09T00:00:00-08:00").goto('America/Los_Angeles').add(1, 'day').format('iso'),
"2014-03-10T00:00:00-07:00",
"adding 1 day while crossing a DST boundary should not affect time (PST -> PDT)."
);
t.end();
})
//
// test('subtract', t => {
// t.equal(
// spacetime('2012-10-29T00:00:00+00:00').goto('Europe/London').subtract(1, 'days').format('iso'),
// '2012-10-28T00:00:00+01:00',
// "subtracting 1 day while crossing a DST boundary should not affect time (GMT -> BST)."
// );
// t.equal(
// spacetime("2013-11-04T00:00:00-08:00").goto('America/Los_Angeles').subtract(1, 'day').format('iso'),
// '2013-11-03T00:00:00-07:00',
// "adding 1 day while crossing a DST boundary should not affect time (PST -> PDT)."
// );
// t.equal(
// spacetime("2014-03-10T00:00:00-07:00").goto('America/Los_Angeles').subtract(1, 'day').format('iso'),
// "2014-03-09T00:00:00-08:00",
// "adding 1 day while crossing a DST boundary should not affect time (PDT -> PST)."
// );
// t.end();
// })
//
// test('month', t => {
// t.equal(
// spacetime("2014-03-09T00:00:00-08:00").goto('America/Los_Angeles').add(1, 'month').format('iso'),
// "2014-04-09T00:00:00-07:00",
// "adding 1 month while crossing a DST boundary should not affect time (PST -> PDT)."
// );
// t.equal(
// spacetime("2014-03-09T00:00:00-08:00").goto('America/Los_Angeles').month(3).format('iso'),
// "2014-04-09T00:00:00-07:00",
// "setting month across a DST boundary should not affect time (PST -> PDT)."
// );
//
// t.end();
// })

// test('issame', t => {
// var m1 = spacetime('2014-10-01T00:00:00', 'Europe/London');
// var m2 = spacetime('2014-10-01T00:00:00', 'Europe/London');
//
// var lastDayOfMonth = m1.clone().endOf('month').date();
//
// for (var day1 = 1; day1 <= lastDayOfMonth; day1++) {
// for (var day2 = 1; day2 <= lastDayOfMonth; day2++) {
// m1.date(day1);
// m2.date(day2);
// t.ok(
// m1.isSame(m2, 'month'),
// 'month should be the same for ' + m1.format('iso') + ' and ' + m2.format('iso')
// );
// }
// }
//
// var m3 = spacetime('2014-09-09T00:00:00', 'America/Chicago');
// var m4 = spacetime('2014-09-09T01:00:00', 'America/Toronto');
//
// for (var hour1 = 0; hour1 <= 22; hour1++) {
// for (var hour2 = 1; hour2 <= 23; hour2++) {
// m3.hour(hour1);
// m4.hour(hour2);
//
// t.ok(
// m3.isSame(m4, 'day'),
// 'day should be the same for ' + m3.format('iso') + ' and ' + m4.format('iso')
// );
// t.ok(
// m4.isSame(m3, 'day'),
// 'day should be the same for ' + m4.format('iso') + ' and ' + m3.format('iso')
// );
// }
// }
//
// t.end();
// })
Loading

0 comments on commit f19d89a

Please sign in to comment.