Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date Groovy-以毫秒为单位将时间戳字符串转换为历元时间_Date_Groovy_Time_Epoch_Jenkins Groovy - Fatal编程技术网

Date Groovy-以毫秒为单位将时间戳字符串转换为历元时间

Date Groovy-以毫秒为单位将时间戳字符串转换为历元时间,date,groovy,time,epoch,jenkins-groovy,Date,Groovy,Time,Epoch,Jenkins Groovy,我有一个时间戳字符串,如下所示: String build_time=2017-11-20T21:27:03Z 我想根据PST时区将其转换为以毫秒为单位的历元时间,这样我的结果是: long build_time_ms=1511299623000 我怎样才能做到这一点呢?您可以使用java的java.time.包来实现这一点。下面是通过docker运行groovy的工作脚本和输出。但我还没有在Jenkins中尝试过这一点,脚本可能需要添加适当的def语句以避免管道序列化问题 剧本 impor

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

String build_time=2017-11-20T21:27:03Z
我想根据PST时区将其转换为以毫秒为单位的历元时间,这样我的结果是:

long build_time_ms=1511299623000

我怎样才能做到这一点呢?

您可以使用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)

源时间的时区是什么?你试过什么?你用Java试过吗?(如果您了解Java…我问的原因是这些解决方案很容易迁移到Groovy)源时间的时区是GMT@RaoSometimes如果您不需要导入
import
,那么只需尝试
Groovy-e“println new Date().time/1000”