From edd71b41c2910ac27805c97150d80a9e26184763 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 31 May 2022 12:29:35 +0200 Subject: [PATCH] feat(cli): interpet input dates & print dates as local timezone --- src/console/logs/logs.v | 32 +++++++++++++++++++------------- src/docker/containers.v | 1 + 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/console/logs/logs.v b/src/console/logs/logs.v index 3b1c756b..cb6997fc 100644 --- a/src/console/logs/logs.v +++ b/src/console/logs/logs.v @@ -20,7 +20,7 @@ pub fn cmd() cli.Command { commands: [ cli.Command{ name: 'list' - description: 'List build logs.' + description: 'List build logs. All date strings in the output are converted to the local timezone. Any time strings provided as input should be in the local timezone as well.' flags: [ cli.Flag{ name: 'limit' @@ -39,7 +39,7 @@ pub fn cmd() cli.Command { }, cli.Flag{ name: 'today' - description: 'Only list logs started today (UTC time).' + description: 'Only list logs started today.' flag: cli.FlagType.bool }, cli.Flag{ @@ -49,17 +49,17 @@ pub fn cmd() cli.Command { }, cli.Flag{ name: 'day' - description: 'Only list logs started on this day. Format is YYYY-MM-DD.' + description: 'Only list logs started on this day. (format: YYYY-MM-DD)' flag: cli.FlagType.string }, cli.Flag{ name: 'before' - description: 'Only list logs started before this timestamp. Accepts any RFC 3339 date.' + description: 'Only list logs started before this timestamp. (format: YYYY-MM-DD HH:mm:ss)' flag: cli.FlagType.string }, cli.Flag{ name: 'after' - description: 'Only list logs started after this timestamp. Accepts any RFC 3339 date.' + description: 'Only list logs started after this timestamp. (format: YYYY-MM-DD HH:mm:ss)' flag: cli.FlagType.string }, ] @@ -84,6 +84,8 @@ pub fn cmd() cli.Command { filter.repo = repo_id } + tz_offset := time.offset() + if cmd.flags.get_bool('today')? { today := time.now() @@ -91,7 +93,7 @@ pub fn cmd() cli.Command { year: today.year month: today.month day: today.day - }) + }).add_seconds(-tz_offset) filter.before = filter.after.add_days(1) } // The -today flag overwrites any of the other date flags. @@ -102,21 +104,25 @@ pub fn cmd() cli.Command { if day_str != '' { day := time.parse_rfc3339(day_str)? - - filter.after = time.new_time(time.Time{ + day_utc := time.new_time(time.Time{ year: day.year month: day.month day: day.day - }) + }).add_seconds(-tz_offset) - filter.before = filter.after.add_days(1) + // The extra -1 is so we also return logs that + // started at exactly midnight (filter bounds are + // exclusive). therefore, we have to request logs + // started after 23:59:59 the previous day. + filter.after = day_utc.add_seconds(-1) + filter.before = day_utc.add_days(1) } else { if before_str != '' { - filter.before = time.parse_rfc3339(before_str)? + filter.before = time.parse(before_str)?.add_seconds(-tz_offset) } if after_str != '' { - filter.after = time.parse_rfc3339(after_str)? + filter.after = time.parse(after_str)?.add_seconds(-tz_offset) } } } @@ -162,7 +168,7 @@ pub fn cmd() cli.Command { // print_log_list prints a list of logs. fn print_log_list(logs []BuildLog) ? { - data := logs.map([it.id.str(), it.repo_id.str(), it.start_time.str(), + data := logs.map([it.id.str(), it.repo_id.str(), it.start_time.local().str(), it.exit_code.str()]) println(console.pretty_table(['id', 'repo', 'start time', 'exit code'], data)?) diff --git a/src/docker/containers.v b/src/docker/containers.v index 0bc59bb3..8fbf0274 100644 --- a/src/docker/containers.v +++ b/src/docker/containers.v @@ -83,6 +83,7 @@ pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect { mut data := json.decode(ContainerInspect, body)? + // The Docker engine API *should* always return UTC time. data.state.start_time = time.parse_rfc3339(data.state.start_time_str)? if data.state.status == 'exited' {