Splunk官方文档地址:Date and time format variables
本文列出了可以在函数strftime()和strptime()中定义时间格式的变量,这些参数也可用来在事务数据中描述时间戳。
另外可以使用relative_time()和now()函数作为参数。
目录
Date and time variables【日期和时间变量】
Variable | Description |
---|---|
%c | 采用由服务器操作系统定义的日期、时间,例如:Thu Jul 18 09:30:00 2019 for US English on Linux. |
%+ | 采用由服务器操作系统定义的日期、时间还有时区, Thu Jul 18 09:30:00 PDT 2019 for US English on Linux. |
Time variables【时间变量】
Variable | Description |
---|---|
%Ez | Splunk-specific, timezone in minutes.Splunk特指的时区以分为单位 |
%f | 十进制微秒 |
%H | 小时(24小时制),小时数由00-23表示,前导0可接受但不是必需的 |
%I | 大写字母i。小时(12 小时制),小时数由01-12表示。前导0可接受但不是必需的。与 %p一起使用可为12小时制指定AM或PM。 |
%k | 与%H一样,小时(24 小时制)。前导0替换为空格,例如0-23。 |
%M | 分钟。分钟由00到59表示。前导0可接受但不是必需的。 |
%N | 亚秒位数。默认值为%9N。您可以指定%3N=毫秒,%6N=微秒,%9N=纳秒。 |
%p | AM或PM。与%I一起使用以指定AM或PM的12小时制。不要与%H一起使用。 |
%Q | UTC 时间戳的亚秒级分量。默认值为毫秒,即 %3Q。有效值为:%3Q=毫秒,值为 000-999,%6Q=微秒,值为 000000-999999,%9Q=纳秒,值为000000000-9999999999 |
%S | 秒,例如 00 到 59。 |
%s | UNIX 纪元时间戳,或自纪元以来的秒数:1970-01-01 00:00:00 +0000 (UTC)。例如,UNIX 纪元时间1484993700等于 Tue Jan 21 10:15:00 2020。 |
%T | 以24小时制表示的时间 (%H:%M:%S)。例如,23:59:59。 |
%X | 当前区域设置格式的时间。对于US English,上午 9:30 的格式为 9:30:00。 |
%Z | 时区缩写。例如,美国东部标准时间的 EST。 |
%z | 与 UTC 的时区偏移量,以小时和分钟为单位:+hhmm 或 -hhmm。例如,UTC 之前的 5 小时,值为 -0500,即美国东部标准时间。例子:使用 %z 指定小时和分钟,例如 -0500;使用 %: z 指定用冒号分隔的小时和分钟,例如 -05:00‘使用 %:: z 指定用冒号分隔的小时、分钟和秒,例如 -05:00:00;使用 %::: z 仅指定小时,例如 -05 |
%% | 文本“%”字符。 |
Date variables【日期变量】
Variable | Description |
---|---|
%F | 相当于 %Y-%m-%d(ISO 8601 日期格式)。 |
%x | 采用当前区域设置格式的日期。例如,7/13/2019 表示US English。 |
Specifying days and weeks【指定天和周】
Variable | Description |
---|---|
%A | Full weekday name. (Sunday, …, Saturday) |
%a | Abbreviated weekday name. (Sun, … ,Sat) |
%d | Day of the month as a decimal number, includes a leading zero. (01 to 31) |
%e | Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (1 to 31) |
%j | Day of year as a decimal number, includes a leading zero. (001 to 366) |
%V (or %U) | Week of the year. The %V variable starts the count at 1, which is the most common start number. The %U variable starts the count at 0. |
%w | Weekday as a decimal number. (0 = Sunday, …, 6 = Saturday) |
Specifying months【指定月份】
Variable | Description |
---|---|
%b | Abbreviated month name. (Jan, Feb, etc.) |
%B | Full month name. (January, February, etc.) |
%m | Month as a decimal number. (01 to 12). Leading zeros are accepted but not required. |
Specifying year【指定年份】
Variable | Description |
---|---|
%y | Year as a decimal number, without the century. (00 to 99). Leading zeros are accepted but not required. |
%Y | Year as a decimal number with century. For example, 2020. |
Examples【举例】
Converting UNIX timestamps into dates
下表显示了使用strftime()的几个日期格式变量的结果。这些示例显示了用strftime()函数处理日期Fri Apr 29, 2022 23:45:22 GMT-0700(太平洋夏令时间)的结果。
Date format string | Result |
---|---|
%Y-%m-%d | 2022-04-29 |
%y-%m-%d | 22-04-29 |
%b %d, %Y | Apr 29, 2022 |
%B %d, %Y | April 29, 2022 |
%a %b %d, %Y | Fri Apr 29, 2022 |
%d %b '%y = %Y-%m-%d | %d %b '%y = %Y-%m-%d |
Converting UNIX timestamps into dates and times
下表显示了使用strftime()的几个日期时间变量的结果。这些示例显示了用strftime()函数处理日期Fri Apr 29, 2022 23:45:22 GMT-0700(太平洋夏令时间)的结果。
Date and Time format string | Result |
---|---|
%Y-%m-%dT%H:%M:%S.%Q | 2022-04-29T23:45:22.000 |
%Y-%m-%dT %H:%M:%S.%Z | 2022-04-29T 23:45:22.PDT |
%Y-%m-%dT %H:%M:%S.%QZ | 2022-04-29T 23:45:22.000Z |
%Y-%m-%dT%H:%M:%S.%QZ | 2022-04-29T23:45:22.000Z |
%Y-%m-%dT%H:%M:%S | 2022-04-29T23:45:22 |
%Y-%m-%dT%T | 2022-04-29T23:45:22 |
%m-%d-%Y %I:%M:%S %p | 04-29-2022 11:45:22 PM |
%b %d, %Y %I:%M:%S %p | Apr 29, 2022 11:45:22 PM |
%m-%d-%Y %H:%M:%S.%Q | 04-29-2022 23:45:22.000 |
%m-%d-%Y %H:%M:%S.%Q %z | 04-29-2022 23:45:22.000 -0700 |
%d/%b/%Y:%H:%M:%S.%f %z | 29/Apr/2022:23:45:22.000000 -0700 |
Converting timestamps into UNIX
下表显示了使用strptime()使用多个日期时间格式变量将时间戳变为UNIX时间格式的结果。
例如,此搜索返回 UNIX 时间 1671126322.000000。
… | eval mytime=strptime(“2022-12-15T09:45:22”,“%Y-%m-%dT%H:%M:%S”)
Timestamps | Date and Time format string | UNIX time |
---|---|---|
2022-9-25T09:45:22.000 | %Y-%m-%dT%H:%M:%S.%Q | 1664124322.000000 |
2022-12-15 09:45:22 | %Y-%m-%d %H:%M:%S | 1671126322.000000 |
The following table shows the results of searches that use time variables:
host=“www1” | eval WeekNo = strftime(_time, “%V”)
Creates a field called WeekNo and returns the values for the week numbers that correspond to the dates in the _time field.
… | eval mytime=strftime(_time,“%Y-%m-%dT%H:%M:%S.%Q”)
Creates a field called mytime and returns the converted timestamp values in the _time field. The values are stored in UNIX format and converted using the format specified, which is the ISO 8601 format. For example: 2021-04-13T14:00:15.000.
… | eval start=strptime(Sent, “%H:%M:%S.%N”), end=strptime(Received, “%H:%M:%S.%N”) | eval difference=end-start | table end, start, difference
Takes the values in the Sent and Received fields and converts them into a standard time using the strptime function. Then calculates the difference between the start and end times. The results are displayed in a table.
You can use the round function to round the difference to a specific number of decimal places. For example …| eval difference=round(end-start, 2).
For more information about working with dates and time, see Time modifiers for search and About searching with time in the Search Manual.
Refer to the list of tz database time zones for all permissible time zone values. For more information about how the Splunk software determines a time zone and the tz database, see Specify time zones for timestamps in Getting Data In.
Subsecond time variables such as %N and %Q can be used in metrics searches of metrics indexes that are enabled for millisecond timestamp resolution.
For more information about enabling metrics indexes to index metric data points with millisecond timestamp precision:
- For Splunk Cloud Platform, see Manage Splunk Cloud Platform indexes in the Splunk Cloud Platform Admin Manual.
- For Splunk Enterprise, see Create custom indexes in Managing indexers and clusters of indexers.