KBEC-00224 - Date Manipulation in Perl

Article ID:360032829512
1 minute readKnowledge base

Summary

CloudBees CD (CloudBees Flow) server accepts dates in ISO8601 form ("Zulu" times), that is, yyyy-MM-ddTHH:mm:ssZ, for example: 2010-02-26T23:30:31.899Z.

How do I convert it to a different format and/or timezone?

Solution

Use a language of your choice (ec-perl, javascript, shell etc.) to convert this string into the format you want.
Use language-specific tools to convert the date from one timezone to another.

Examples - using ec-perl

  1. Changing the format of the job property value /myJob/createTime to yyyyMMddhhmmss

use strict;


use ElectricCommander;


my $ec = ElectricCommander();


my $createtime = $ec->getProperty("/myjob/createTime")->findvalue("//value");


$createtime =~ s/T|:|-|\..+$//g;


print $createtime;
  1. Converting /myjob/createTime to Eastern Time.

use strict;


use Date::Manip;


use ElectricCommander;


my $ec = new ElectricCommander();





my $createtime = $ec->getProperty("/myjob/createTime")->findvalue("//value");


$createtime =~ s/\.\d+Z$//;


$createtime =~ s/T/ /;


print "createtime = $createtime\n";





Date_Init("TZ=GMT");


$createtime = ParseDate($createtime);


my $formattedtime = UnixDate($createtime, "%Y%m%d%H%M%S");


print "formattedtime = $formattedtime\n";





$createtime = Date_ConvTZ($createtime, 'GMT', 'EST');


$formattedtime = UnixDate($createtime, "%Y%m%d%H%M%S");


print "formattedtime = $formattedtime\n";

Please note…​

  • There is more than one way to accomplish this using Perl.
    The above examples are only intended to give you an idea; they are not a required way of accomplishing the change.

  • You are not limited to only using the Perl modules that come with CloudBees CD (CloudBees Flow).
    If they do not cover your scenario, you can