Skip to content

CLI: accept RFC3339-formatted dates in relevant arguments #1263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions go/cli/mcap/cmd/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (

var (
addAttachmentLogTime uint64
addAttachmentLogDate string
addAttachmentName string
addAttachmentCreationTime uint64
addAttachmentCreationDate string
addAttachmentFilename string
addAttachmentMediaType string
)
Expand Down Expand Up @@ -158,10 +160,24 @@ var addAttachmentCmd = &cobra.Command{
if addAttachmentCreationTime > 0 {
createTime = addAttachmentCreationTime
}
if addAttachmentCreationDate != "" {
date, err := parseDate(addAttachmentCreationDate)
if err != nil {
die("failed to parse creation date: %s", err)
}
createTime = date
}
logTime := uint64(time.Now().UTC().UnixNano())
if addAttachmentLogTime > 0 {
logTime = addAttachmentLogTime
}
if addAttachmentLogDate != "" {
date, err := parseDate(addAttachmentLogDate)
if err != nil {
die("failed to parse log date: %s", err)
}
logTime = date
}
err = utils.AmendMCAP(f, []*mcap.Attachment{
{
LogTime: logTime,
Expand Down Expand Up @@ -190,9 +206,15 @@ func init() {
addAttachmentCmd.PersistentFlags().Uint64VarP(
&addAttachmentLogTime, "log-time", "", 0, "attachment log time in nanoseconds (defaults to current timestamp)",
)
addAttachmentCmd.PersistentFlags().StringVarP(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think these are fine on a single line and easier to read

&addAttachmentLogDate, "log-date", "", "", "RFC3339-formatted log date",
)
addAttachmentCmd.PersistentFlags().Uint64VarP(
&addAttachmentLogTime, "creation-time", "", 0, "attachment creation time in nanoseconds (defaults to ctime)",
)
addAttachmentCmd.PersistentFlags().StringVarP(
&addAttachmentCreationDate, "creation-date", "", "", "RFC3339-formatted creation date",
)
err := addAttachmentCmd.MarkPersistentFlagRequired("file")
if err != nil {
die("failed to mark --file flag as required: %s", err)
Expand Down
41 changes: 40 additions & 1 deletion go/cli/mcap/cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math"
"os"
"regexp"
"time"

"github.com/foxglove/mcap/go/cli/mcap/utils"
"github.com/foxglove/mcap/go/mcap"
Expand All @@ -22,6 +23,8 @@ type filterFlags struct {
endSec uint64
startNano uint64
endNano uint64
startDate string
endDate string
includeMetadata bool
includeAttachments bool
outputCompression string
Expand All @@ -41,6 +44,14 @@ type filterOpts struct {
chunkSize int64
}

func parseDate(date string) (uint64, error) {
stamp, err := time.Parse(time.RFC3339, date)
if err != nil {
return 0, err
}
return uint64(stamp.UnixNano()), nil
}

func buildFilterOptions(flags *filterFlags) (*filterOpts, error) {
opts := &filterOpts{
output: flags.output,
Expand All @@ -51,9 +62,23 @@ func buildFilterOptions(flags *filterFlags) (*filterOpts, error) {
if flags.startSec > 0 {
opts.start = flags.startSec * 1e9
}
if flags.startDate != "" {
start, err := parseDate(flags.startDate)
if err != nil {
return nil, fmt.Errorf("invalid start date: %w", err)
}
opts.start = start
}
opts.end = flags.endNano
if flags.endSec > 0 {
opts.end = flags.endSec * 1e9
opts.end = flags.startSec * 1e9
}
if flags.endDate != "" {
end, err := parseDate(flags.endDate)
if err != nil {
return nil, fmt.Errorf("invalid end date: %w", err)
}
opts.end = end
}
if opts.end == 0 {
opts.end = math.MaxUint64
Expand Down Expand Up @@ -416,6 +441,18 @@ usage:
0,
"messages with log times before nanosecond-precision timestamp will be included.",
)
startDate := filterCmd.PersistentFlags().String(
"start-date",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit start-datetime? Since it is both a date and time? Probably I'd even say that start is sufficient and can accept the start string as RFC3339 or the nanosecond variant. Not "real" reason that the program can't figure it out between those two formatss

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should start and start-secs accept an RFC3339 formatted timestamp in that world?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start-secs I'd have a harder time making an argument for given it says "secs". start seems to be asking for flexible support input formats

"",
"messages with log times after or equal to this RFC3339-formatted date will be included."+
" Assumes log times are relative to Jan 1 1970 UTC.",
)
endDate := filterCmd.PersistentFlags().String(
"end-date",
"",
"messages with log times before this RFC3339-formatted date will be included. Assumes"+
" log times are relative to Jan 1 1970 UTC.",
)
filterCmd.MarkFlagsMutuallyExclusive("start-secs", "start-nsecs")
filterCmd.MarkFlagsMutuallyExclusive("end-secs", "end-nsecs")
chunkSize := filterCmd.PersistentFlags().Int64P("chunk-size", "", 4*1024*1024, "chunk size of output file")
Expand Down Expand Up @@ -443,6 +480,8 @@ usage:
endSec: *endSec,
startNano: *startNano,
endNano: *endNano,
startDate: *startDate,
endDate: *endDate,
chunkSize: *chunkSize,
includeMetadata: *includeMetadata,
includeAttachments: *includeAttachments,
Expand Down
10 changes: 10 additions & 0 deletions go/cli/mcap/cmd/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,13 @@ func TestCompileMatchers(t *testing.T) {
assert.True(t, matchers[0].MatchString("camera"))
assert.True(t, matchers[1].MatchString("lights"))
}

func TestParseDate(t *testing.T) {
expected := uint64(1690298850132545471)
zulu, err := parseDate("2023-07-25T15:27:30.132545471Z")
require.NoError(t, err)
assert.Equal(t, expected, zulu)
withTimezone, err := parseDate("2023-07-26T01:27:30.132545471+10:00")
require.NoError(t, err)
assert.Equal(t, expected, withTimezone)
}
Loading