feat(cli): interpet input dates & print dates as local timezone

main
Jef Roosens 2022-05-31 12:29:35 +02:00
parent f0565c4168
commit edd71b41c2
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 20 additions and 13 deletions

View File

@ -20,7 +20,7 @@ pub fn cmd() cli.Command {
commands: [ commands: [
cli.Command{ cli.Command{
name: 'list' 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: [ flags: [
cli.Flag{ cli.Flag{
name: 'limit' name: 'limit'
@ -39,7 +39,7 @@ pub fn cmd() cli.Command {
}, },
cli.Flag{ cli.Flag{
name: 'today' name: 'today'
description: 'Only list logs started today (UTC time).' description: 'Only list logs started today.'
flag: cli.FlagType.bool flag: cli.FlagType.bool
}, },
cli.Flag{ cli.Flag{
@ -49,17 +49,17 @@ pub fn cmd() cli.Command {
}, },
cli.Flag{ cli.Flag{
name: 'day' 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 flag: cli.FlagType.string
}, },
cli.Flag{ cli.Flag{
name: 'before' 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 flag: cli.FlagType.string
}, },
cli.Flag{ cli.Flag{
name: 'after' 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 flag: cli.FlagType.string
}, },
] ]
@ -84,6 +84,8 @@ pub fn cmd() cli.Command {
filter.repo = repo_id filter.repo = repo_id
} }
tz_offset := time.offset()
if cmd.flags.get_bool('today')? { if cmd.flags.get_bool('today')? {
today := time.now() today := time.now()
@ -91,7 +93,7 @@ pub fn cmd() cli.Command {
year: today.year year: today.year
month: today.month month: today.month
day: today.day day: today.day
}) }).add_seconds(-tz_offset)
filter.before = filter.after.add_days(1) filter.before = filter.after.add_days(1)
} }
// The -today flag overwrites any of the other date flags. // The -today flag overwrites any of the other date flags.
@ -102,21 +104,25 @@ pub fn cmd() cli.Command {
if day_str != '' { if day_str != '' {
day := time.parse_rfc3339(day_str)? day := time.parse_rfc3339(day_str)?
day_utc := time.new_time(time.Time{
filter.after = time.new_time(time.Time{
year: day.year year: day.year
month: day.month month: day.month
day: day.day 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 { } else {
if before_str != '' { if before_str != '' {
filter.before = time.parse_rfc3339(before_str)? filter.before = time.parse(before_str)?.add_seconds(-tz_offset)
} }
if after_str != '' { 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. // print_log_list prints a list of logs.
fn print_log_list(logs []BuildLog) ? { 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()]) it.exit_code.str()])
println(console.pretty_table(['id', 'repo', 'start time', 'exit code'], data)?) println(console.pretty_table(['id', 'repo', 'start time', 'exit code'], data)?)

View File

@ -83,6 +83,7 @@ pub fn (mut d DockerConn) inspect_container(id string) ?ContainerInspect {
mut data := json.decode(ContainerInspect, body)? 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)? data.state.start_time = time.parse_rfc3339(data.state.start_time_str)?
if data.state.status == 'exited' { if data.state.status == 'exited' {