【问题标题】:Groovy - Convert a timestamp string to Epoch time in millisecondsGroovy - 以毫秒为单位将时间戳字符串转换为纪元时间
【发布时间】:2018-05-04 08:27:52
【问题描述】:

我有一个时间戳字符串如下:

String build_time=2017-11-20T21:27:03Z

我想根据 PST 时区将其转换为以毫秒为单位的纪元时间,这样我的结果是:

long build_time_ms=1511299623000

我该怎么做?

【问题讨论】:

  • 源时间的时区是什么?
  • 你试过什么?你在Java中试过吗? (如果你知道 Java ......我问的原因是这些解决方案很容易迁移到 Groovy)
  • 源时间的时区是格林威治标准时间@Rao

标签: date groovy time epoch jenkins-groovy


【解决方案1】:

您可以使用 java 的 java.time.* 包来实现这一点。下面是通过 docker 运行 groovy 的工作脚本和输出。不过我还没有在 Jenkins 中尝试过,脚本可能需要添加适当的 def 语句以避免管道序列化问题

脚本

import java.time.*

// build time as string
build_time='2017-11-20T21:27:03Z'

// parse and get epoch
time=Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", build_time)

// get epoch milis
epoch_milis = time.getTime()

// create UTC local time
local_dt = LocalDateTime.ofInstant(Instant.ofEpochMilli(epoch_milis), ZoneId.of('UTC'));

// created zoned time out of UTC time
zoned_dt = local_dt.atZone(ZoneId.of('America/Los_Angeles'))

// get offset in milis
offset_ms = zoned_dt.getOffset().getTotalSeconds() * 1000

// add to UTC epoc
local_timestamp = epoch_milis + offset_ms

println "Time is ${local_timestamp}"

脚本输出,在 groovy REPL 中运行

$ docker run --rm -it groovy
Nov 21, 2017 3:37:26 AM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
Groovy Shell (2.4.12, JVM: 1.8.0_141)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> import java.time.*
===> java.time.*
groovy:000> 
groovy:000> // build time as string
===> true
groovy:000> build_time='2017-11-20T21:27:03Z'
===> 2017-11-20T21:27:03Z
groovy:000> 
groovy:000> // parse and get epoch
===> true
groovy:000> time=Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", build_time)
===> Mon Nov 20 21:27:03 UTC 2017
groovy:000> 
groovy:000> // get epoch milis
===> true
groovy:000> epoch_milis = time.getTime()
===> 1511213223000
groovy:000> 
groovy:000> // create UTC local time
===> true
groovy:000> local_dt = LocalDateTime.ofInstant(Instant.ofEpochMilli(epoch_milis), ZoneId.of('UTC'));
===> 2017-11-20T21:27:03
groovy:000> 
groovy:000> // created zoned time out of UTC time
===> true
groovy:000> zoned_dt = local_dt.atZone(ZoneId.of('America/Los_Angeles'))
===> 2017-11-20T21:27:03-08:00[America/Los_Angeles]
groovy:000> 
groovy:000> // get offset in milis
===> true
groovy:000> offset_ms = zoned_dt.getOffset().getTotalSeconds() * 1000
===> -28800000
groovy:000> 
groovy:000> // add to UTC epoc
===> true
groovy:000> local_timestamp = epoch_milis + offset_ms
===> 1511184423000
groovy:000> 
groovy:000> println "Time is ${local_timestamp}"
Time is 1511184423000

另外,在您的示例中,您给出的结果为 1511299623000 毫秒,这似乎比时间戳早 24 小时,查看 javascript 控制台,输入来自您的示例

new Date('2017-11-20T21:27:03Z')
>> Tue Nov 21 2017 08:27:03 GMT+1100 (AEDT)

new Date(1511299623000)
>> Wed Nov 22 2017 08:27:03 GMT+1100 (AEDT)

【讨论】:

  • 有时如果你不需要/想要import,那么试试groovy -e "println new Date().time / 1000"
猜你喜欢
  • 2021-03-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 2013-08-19
  • 2017-02-17
  • 2016-08-01
  • 2015-06-17
相关资源
最近更新 更多