Merge pull request 'Use local timezone with CLI' (#210) from Chewing_Bever/vieter:utc-only into dev

Reviewed-on: vieter/vieter#210
main
Jef Roosens 2022-05-31 12:55:16 +02:00
commit ec92b16a73
3 changed files with 23 additions and 15 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' {

View File

@ -17,8 +17,9 @@ pub fn (bl &BuildLog) str() string {
mut parts := [ mut parts := [
'id: $bl.id', 'id: $bl.id',
'repo id: $bl.repo_id', 'repo id: $bl.repo_id',
'start time: $bl.start_time', 'start time: $bl.start_time.local()',
'end time: $bl.end_time', 'end time: $bl.end_time.local()',
'duration: ${bl.end_time - bl.start_time}',
'arch: $bl.arch', 'arch: $bl.arch',
'exit code: $bl.exit_code', 'exit code: $bl.exit_code',
] ]