Splunk Search

How to get current GMT time?

Sundried
Explorer

I have a search: 

 

 

search | eval difference=now() - strptime(createdDate,"%Y-%m-%d %H:%M:%S.%3N")

 

 

 This works, except the createdDate field from my results are in GMT+0, whilst I'm in GMT+10 so there's 10 hours added to every result. I was going to just do a -36000 bandaid fix but after daylight savings this would break.

How can I get current GMT time?

Labels (1)
1 Solution

to4kawa
Ultra Champion
| makeresults
| eval time=strftime(_time,"%FT%T %:::z")
| eval offset = substr(time,21,23)
| eval time_args = if( -1 * offset >= 0, "+".substr(offset,2,3), printf("%03d",-1 * offset))
| eval GMT = ceil(relative_time(_time,time_args."h"))
| convert ctime(GMT)

now() returns epoch time(UTC). try my logic (change _time to now()).

View solution in original post

niketn
Legend

@Sundried try the following SPL

| eval createdDate=strptime(strftime(createdDate,"%Y-%m-%d %H:%M:%S.%3N")."+0000","%Y-%m-%d %H:%M:%S.%3N%z")

 Following is a run anywhere example:

| makeresults
| eval currentTimeInGMT=strptime(strftime(now(),"%Y-%m-%d %H:%M:%S.%3N")."+0000","%Y-%m-%d %H:%M:%S.%3N%z")
| fieldformat currentTimeInGMT=strftime(currentTimeInGMT,"%Y-%m-%d %H:%M:%S.%3N")
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

Sundried
Explorer

@niketn 

This doesn't seem to get the current GMT time. At least not for me. I had to change the "+0000" to "+2000". In this case, won't it still fail after daylight savings?

0 Karma

to4kawa
Ultra Champion
| makeresults
| eval time=strftime(_time,"%FT%T %:::z")
| eval offset = substr(time,21,23)
| eval time_args = if( -1 * offset >= 0, "+".substr(offset,2,3), printf("%03d",-1 * offset))
| eval GMT = ceil(relative_time(_time,time_args."h"))
| convert ctime(GMT)

now() returns epoch time(UTC). try my logic (change _time to now()).

Sundried
Explorer

@to4kawa it works, thanks!

0 Karma

harsmarvania57
Ultra Champion

Change your user profile setting with timezone GMT.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Mastering Threat Intelligence in ES 8.5, Splunk AI Assistant v2, and More from Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...

Break the Build: Inside the KubeDoom Lounge at .conf26

    You step up to the machine. The pixelated corridors of a certain 1993 FPS load in front of you, EMP Pulse ...

Splunk Auto Ingestion Parallel Pipeline Scaling

Why this feature matters Many Splunk environments experience ingestion pressure long before the host is fully ...