1

I am taking a user input, "2023-09-02" (YYYY-DD-MM) and converting it to the specified datetime format(RFC3339) as specified in the official docs:

--since-time="": Only return logs after a specific date (RFC3339)

Here is the section responsible for the conversion:

date, err := time.Parse(time.RFC3339, "2023-09-02")
if err != nil {
    log.Fatal(err)
}
since = date.String()

the error I get is:

parsing time "2023-09-02" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "T"

I altered the format according to this article and altered the code to:

date, err := time.Parse("2006-01-02 15:04:05 -0700 MST", "2023-09-02")

the error that I get now is:

parsing time "2023-09-02" as "2006-01-02 15:04:05 -0700 MST": cannot parse "" as "15"

I understand that it's trying to parse the "15" of the hour but there isn't an hour provided. Is there a way to default it to midnight that day?

edit: after changing it to:

d, err := time.Parse("2006-01-02", since)
if err != nil {
    log.Fatal(err)
}
sinceTime = d.Format(time.RFC3339)

sinceTime = 2023-09-02T00:00:00Z

but when I use that month in kubectl the command freezes $ kubectl logs -n <NAME_SPACE> --container=<CONTAINER> -f --timestamps=true --since-time=2023-09-02T00:00:00Z --tail 500 -l app=<APP>

FIX: d, err := time.Parse("2006-02-01", since)

Ctfrancia
  • 1,248
  • 1
  • 15
  • 39

1 Answers1

0

The issue is with your layout string not matching your input.

If you specify your custom format string as "2006-01-02 15:04:05 -0700 MST" your input has to match it exactly, you can't just leave out the time-portion of the timestamp.

Note that the error message is saying that it is looking for the "15" part in your layout but can't find it (cannot parse "") as there's nothing there.

If you just want to parse the date you can try:

t, err := time.Parse("2006-02-01", "2023-09-02")

Which will return the following timestamp

2023-02-09 00:00:00 +0000 UTC

Without any explicitly stated time zone, it will always default to UTC.

Fiduro
  • 88
  • 1
  • 6