【Splunk】日期和时间格式变量

Splunk官方文档地址:Date and time format variables
本文列出了可以在函数strftime()和strptime()中定义时间格式的变量,这些参数也可用来在事务数据中描述时间戳。
另外可以使用relative_time()和now()函数作为参数。



Date and time variables【日期和时间变量】

VariableDescription
%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【时间变量】

VariableDescription
%EzSplunk-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=纳秒。
%pAM或PM。与%I一起使用以指定AM或PM的12小时制。不要与%H一起使用。
%QUTC 时间戳的亚秒级分量。默认值为毫秒,即 %3Q。有效值为:%3Q=毫秒,值为 000-999,%6Q=微秒,值为 000000-999999,%9Q=纳秒,值为000000000-9999999999
%S秒,例如 00 到 59。
%sUNIX 纪元时间戳,或自纪元以来的秒数: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【日期变量】

VariableDescription
%F相当于 %Y-%m-%d(ISO 8601 日期格式)。
%x采用当前区域设置格式的日期。例如,7/13/2019 表示US English。

Specifying days and weeks【指定天和周】

VariableDescription
%AFull weekday name. (Sunday, …, Saturday)
%aAbbreviated weekday name. (Sun, … ,Sat)
%dDay of the month as a decimal number, includes a leading zero. (01 to 31)
%eLike %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (1 to 31)
%jDay 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.
%wWeekday as a decimal number. (0 = Sunday, …, 6 = Saturday)

Specifying months【指定月份】

VariableDescription
%bAbbreviated month name. (Jan, Feb, etc.)
%BFull month name. (January, February, etc.)
%mMonth as a decimal number. (01 to 12). Leading zeros are accepted but not required.

Specifying year【指定年份】

VariableDescription
%yYear as a decimal number, without the century. (00 to 99). Leading zeros are accepted but not required.
%YYear 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 stringResult
%Y-%m-%d2022-04-29
%y-%m-%d22-04-29
%b %d, %YApr 29, 2022
%B %d, %YApril 29, 2022
%a %b %d, %YFri 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 stringResult
%Y-%m-%dT%H:%M:%S.%Q2022-04-29T23:45:22.000
%Y-%m-%dT %H:%M:%S.%Z2022-04-29T 23:45:22.PDT
%Y-%m-%dT %H:%M:%S.%QZ2022-04-29T 23:45:22.000Z
%Y-%m-%dT%H:%M:%S.%QZ2022-04-29T23:45:22.000Z
%Y-%m-%dT%H:%M:%S2022-04-29T23:45:22
%Y-%m-%dT%T2022-04-29T23:45:22
%m-%d-%Y %I:%M:%S %p04-29-2022 11:45:22 PM
%b %d, %Y %I:%M:%S %pApr 29, 2022 11:45:22 PM
%m-%d-%Y %H:%M:%S.%Q04-29-2022 23:45:22.000
%m-%d-%Y %H:%M:%S.%Q %z04-29-2022 23:45:22.000 -0700
%d/%b/%Y:%H:%M:%S.%f %z29/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”)

TimestampsDate and Time format stringUNIX time
2022-9-25T09:45:22.000%Y-%m-%dT%H:%M:%S.%Q1664124322.000000
2022-12-15 09:45:22%Y-%m-%d %H:%M:%S1671126322.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:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值