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

我正在尝试将文件中的所有时间戳转换为Unix时间格式,最好使用jq/bash/python。

The original format is kinda odd:

原来的格式有点奇怪:

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

The sample file content from GoToMeeting API:

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:

我可以通过运行这个方法将一个值转换为Unix:

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.

这样做的主要目的是过滤掉比当前时间更早的所有日期,然后将其转换为本地时间。

1 个解决方案

#1


1

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:

要选择感兴趣的对象,您可以使用上述def与此过滤器:

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.

不幸的是,jq目前对时区的支持几乎为零,因此我不确定您将如何将其转换为“本地时间”。通过一些精巧的设计和字符串处理,可以在jq中完成,尽管可能不是最通用的方式。

更多相关文章

  1. 时间总是过得比你想像中的快,理想总是会被四周的事情所掩盖,希望能
  2. Linux下的文件时间
  3. linux 下的时间获取函数
  4. 嵌入式linux系统如何微秒级采样以及while循环时间波动很大
  5. linux设置时间和把时间从UTC转成本地时间的
  6. sql语句延时执行或者是指定时间执行
  7. Mysql查询需要太多时间来执行。
  8. PHP获取MySQL执行sql语句的查询时间
  9. 我想在每次更新或在SQL Server中插入行时获取当前日期时间

随机推荐

  1. Android(安卓)游戏开发中横竖屏切换问题
  2. Android提供的系统服务之--WindowManager
  3. [转]: 两分钟彻底让你明白Android(安卓)A
  4. Android的控件属性
  5. 驾考一点通 android
  6. 扣丁学堂笔记第05天高级UI组件(一)
  7. Android之AudioRecord实现"助听器"
  8. Androd学习笔记——Conflict between And
  9. Android中的Drawable资源—— ScaleDrawa
  10. Qt平台下OpenCV for Android库的顺序