Skip to content

Commit 5cddf62

Browse files
authored
[pkg/stanza] Improve timestamp parsing documentation (#31490)
This PR adds explicit sections to describe the meaning of the various `layout_types` along with a table showing the available `strptime` and `gotime` directives.
1 parent a62c712 commit 5cddf62

File tree

2 files changed

+92
-21
lines changed

2 files changed

+92
-21
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/stanza
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Improve timestamp parsing documentation
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [31490]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/stanza/docs/types/timestamp.md

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,71 @@ If a timestamp block is specified, the parser operator will perform the timestam
1313
| `layout` | required | The exact layout of the timestamp to be parsed. |
1414
| `location` | `Local` | The geographic location (timezone) to use when parsing a timestamp that does not include a timezone. The available locations depend on the local IANA Time Zone database. [This page](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) contains many examples, such as `America/New_York`. |
1515

16+
## Layout Types
17+
18+
### `strptime` and `gotime`
19+
20+
The `strptime` layout type approximates familiar strptime/strftime formats. See the table below for a list of supported directives.
21+
22+
The `gotime` layout type uses Golang's native time parsing capabilities. Golang takes an [unconventional approach](https://www.pauladamsmith.com/blog/2011/05/go_time.html) to time parsing. Finer details are documented [here](https://golang.org/src/time/format.go?s=25102:25148#L9).
23+
24+
| `strptime` directive | `gotime` equivalent | Description |
25+
| --- | --- | --- |
26+
| `%Y` | `2006` | Year, zero-padded (0001, 0002, ..., 2019, 2020, ..., 9999) |
27+
| `%y` | `06` | Year, last two digits, zero-padded (01, ..., 99) |
28+
| `%m` | `01` | Month as a decimal number (01, 02, ..., 12) |
29+
| `%o` | `_1` | Month as a space-padded number ( 1, 2, ..., 12) |
30+
| `%q` | `1` | Month as a unpadded number (1,2,...,12) |
31+
| `%b` | `Jan` | Abbreviated month name (Jan, Feb, ...) |
32+
| `%B` | `January` | Full month name (January, February, ...) |
33+
| `%d` | `02` | Day of the month, zero-padded (01, 02, ..., 31) |
34+
| `%e` | `_2` | Day of the month, space-padded ( 1, 2, ..., 31) |
35+
| `%g` | `2` | Day of the month, unpadded (1,2,...,31) |
36+
| `%a` | `Mon` | Abbreviated weekday name (Sun, Mon, ...) |
37+
| `%A` | `Monday` | Full weekday name (Sunday, Monday, ...) |
38+
| `%H` | `15` | Hour (24-hour clock) as a zero-padded decimal number (00, ..., 24) |
39+
| `%I` | `3` | Hour (12-hour clock) as a zero-padded decimal number (00, ..., 12) |
40+
| `%l` | `03` | Hour (12-hour clock: 0, ..., 12) |
41+
| `%p` | `PM` | Locale’s equivalent of either AM or PM |
42+
| `%P` | `pm` | Locale’s equivalent of either am or pm |
43+
| `%M` | `04` | Minute, zero-padded (00, 01, ..., 59) |
44+
| `%S` | `05` | Second as a zero-padded decimal number (00, 01, ..., 59) |
45+
| `%L` | `999` | Millisecond as a decimal number, zero-padded on the left (000, 001, ..., 999) |
46+
| `%f` | `999999` | Microsecond as a decimal number, zero-padded on the left (000000, ..., 999999) |
47+
| `%s` | `99999999` | Nanosecond as a decimal number, zero-padded on the left (000000, ..., 999999) |
48+
| `%Z` | `MST` | Timezone name or abbreviation or empty (UTC, EST, CST) |
49+
| `%z` | `Z0700` | UTC offset in the form ±HHMM[SS[.ffffff]] or empty(+0000, -0400) |
50+
| `%i` | `-07` | UTC offset in the form ±HH or empty(+00, -04) |
51+
| `%j` | `-07:00` | UTC offset in the form ±HH:MM or empty(+00:00, -04:00) |
52+
| `%k` | `-07:00:00` | UTC offset in the form ±HH:MM:SS or empty(+00:00:00, -04:00:00) |
53+
| `%D` | `01/02/2006` | Short MM/DD/YY date, equivalent to `%m/%d/%y` |
54+
| `%F` | `2006-01-02` | Short YYYY-MM-DD date, equivalent to `%Y-%m-%d` |
55+
| `%T` | `15:04:05` | ISO 8601 time format (HH:MM:SS), equivalent to `%H:%M:%S` |
56+
| `%r` | `03:04:05 pm` | 12-hour clock time, equivalent to `%l:%M:%S %p` |
57+
| `%c` | `Mon Jan 02 15:04:05 2006` | Date and time representation, equivalent to `%a %b %d %H:%M:%S %Y` |
58+
| `%R` | `15:04` | 24-hour HH:MM time, equivalent to `%H:%M` |
59+
| `%n` | `\n` | New-line character |
60+
| `%t` | `\t` | Horizontal-tab character |
61+
| `%%` | `%` | Percent sign |
62+
63+
### `epoch`
64+
65+
The `epoch` layout type uses can consume epoch-based timestamps. The following layouts are supported:
66+
67+
| Layout | Meaning | Example | `parse_from` data type support |
68+
| --- | --- | --- | --- |
69+
| `s` | Seconds since the epoch | 1136214245 | `string`, `int64`, `float64` |
70+
| `ms` | Milliseconds since the epoch | 1136214245123 | `string`, `int64`, `float64` |
71+
| `us` | Microseconds since the epoch | 1136214245123456 | `string`, `int64`, `float64` |
72+
| `ns` | Nanoseconds since the epoch | 1136214245123456789 | `string`, `int64`, `float64`<sup>[2]</sup> |
73+
| `s.ms` | Seconds plus milliseconds since the epoch | 1136214245.123 | `string`, `int64`<sup>[1]</sup>, `float64` |
74+
| `s.us` | Seconds plus microseconds since the epoch | 1136214245.123456 | `string`, `int64`<sup>[1]</sup>, `float64` |
75+
| `s.ns` | Seconds plus nanoseconds since the epoch | 1136214245.123456789 | `string`, `int64`<sup>[1]</sup>, `float64`<sup>[2]</sup> |
76+
77+
<sub>[1] Interpretted as seconds. Equivalent to using `s` layout.</sub><br/>
78+
<sub>[2] Due to floating point precision limitations, loss of up to 100ns may be expected.</sub>
79+
80+
1681
### How to specify timestamp parsing parameters
1782

1883
```yaml
@@ -145,8 +210,6 @@ but you could also use a `timestamp` block inside the `json_parser`.
145210

146211
#### Parse a timestamp using a `strptime` layout
147212

148-
The default `layout_type` is `strptime`, which uses "directives" such as `%Y` (4-digit year) and `%H` (2-digit hour). A full list of supported directives is found [here](../../../../internal/coreinternal/timeutils/internal/ctimefmt/ctimefmt.go#L68).
149-
150213
Configuration:
151214
```yaml
152215
- type: time_parser
@@ -187,8 +250,6 @@ Configuration:
187250

188251
#### Parse a timestamp using a `gotime` layout
189252

190-
The `gotime` layout type uses Golang's native time parsing capabilities. Golang takes an [unconventional approach](https://www.pauladamsmith.com/blog/2011/05/go_time.html) to time parsing. Finer details are well-documented [here](https://golang.org/src/time/format.go?s=25102:25148#L9).
191-
192253
Configuration:
193254
```yaml
194255
- type: time_parser
@@ -229,23 +290,6 @@ Configuration:
229290

230291
#### Parse a timestamp using an `epoch` layout
231292

232-
The `epoch` layout type uses can consume epoch-based timestamps. The following layouts are supported:
233-
234-
| Layout | Meaning | Example | `parse_from` data type support |
235-
| --- | --- | --- | --- |
236-
| `s` | Seconds since the epoch | 1136214245 | `string`, `int64`, `float64` |
237-
| `ms` | Milliseconds since the epoch | 1136214245123 | `string`, `int64`, `float64` |
238-
| `us` | Microseconds since the epoch | 1136214245123456 | `string`, `int64`, `float64` |
239-
| `ns` | Nanoseconds since the epoch | 1136214245123456789 | `string`, `int64`, `float64`<sup>[2]</sup> |
240-
| `s.ms` | Seconds plus milliseconds since the epoch | 1136214245.123 | `string`, `int64`<sup>[1]</sup>, `float64` |
241-
| `s.us` | Seconds plus microseconds since the epoch | 1136214245.123456 | `string`, `int64`<sup>[1]</sup>, `float64` |
242-
| `s.ns` | Seconds plus nanoseconds since the epoch | 1136214245.123456789 | `string`, `int64`<sup>[1]</sup>, `float64`<sup>[2]</sup> |
243-
244-
<sub>[1] Interpretted as seconds. Equivalent to using `s` layout.</sub><br/>
245-
<sub>[2] Due to floating point precision limitations, loss of up to 100ns may be expected.</sub>
246-
247-
248-
249293
Configuration:
250294
```yaml
251295
- type: time_parser

0 commit comments

Comments
 (0)