mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 21:01:52 +00:00
parse seconds, minutes, hours, days
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user