-
Notifications
You must be signed in to change notification settings - Fork 22
/
Equinox.js
66 lines (58 loc) · 1.67 KB
/
Equinox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict'
const solstice = require('astronomia/lib/solstice')
const julian = require('astronomia/lib/julian')
const planetpos = require('astronomia/lib/planetposition')
const earth = new planetpos.Planet(require('astronomia/data/vsop87Bearth'))
const moment = require('moment-timezone')
const CalEvent = require('./CalEvent')
const CalDate = require('caldate')
class Equinox extends CalEvent {
/**
* @param {object} [opts]
* @param {string} opts.season - type of season (spring|summer|autumn|winter)
* @param {number|string} opts.offset - offset in days
*/
constructor (opts) {
opts = opts || {}
super(opts)
this._season = opts.season
this._timezone = opts.timezone || 'GMT'
}
inYear (year) {
let jde
switch (this._season) {
case 'march': {
jde = solstice.march2(year, earth)
break
}
case 'june': {
jde = solstice.june2(year, earth)
break
}
case 'september': {
jde = solstice.september2(year, earth)
break
}
case 'december': {
jde = solstice.december2(year, earth)
break
}
}
const str = new julian.Calendar().fromJDE(jde).toDate().toISOString()
let date
if (/^[+-]\d{2}:\d{2}?$/.test(this._timezone)) { // for '+08:00' formats
date = moment(str).utcOffset(this._timezone)
} else { // for 'Asia/Shanghai' formats
date = moment(str).tz(this._timezone) // move to timezone
}
const floorDate = {
year: year,
month: date.month() + 1,
day: date.date()
}
const d = new CalDate(floorDate).setOffset(this.offset)
this.dates.push(d)
return this
}
}
module.exports = Equinox