我不在乎我使用哪种语言(只要它是 Open Refine 中可用的三种语言之一),但我需要将从 API 返回的时间戳从纪元时间转换为常规日期(请参阅下面屏幕截图中的表达式框)。对输出日期格式不太挑剔,只是将日期保留到秒。谢谢!
可以使用:GREL、Jython 或 Clojure。
我不在乎我使用哪种语言(只要它是 Open Refine 中可用的三种语言之一),但我需要将从 API 返回的时间戳从纪元时间转换为常规日期(请参阅下面屏幕截图中的表达式框)。对输出日期格式不太挑剔,只是将日期保留到秒。谢谢!
可以使用:GREL、Jython 或 Clojure。
如果您必须坚持使用 GREL,您可以使用以下单线:
inc(toDate("01/01/1970 00:00:00","dd/MM/YYYY H:m:s"),value.toNumber(),"seconds").toString('yyyy-MM-dd HH:mm:ss')
inc(date d, number value, string unit)
如GREL 文档中所定义:返回在给定时间单位内按给定数量更改的日期。单位默认为“小时”
toDate(o, string format)
:返回o
转换为日期对象。(更复杂toDate()
的用法见GREL 文档)
我们使用字符串"01/01/1970 00:00:00"
作为输入toDate()
来获取 UNIX 纪元(1970 年 1 月 1 日午夜)的开始。
我们将新创建的日期对象inc()
作为第二个参数传递给结果value.toNumber()
(假设值是一个字符串表示自 Unix 纪元开始以来的秒数),作为第三个参数,该字符串"seconds"
告诉inc()
单位第二个参数。
yyyy-MM-dd HH:mm:ss
以下是使用上述函数将从时间戳生成器中获取的一系列时间戳转换为字符串日期的结果。
| Name | Value | Date String |
|-----------|------------|---------------------|
| Timestamp | 1491998962 | 2017-04-09 12:09:22 |
| +1 Hour | 1492002562 | 2017-04-09 13:09:22 |
| +1 Day | 1492085362 | 2017-04-10 12:09:22 |
| +1 Week | 1492603762 | 2017-04-16 12:09:22 |
| +1 Month | 1494590962 | 2017-05-09 12:09:22 |
| +1 Year | 1523534962 | 2018-04-09 12:09:22 |
不幸的是,我认为你不能用这样或类似的 GREL 语句来做到这一点,但我可能会对其他可以让它以某种方式工作的人感到惊喜:
value.toDate().toString("dd/MM/yyy")
所以与此同时,使用这个 Jython / Python 代码:
import time;
# This is a comment.
# We change 'value' to an integer, since time needs to work with numbers.
# If we needed to, we could also * 1000 if we had a Unix Epoch Time in seconds, instead of milliseconds.
# We also have no idea what the local time zone is for this, which could affect the date. But we digress...
epochlong = int(float(value));
datetimestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epochlong));
return datetimestamp