Bitcoin Forum
May 08, 2024, 04:34:26 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Parsing unix timestamp from CLI response with jq  (Read 122 times)
rpstatic (OP)
Jr. Member
*
Offline Offline

Activity: 53
Merit: 7


View Profile
July 18, 2018, 11:55:16 AM
 #1

I would like to query data with the CLI and parse the response. I am using the terminal with jq for this.

{                                                                                                                                                   
  "txid": "5756ff16e2b9f881cd15b8a7e478b4899965f87f553b6210d0f8e5bf5be7df1d",                                                                                                                                                                                               
  "blockhash": "0000000000000000027d0985fef71cbc05a5ee5cdbdc4c6baf2307e6c5db8591",                                                                   
  "blockheight": 371622,                                                                                                                             
  "confirmations": 160829,                                                                                                                           
  "time": 1440604784,                                                                                                                               
  "blocktime": 1440604784,                                                                                                                           
  "isCoinBase": true,                                                                                                                               
  "valueOut": 25.37726812,                                                                                                                           
  "size": 185                                                                                                                                       
}

My goal is to change the unix timestamps to another format and pipe the result to another command.

So, the block time should be changed from this
  "blocktime": 1440604784,                                                                                                                           

to this
  "blocktime": "18.07.2018"
1715142866
Hero Member
*
Offline Offline

Posts: 1715142866

View Profile Personal Message (Offline)

Ignore
1715142866
Reply with quote  #2

1715142866
Report to moderator
1715142866
Hero Member
*
Offline Offline

Posts: 1715142866

View Profile Personal Message (Offline)

Ignore
1715142866
Reply with quote  #2

1715142866
Report to moderator
In order to achieve higher forum ranks, you need both activity points and merit points.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715142866
Hero Member
*
Offline Offline

Posts: 1715142866

View Profile Personal Message (Offline)

Ignore
1715142866
Reply with quote  #2

1715142866
Report to moderator
1715142866
Hero Member
*
Offline Offline

Posts: 1715142866

View Profile Personal Message (Offline)

Ignore
1715142866
Reply with quote  #2

1715142866
Report to moderator
jseverson
Hero Member
*****
Offline Offline

Activity: 1834
Merit: 759


View Profile
July 18, 2018, 02:38:21 PM
 #2

Not sure if you can use this but if you simply want to convert that to a readable date, you can use:

Code:
date -d @1440604784 +'%d.%m.%Y'

The output would be:

26.08.2015

BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
July 18, 2018, 03:20:26 PM
 #3

Not sure if you can use this but if you simply want to convert that to a readable date, you can use:

Code:
date -d @1440604784 +'%d.%m.%Y'

The output would be:

26.08.2015

i think you have to multiply the Unix time by 1000 because Unix time is in Seconds but the "date" takes milliseconds.
also the output should also contain time 08/26/2015 @ 3:59pm

There is a FOMO brewing...
jseverson
Hero Member
*****
Offline Offline

Activity: 1834
Merit: 759


View Profile
July 20, 2018, 02:34:31 AM
 #4

i think you have to multiply the Unix time by 1000 because Unix time is in Seconds but the "date" takes milliseconds.
also the output should also contain time 08/26/2015 @ 3:59pm

The CLI output he quoted must be in milliseconds then, because I only took the blockTime variable, plugged it into the command, and formatted it the way he wanted. It looks like he only wanted the date so I cut out the time.

rpstatic (OP)
Jr. Member
*
Offline Offline

Activity: 53
Merit: 7


View Profile
July 20, 2018, 07:50:28 AM
 #5

Not sure if you can use this but if you simply want to convert that to a readable date, you can use:

Code:
date -d @1440604784 +'%d.%m.%Y'

The output would be:

26.08.2015

Thank you. I used another approach, but your idea helped me to find the right solution for my issue.

e.g. (output truncated):
Code:
bitcoin-cli listtransactions | jq '.[].time |=(. | strftime("%Y-%m-%d"))'

Result:
Code:
{
  "txid": "5756ff16e2b9f881cd15b8a7e478b4899965f87f553b6210d0f8e5bf5be7df1d",
  "blockhash": "0000000000000000027d0985fef71cbc05a5ee5cdbdc4c6baf2307e6c5db8591",
  "blockheight": 371622,
  "confirmations": 160829,
  "time": "2015-08-26",
  "blocktime": 1440604784,
  "isCoinBase": true,
  "valueOut": 25.37726812,
  "size": 185
}
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!