Skip to content

Commit d885ea5

Browse files
authored
fix: enable and fix date command tests
Closes #796
1 parent a402528 commit d885ea5

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

src/phoenix/src/puter-shell/coreutils/date.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ export default {
108108
const locale = 'en-US'; // TODO: POSIX: Pull this from the user's settings.
109109
const timeZone = values.utc ? 'UTC' : undefined;
110110

111+
// Helper functions to get date/time values respecting UTC option
112+
const getYear = () => values.utc ? date.getUTCFullYear() : date.getFullYear();
113+
const getMonth = () => values.utc ? date.getUTCMonth() : date.getMonth();
114+
const getDate = () => values.utc ? date.getUTCDate() : date.getDate();
115+
const getDay = () => values.utc ? date.getUTCDay() : date.getDay();
116+
const getHours = () => values.utc ? date.getUTCHours() : date.getHours();
117+
const getMinutes = () => values.utc ? date.getUTCMinutes() : date.getMinutes();
118+
const getSeconds = () => values.utc ? date.getUTCSeconds() : date.getSeconds();
119+
111120
let output = '';
112121
for (let i = 0; i < format.length; i++) {
113122
let char = format[i];
@@ -148,41 +157,41 @@ export default {
148157

149158
// "Century (a year divided by 100 and truncated to an integer) as a decimal number [00,99]."
150159
case 'C': {
151-
output += Math.trunc(date.getFullYear() / 100);
160+
output += Math.trunc(getYear() / 100);
152161
break;
153162
}
154163

155164
// "Day of the month as a decimal number [01,31]."
156165
case 'd': {
157-
output += padStart(date.getDate(), 2, '0');
166+
output += padStart(getDate(), 2, '0');
158167
break;
159168
}
160169

161170
// "Date in the format mm/dd/yy."
162171
case 'D': {
163-
const month = padStart(date.getMonth() + 1, 2, '0');
164-
const day = padStart(date.getDate(), 2, '0');
165-
const year = padStart(date.getFullYear() % 100, 2, '0');
172+
const month = padStart(getMonth() + 1, 2, '0');
173+
const day = padStart(getDate(), 2, '0');
174+
const year = padStart(getYear() % 100, 2, '0');
166175
output += `${month}/${day}/${year}`;
167176
break;
168177
}
169178

170179
// "Day of the month as a decimal number [1,31] in a two-digit field with leading <space>
171180
// character fill."
172181
case 'e': {
173-
output += padStart(date.getDate(), 2, ' ');
182+
output += padStart(getDate(), 2, ' ');
174183
break;
175184
}
176185

177186
// "Hour (24-hour clock) as a decimal number [00,23]."
178187
case 'H': {
179-
output += padStart(date.getHours(), 2, '0');
188+
output += padStart(getHours(), 2, '0');
180189
break;
181190
}
182191

183192
// "Hour (12-hour clock) as a decimal number [01,12]."
184193
case 'I': {
185-
output += padStart((date.getHours() % 12) || 12, 2, '0');
194+
output += padStart((getHours() % 12) || 12, 2, '0');
186195
break;
187196
}
188197

@@ -192,13 +201,13 @@ export default {
192201
// "Month as a decimal number [01,12]."
193202
case 'm': {
194203
// getMonth() starts at 0 for January
195-
output += padStart(date.getMonth() + 1, 2, '0');
204+
output += padStart(getMonth() + 1, 2, '0');
196205
break;
197206
}
198207

199208
// "Minute as a decimal number [00,59]."
200209
case 'M': {
201-
output += padStart(date.getMinutes(), 2, '0');
210+
output += padStart(getMinutes(), 2, '0');
202211
break;
203212
}
204213

@@ -208,26 +217,26 @@ export default {
208217
// "Locale's equivalent of either AM or PM."
209218
case 'p': {
210219
// TODO: We should access this from the locale.
211-
output += date.getHours() < 12 ? 'AM' : 'PM';
220+
output += getHours() < 12 ? 'AM' : 'PM';
212221
break;
213222
}
214223

215224
// "12-hour clock time [01,12] using the AM/PM notation; in the POSIX locale, this shall be
216225
// equivalent to %I : %M : %S %p."
217226
case 'r': {
218-
const rawHours = date.getHours();
227+
const rawHours = getHours();
219228
const hours = padStart((rawHours % 12) || 12, 2, '0');
220229
// TODO: We should access this from the locale.
221230
const am_pm = rawHours < 12 ? 'AM' : 'PM';
222-
const minutes = padStart(date.getMinutes(), 2, '0');
223-
const seconds = padStart(date.getSeconds(), 2, '0');
231+
const minutes = padStart(getMinutes(), 2, '0');
232+
const seconds = padStart(getSeconds(), 2, '0');
224233
output += `${hours}:${minutes}:${seconds} ${am_pm}`;
225234
break;
226235
}
227236

228237
// "Seconds as a decimal number [00,60]."
229238
case 'S': {
230-
output += padStart(date.getSeconds(), 2, '0');
239+
output += padStart(getSeconds(), 2, '0');
231240
break;
232241
}
233242

@@ -236,17 +245,17 @@ export default {
236245

237246
// "24-hour clock time [00,23] in the format HH:MM:SS."
238247
case 'T': {
239-
const hours = padStart(date.getHours(), 2, '0');
240-
const minutes = padStart(date.getMinutes(), 2, '0');
241-
const seconds = padStart(date.getSeconds(), 2, '0');
248+
const hours = padStart(getHours(), 2, '0');
249+
const minutes = padStart(getMinutes(), 2, '0');
250+
const seconds = padStart(getSeconds(), 2, '0');
242251
output += `${hours}:${minutes}:${seconds}`;
243252
break;
244253
}
245254

246255
// "Weekday as a decimal number [1,7] (1=Monday)."
247256
case 'u': {
248257
// getDay() returns 0 for Sunday
249-
output += date.getDay() || 7;
258+
output += getDay() || 7;
250259
break;
251260
}
252261

@@ -262,7 +271,7 @@ export default {
262271

263272
// "Weekday as a decimal number [0,6] (0=Sunday)."
264273
case 'w': {
265-
output += date.getDay();
274+
output += getDay();
266275
break;
267276
}
268277

@@ -284,13 +293,13 @@ export default {
284293

285294
// "Year within century [00,99]."
286295
case 'y': {
287-
output += date.getFullYear() % 100;
296+
output += getYear() % 100;
288297
break;
289298
}
290299

291300
// "Year with century as a decimal number."
292301
case 'Y': {
293-
output += date.getFullYear();
302+
output += getYear();
294303
break;
295304
}
296305

src/phoenix/test/coreutils/date.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import { MakeTestContext } from './harness.js'
2222
import builtins from '../../src/puter-shell/coreutils/__exports__.js';
2323

2424
export const runDateTests = () => {
25-
// These tests are disabled for now.
26-
// (broken, very low priority)
27-
return;
2825
describe('date', function () {
2926
beforeEach(() => {
3027
ck.freeze();

0 commit comments

Comments
 (0)