Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert all time stamps in JSON file to unix time stamp in bash(Ubuntu)

I am trying to convert all time stamps in a file to the Unix time format preferably using jq/bash/python.

The original format is kinda odd:

%Y-%m-%dT%H:%M:%S.%z

The sample file content from GoToMeeting API:

[
  {
    "Last Name": "John",
    "Subject": "Meet Now",
    "meetingId": "983329197",
    "meetingType": "immediate",
    "First Name": "Doe",
    "Conference Info": "111-222-333",
    "startdate": "2017-01-25T04:50:25.+0000",
    "enddate": "2017-01-25T05:50:25.+0000",
    "status": "INACTIVE"
  },
  {
    "Last Name": "John",
    "Subject": "dumb meeting",
    "meetingId": "569996685",
    "meetingType": "scheduled",
    "First Name": "Doe",
    "Conference Info": "111-222-333",
    "startdate": "2017-02-15T10:00:00.+0000",
    "enddate": "2017-02-15T10:30:00.+0000",
    "status": "INACTIVE"
  }
]

I was able to convert just one single value to Unix by running this:

Input:

jq 'strptime("%Y-%m-%dT%H:%M:%S.%z") | mktime';
"2015-03-05T04:50:25.+0000"

Output:

1425531025

The main goal of this is to filter out all dates older than the current time and then convert it to local time.

like image 298
Randomizer Avatar asked Sep 19 '25 17:09

Randomizer


1 Answers

To tackle both the first question and the main goal, you'll probably want to define a convenience function, which is here specified according to your one-liner:

def convert: strptime("%Y-%m-%dT%H:%M:%S.%z") | mktime;

The filter to answer the first question could then be written as follows:

map( ( .startdate |= convert) | (.enddate |= convert) )

To select the objects of interest, you could use the above def with this filter:

now as $now
| map( select((.startdate|convert >= $now) and (.enddate|convert >= $now)))

Unfortunately, jq's current support for time zones is virtually nil, so I'm not sure how you'll want to convert back to "local time". With some ingenuity and string manipulation, it could be done in jq, though maybe not in the most general way.

like image 189
peak Avatar answered Sep 21 '25 13:09

peak