-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic.coffee
More file actions
282 lines (223 loc) · 9.58 KB
/
tic.coffee
File metadata and controls
282 lines (223 loc) · 9.58 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
window.tic = do ->
# tic has some languages
langs =
"en-US":
stdDateFormat: "MM/DD/YYYY"
days : ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
months : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
monthsShort : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
# change this, if your default language is something else
lang = langs["en-US"]
# internal functions
apply = (f, args, obj = window) -> f.apply obj, args
curry = (f, args...) ->
(moreArgs...) -> f.apply this, (args.concat moreArgs)
compose = (fs...) ->
(args...) ->
args = [apply fs[i], args] for i in [fs.length-1..0]
first args
foldl = (f, a, [x, xs...]) ->
if xs.length == 0 then f a, x else foldl f, (f a, x), xs
first = (xs) -> xs[0] if xs? && xs.length > 0
take = (xs, t) -> xs.slice t
drop = (xs, t) -> xs.splice t
repeat = (xs, t) ->
l = xs.length - 1
(xs[i % l] for i in [0..t-1])
keys = (obj) -> (k for k, v of obj)
compact = (xs) -> (x for x in xs when x?)
substr = (str, id, i) -> ("" + str).substr id, i
getConfiguredSubstrFn = (id, i) -> (str) -> substr str, id, i
increment = (num) -> num + 1
padNumber = (num, targetLength = 2) ->
if (num+"").length < targetLength then padNumber "0#{num}", targetLength else num+""
dot = (obj, attribute) -> obj[attribute]
dotExec = (p) -> (obj) -> obj[p]()
# more tic specific stuff
getLanguageLookupFn = (key) -> curry dot, lang[key]
clone = (d) -> new Date d
resetTime = (date, time = "") ->
[h, m, s, ms] = time.split ":"
d = clone date
d.setHours (h || 0)
d.setMinutes (m || 0)
d.setSeconds (s || 0)
d.setMilliseconds (ms || 0)
d
parseFunctions =
"YYYY": (date, val) -> new Date (date.setYear val)
"YY" : (date, val) -> new Date (date.setYear "20"+val)
"MM" : (date, val) -> new Date (date.setMonth +val-1)
"M" : (date, val) -> new Date (date.setMonth +val-1)
"DD" : (date, val) -> new Date (date.setDate val)
"D" : (date, val) -> new Date (date.setDate val)
"HH" : (date, val) -> new Date (date.setHours val)
"H" : (date, val) -> new Date (date.setHours val)
"mm" : (date, val) -> new Date (date.setMinutes val)
"m" : (date, val) -> new Date (date.setMinutes val)
"SS" : (date, val) -> new Date (date.setSeconds val)
"S" : (date, val) -> new Date (date.setSeconds val)
"ss" : (date, val) -> new Date (date.setSeconds val)
"s" : (date, val) -> new Date (date.setSeconds val)
parseFormatRegex = new RegExp ((keys parseFunctions).join "|"), "g"
parse = (str = "", formatStr = lang.stdDateFormat) ->
if str == ""
new Date()
else
switch typeof str
when "string"
date = resetTime new Date()
parseRules = formatStr.match parseFormatRegex
applyRule = (date, rule) ->
val = str.substr (formatStr.indexOf rule), rule.length
if parseFunctions[rule]? then parseFunctions[rule] date, val else date
if parseRules?
foldl applyRule, date, parseRules
else
date
when "number" then new Date str
else new Date()
formatFunctions =
"YYYY": dotExec "getFullYear"
"YY" : compose (getConfiguredSubstrFn 2), (dotExec "getFullYear")
"MMMM": compose (getLanguageLookupFn "months"), (dotExec "getMonth")
"MMM" : compose (getLanguageLookupFn "monthsShort"), (dotExec "getMonth")
"MM" : compose padNumber, increment, (dotExec "getMonth")
"M" : compose increment, (dotExec "getMonth")
"DD" : compose padNumber, (dotExec "getDate")
"D" : dotExec "getDate"
"dddd": compose (getLanguageLookupFn "days"), (dotExec "getDay")
"ddd" : compose (getConfiguredSubstrFn 0, 3), (getLanguageLookupFn "days"), (dotExec "getDay")
"dd" : compose (getConfiguredSubstrFn 0, 2), (getLanguageLookupFn "days"), (dotExec "getDay")
"d" : dotExec "getDay"
"HH" : compose padNumber, (dotExec "getHours")
"H" : dotExec "getHours"
"mm" : compose padNumber, (dotExec "getMinutes")
"m" : dotExec "getMinutes"
"SS" : compose padNumber, (dotExec "getSeconds")
"ss" : compose padNumber, (dotExec "getSeconds")
"S" : dotExec "getSeconds"
"s" : dotExec "getSeconds"
formatFunctionsRegex = new RegExp ((keys formatFunctions).join "|")
formatStrGroups = (formatStr) ->
if formatFunctionsRegex.test formatStr
group = (formatStr.match formatFunctionsRegex)[0]
groupId = formatStr.indexOf group
preGroup = (formatStr.substr 0, groupId) if groupId != 0
newFormatStr = formatStr.substr (groupId + group.length)
(if preGroup? then [preGroup, group] else [group]).concat (formatStrGroups newFormatStr)
else
[formatStr]
format = (date, formatStr = lang.stdDateFormat) ->
(for g in formatStrGroups formatStr
(if formatFunctions[g]? then formatFunctions[g] date else g)
).join ""
compare = (a, b, precision) ->
if !precision?
if a < b then 1 else (if a > b then -1 else 0)
else
switch precision
when "y", "year", "years" then compare a, b, "YYYY"
when "m", "month", "months" then compare a, b, "YYYYMM"
when "d", "day", "days" then compare a, b, "YYYYMMDD"
when "h", "hour", "hours" then compare a, b, "YYYYMMDDHH"
when "min", "minute", "minutes" then compare a, b, "YYYYMMDDHHmm"
when "s", "second", "seconds" then compare a, b, "YYYYMMDDHHmmSS"
when "ms", "mlliisecond", "milliseconds" then compare a, b
else compare +(format a, precision), +(format b, precision)
equals = (a, b, precision) ->
(compare a, b, precision) == 0
isPast = (date, precision = "d") ->
(compare new Date(), date, precision) < 0
isToday = (date, precision = "d") ->
(compare new Date(), date, precision) == 0
isFuture = (date, precision = "d") ->
(compare new Date(), date, precision) > 0
isBetween = (date, date1, date2, precision) ->
(compare date1, date, precision) >= 0 && (compare date2, date, precision) <= 0
stdTimezoneOffset = (date) ->
jan = new Date date.getFullYear(), 0, 1
jul = new Date date.getFullYear(), 6, 1
Math.max jan.getTimezoneOffset(), jul.getTimezoneOffset()
isDST = (date) -> date.getTimezoneOffset() < (stdTimezoneOffset date)
isBeforeMarch1 = (d) -> d.getMonth() < 2 if d.getMonth?
isLeapYear = (d) ->
y = if d.getFullYear? then d.getFullYear() else +d
y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)
leapDaysBetween = (date1, date2) ->
[date1, date2] = [date2, date1] if date1 > date2
y1 = date1.getFullYear()
y1 = y1 + 1 if !(isBeforeMarch1 date1)
y2 = date2.getFullYear()
y2 = y2 - 1 if isBeforeMarch1 date2
unless y2 < y1
foldl ((a, b) -> a + b), 0, ((if isLeapYear y then 1 else 0) for y in [y1..y2])
else
0
addLeapDaysR = (d1, d2, dir = 1) ->
a = new Date (if dir > 0 then Math.max d1, d2 else Math.min d1, d2)
ld = (leapDaysBetween d1, d2) * dir
if ld == 0 then a else addLeapDaysR a, (add a, ld, "days"), dir
msFactors =
"millisecond": 1
"second" : 1e3
"minute" : 6e4
"hour" : 36e5
"day" : 864e5
"week" : 6048e5
"year" : 31536e6
addYears = (date, amount) ->
d = addMilliseconds date, amount*msFactors.year
d = addLeapDaysR date, d, (amount / (Math.abs amount))
resetTime d, (format date, "HH:mm:SS")
daysForMonths = (m, mc) ->
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if mc < 0
days = days.reverse()
m = [0..11].reverse()[m] - 1
drop (repeat days, (Math.abs mc) + m), m
addMonths = (date, amount) ->
factor = (amount / (Math.abs amount))
days = foldl ((a, b) -> a + b), 0, (daysForMonths date.getMonth(), amount)
d = addLeapDaysR date, (addDays date, days*factor), factor
resetTime d, (format date, "HH:mm:SS")
addWeeks = (date, amount) ->
d = addMilliseconds date, amount*msFactors.week
resetTime d, (format date, "HH:mm:SS")
addDays = (date, amount) ->
d = addMilliseconds date, amount*msFactors.day
resetTime d, (format date, "HH:mm:SS")
addHours = (date, amount) -> addMilliseconds date, amount*msFactors.hour
addMinutes = (date, amount) -> addMilliseconds date, amount*msFactors.minute
addSeconds = (date, amount) -> addMilliseconds date, amount*msFactors.second
addMilliseconds = (date, amount) -> new Date (date.getTime() + amount)
add = (date, amount, unit = "d") ->
switch unit
when "y", "year", "years" then addYears date, +amount
when "m", "month", "months" then addMonths date, +amount
when "w", "week", "weeks" then addWeeks date, +amount
when "d", "day", "days" then addDays date, +amount
when "h", "hour", "hours" then addHours date, +amount
when "min", "minute", "minutes" then addMinutes date, +amount
when "s", "second", "seconds" then addSeconds date, +amount
when "ms", "millisecond", "milliseconds" then addMilliseconds date, +amount
else addMilliseconds date, +amount
remove = (date, amount, unit) -> add date, -amount, unit
increment = (date, amount, unit) -> add date, 1, unit
decrement = (date, amount, unit) -> add date, -1, unit
ticObj =
compare: compare
equals: equals
resetTime: resetTime
parse: parse
format: format
add: add
increment: increment
remove: remove
decrement: decrement
isPast: isPast
isToday: isToday
isFuture: isFuture
isBetween: isBetween
isLeapYear: isLeapYear
isDST: isDST