parse seconds, minutes, hours, days

This commit is contained in:
Dylan Conway
2024-09-10 15:57:22 -07:00
parent e565076eee
commit 1c3d6da9b2
2 changed files with 31 additions and 3 deletions

View File

@@ -10520,16 +10520,44 @@ pub const Interpreter = struct {
var total_seconds: f64 = 0;
while (iter.next()) |arg| {
invalid_interval: {
const trimmed = bun.strings.trimLeft(bun.sliceTo(arg, 0), " ");
var trimmed: string = bun.strings.trimLeft(bun.sliceTo(arg, 0), " ");
if (trimmed.len == 0 or trimmed[0] == '-') {
break :invalid_interval;
}
const seconds = bun.fmt.parseFloat(f64, trimmed) catch {
const maybe_unit: ?enum {
seconds,
minutes,
hours,
days,
pub fn apply(unit: @This(), value: f64) f64 {
return switch (unit) {
.seconds => value,
.minutes => value * 60,
.hours => value * 60 * 60,
.days => value * 60 * 60 * 24,
};
}
} = switch (trimmed[trimmed.len - 1]) {
's' => .seconds,
'm' => .minutes,
'h' => .hours,
'd' => .days,
else => null,
};
if (maybe_unit != null) {
trimmed = trimmed[0 .. trimmed.len - 1];
}
const value = bun.fmt.parseFloat(f64, trimmed) catch {
break :invalid_interval;
};
const seconds = if (maybe_unit) |unit| unit.apply(value) else value;
if (std.math.isInf(seconds)) {
// if positive infinity is seen, set total seconds to `-1`.
// continue iterating to catch invalid args

View File

@@ -222,7 +222,7 @@ export function createTestBuilder(path: string) {
async doChecks(stdout: Buffer, stderr: Buffer, exitCode: number, durationMs: number): Promise<void> {
const tempdir = this.tempdir || "NO_TEMP_DIR";
if (this._expectedDuration !== undefined) {
expect(durationMs).toBeGreaterThanOrEqual(this._expectedDuration);
expect(durationMs >= this._expectedDuration && durationMs <= this._expectedDuration + 200).toBeTrue();
}
if (this.expected_stdout !== undefined) {
if (typeof this.expected_stdout === "string") {