-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathddate.js
executable file
·205 lines (188 loc) · 6.02 KB
/
ddate.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
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
#!/usr/bin/env node
var days = [
{ l: 'Sweetmorn', s: 'SM' },
{ l: 'Boomtime', s: 'BT' },
{ l: 'Pungenday', s: 'PD' },
{ l: 'Prickle-Prickle', s: 'PP' },
{ l: 'Setting Orange', s: 'SO' }
];
var seasons = [
{ l: 'Chaos', s: 'Chs' },
{ l: 'Discord', s: 'Dsc' },
{ l: 'Confusion', s: 'Cfn' },
{ l: 'Bureaucracy', s: 'Bcy' },
{ l: 'The Aftermath', s: 'Afm' }
];
var holydays = {
'Chaos': {
5: 'Mungday',
50: 'Chaoflux'
},
'Discord': {
5: 'Mojoday',
50: 'Discoflux'
},
'Confusion': {
5: 'Syaday',
50: 'Confuflux'
},
'Bureaucracy': {
5: 'Zaraday',
50: 'Bureflux'
},
'The Aftermath': {
5: 'Maladay',
50: 'Afflux'
}
};
var minute = 1000 * 60;
var day = minute * 60 * 24;
var year = day * 365;
var DDate = function(epooch) {
/* for reference, epoch is Sweetmorn, 1 Chaos 3136 */
this.date = {};
this.initificate = function(epooch) {
epooch -= new Date().getTimezoneOffset() * minute;
var leps = Math.floor(epooch / year / 4);
epooch -= leps * day;
var cur = Math.floor((epooch % year) / day);
var flarf = Math.floor(epooch / (day * 365)) + 3136;
var ist = ((flarf - 3130) % 4 == 0);
this.tabby = (ist && cur == 59);
if(ist && cur > 59) cur -= 1;
var gwar = Math.floor(cur % 73) + 1;
var sn = Math.floor(cur / 73);
var woody = 0;
for(var i = 1; i <= cur; i++) {
woody = (woody == 4) ? 0 : woody + 1;
}
var hoyl = holydays[seasons[sn].l][gwar] || false;
this.numricks = [ woody, sn, gwar, flarf, hoyl ];
if(this.tabby) return { tibs: true, year: flarf };
return {
tibs: false,
day: days[woody],
season: seasons[sn],
date: gwar,
year: flarf,
holyday: hoyl
};
};
this.numberize = function(num) {
var thtaghn = (num % 100) > 9 && (num % 100) < 15;
switch(num % 10) {
case 1:
return num + (thtaghn ? 'th' : 'st');
case 2:
return num + (thtaghn ? 'th' : 'nd');
case 3:
return num + (thtaghn ? 'th' : 'rd');
case 4:
default:
return num + 'th';
}
};
this.toOldImmediateDateFormat = function() {
return this.date.day.l + ', the ' + this.numberize(this.date.date) + ' day of ' +
this.date.season.l + ' in the YOLD ' + this.date.year;
};
this.toDateString = function() {
return this.format("%{%A, %B %e%}, %Y YOLD");
};
this.getDate = function() {
return this.date;
};
this.format = function(str) {
if(!str) return;
var r = '';
var stopit = false;
var tibsing = false;
for(var i = 0; i < str.length; i++) {
if(stopit) break;
if(str[i] == '%' && str[i+1] == '}') tibsing = ((i += 2) == Infinity);
if(tibsing) continue;
if(str[i] == '%') {
switch(str[i+1]) {
case 'A':
r += days[this.numricks[0]].l;
break;
case 'a':
r += days[this.numricks[0]].s;
break;
case 'B':
r += seasons[this.numricks[1]].l;
break;
case 'b':
r += seasons[this.numricks[1]].s;
break;
case 'd':
r += this.numricks[2];
break;
case 'e':
r += this.numberize(this.numricks[2]);
break;
case 'H':
r += this.numricks[4] || '';
break;
case 'N':
stopit = !Boolean(this.numricks[4]);
break;
case 'n':
r += '\n';
break;
case 't':
r += '\t';
break;
case '{':
if(this.tabby) tibsing = ((r += "St. Tib's Day") != Infinity);
break;
case '.':
r += "I've nothing to say to you. (yet)";
break;
case 'Y':
r += this.numricks[3];
break;
default:
r += str[i];
break;
}
i++;
} else {
r += str[i];
}
}
return r;
};
this.date = this.initificate(epooch || new Date().getTime());
};
module.exports = DDate;
if(process.argv.length > 1 && (process.argv[1].slice(-5) == 'ddate' || process.argv[1].slice(-8) == 'ddate.js')) {
// this will break SO badly when someone requires this module from an executable script named ddate.js.
// But then it will be their fault for stealing my name.
t = false;
f = false;
var varg = process.argv;
if(varg[2] && varg[2][0] == '+') {
f = varg[2].slice(1);
var vor = [];
for(var i = 0; i < varg.length; i++) {
if(i !== 2) vor.push(varg[i]);
}
varg = vor;
}
var d;
if(!varg[2]) {
t = true;
d = new Date();
} else if(varg[2].indexOf(' ') > -1) {
d = new Date(varg[2]);
} else if(varg.length > 4) {
d = new Date(String('000' + varg[3]).slice(-2) + '-' +
String('000' + varg[2]).slice(-2) + '-' +
String('000' + varg[4]).slice(-4));
} else {
d = new Date(parseInt(varg[2]));
}
var g = new DDate(d.getTime());
console.log(t ? g.format((f || 'Today is %{%A, the %e day of %B%} in the YOLD %Y%N%nCelebrate %H')) : (f ? g.format(f) : g.toDateString()));
}