-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (118 loc) · 4.53 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var EventEmitter = require('events').EventEmitter,
fmt = require('util').format
var months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
var days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
var dFmt = '%s-%s-%s'
var dtFmt = '%s-%s-%s %s:%s'
var wdtFmt = '%s %s:%s'
var tFmt = '%s:%s'
module.exports = function createDateEmitter (options) {
options = typeof options === 'object' ? options : {}
options.startDate = options.startDate instanceof Date ? options.startDate : new Date
options.unref = typeof options.unref === 'boolean' ? options.unref : false
var emitter = new EventEmitter,
listenerCount = 0,
timeout = null,
interval = null,
now = options.startDate,
last = {
year : now.getFullYear(),
month : now.getMonth() + 1,
date : now.getDate(),
weekday: now.getDay(),
hour : now.getHours(),
minute : now.getMinutes(),
second : now.getSeconds()
}
emitter.on('newListener', listenerAdded)
emitter.on('removeListener', listenerRemoved)
return emitter
function eachSecond () {
var now = new Date,
year = now.getFullYear(),
month = now.getMonth() + 1,
date = now.getDate(),
weekday = now.getDay() + 1,
hour = now.getHours(),
minute = now.getMinutes()
var monthPadded, datePadded, hourPadded, minutePadded
if (date !== last.date) {
emitter.emit('date', date)
monthPadded = ('0' + month).slice(-2)
datePadded = ('0' + date).slice(-2)
;[year, '*'].forEach(function (year) {
;[monthPadded, '*'].forEach(function (month) {
;[datePadded, '*'].forEach(function (date) {
emitter.emit(fmt(dFmt, year, month, date), now)
})
})
})
last.date = date
}
if (minute !== last.minute) {
monthPadded = ('0' + month).slice(-2)
datePadded = ('0' + date).slice(-2)
hourPadded = ('0' + hour).slice(-2)
minutePadded = ('0' + minute).slice(-2)
emitter.emit('minute', minute)
;[year, '*'].forEach(function (year) {
;[monthPadded, '*'].forEach(function (month) {
;[datePadded, '*'].forEach(function (date) {
;[hourPadded, '*'].forEach(function (hour) {
;[minutePadded, '*'].forEach(function (minute) {
emitter.emit(fmt(dtFmt, year, month, date, hour, minute), now)
})
})
})
})
})
;[hourPadded, '*'].forEach(function (hour) {
;[minutePadded, '*'].forEach(function (minute) {
emitter.emit(fmt(wdtFmt, days[weekday - 1], hour, minute), now)
emitter.emit(fmt(tFmt, hour, minute), now)
})
})
last.minute = minute
}
if (year !== last.year) {
emitter.emit('year', year)
last.year = year
}
if (month !== last.month) {
emitter.emit('month', month, months[month - 1])
emitter.emit(months[month - 1])
last.month = month
}
if (weekday !== last.weekday) {
hourPadded = ('0' + hour).slice(-2)
minutePadded = ('0' + minute).slice(-2)
emitter.emit('weekday', weekday, days[weekday - 1])
emitter.emit(days[weekday - 1])
last.weekday = weekday
}
if (hour !== last.hour) {
emitter.emit('hour', hour)
last.hour = hour
}
emitter.emit('second', ++last.second)
}
function listenerAdded () {
listenerCount++
if (!timeout && !interval) timeout = setTimeout(start, 1000 - (new Date).getMilliseconds())
}
function listenerRemoved () {
listenerCount--
if (!listenerCount) stop()
}
function start () {
interval = setInterval(eachSecond, 1000)
if (options.unref) interval.unref()
eachSecond()
}
function stop () {
clearTimeout(timeout)
clearInterval(interval)
timeout = null
interval = null
}
}