Converter: Workflow Datetime to ISO-Datetime-String

Converter: Workflow Datetime to ISO-Datetime-String
Converter: Workflow Datetime to ISO-Datetime-String
Converts Datetime type data (“2021-12-31 20:34” JST) into ISO 8601 datetime string data (“2021-12-31T20:34:00+09:00” or “2021-12-31T11:34:00Z”). As a general rule Datetime type data is regarded as the time of the Workflow Platform time zone.
Configs
  • A1: Set Workflow Datetime data (eg “2021-12-31 20:34”) *#{EL}
  • A2: To use a time zone other than that of the Workflow Platform, set the offset hours (eg”-8″)#{EL}
  • B1: Select the STRING type Data that stores the ISO Datetime with offset (update) *
  • B2: To specify the offset, set the offset hours (eg”+1″)#{EL}
  • C1: Select the STRING type Data that stores the ISO Datetime in UTC (update)
  • D: Select the NUMERIC type Data that stores the timestamp (in seconds) (update)
Script (click to open)
// GraalJS Script (engine type: 2)

//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const strWorkflowDatetime = configs.get( "StrConfA1" ); // required
  if( strWorkflowDatetime === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: Workflow Datetime data} is empty \n" );
  }
const strWorkflowTz       = configs.get( "StrConfA2" ); // hour
  let numWorkflowTz       = engine.getTimeZoneOffsetInMinutes() /60;
  if( strWorkflowTz     !== "" ){
      numWorkflowTz       = parseFloat( strWorkflowTz );
  }
const strPocketIsoOffset  = configs.getObject( "SelectConfB1" );
const strIsoOffsetTz      = configs.get( "StrConfB2" ); // hour
  let numIsoOffsetTz      = engine.getTimeZoneOffsetInMinutes() /60;
  if( strIsoOffsetTz    !== "" ){
      numIsoOffsetTz      = parseFloat( strIsoOffsetTz );
  }
  let strOffset           = "";
  let numAbsIsoOffsetTz   = numIsoOffsetTz;
  if( numIsoOffsetTz < 0){
      strOffset          += "-";
      numAbsIsoOffsetTz   = 0 - numIsoOffsetTz;
  }else{
      strOffset          += "+";
  }
  if( numAbsIsoOffsetTz < 10){
      strOffset          += "0";
  }
  strOffset              += Math.floor( numAbsIsoOffsetTz ) + ":";
  if( numAbsIsoOffsetTz * 60 % 60 < 10 ){
      strOffset          += "0";
  }
  strOffset              += ( numAbsIsoOffsetTz * 60 % 60 ) + ""; // eg "+09:00"
engine.log( " AutomatedTask: Workflow Platform offset: " +
            (engine.getTimeZoneOffsetInMinutes() /60) );
engine.log( " AutomatedTask: Workflow Datetime as: " + numWorkflowTz );
const strPocketIsoUtc     = configs.getObject( "SelectConfC1" );
const numPocketTimestamp  = configs.getObject( "SelectConfD" );


//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)


//// == Calculating / 演算 ==
let dateUniversalTmp = toJsDate( strWorkflowDatetime );
let dateUniversal    = new Date( dateUniversalTmp.getTime() +
                                 engine.getTimeZoneOffsetInMinutes() *60 *1000 -
                                 numWorkflowTz *60 *60 *1000 );
let numMsecUniversal = dateUniversal.getTime();
engine.log( " AutomatedTask: Timestamp: " + numMsecUniversal );

// Set time forward/back with timezone
let dateLocaltime    = new Date( dateUniversal.getTime() + numIsoOffsetTz *60 *60 *1000 );
engine.log( " AutomatedTask: Timestamp Pseudo: " + dateLocaltime.getTime() );

// The getUTCDate() etc returns number according to universal time.
let strIsoOffset  = dateLocaltime.getFullYear() + "-";
if( dateLocaltime.getUTCMonth() + 1 < 10 ){
    strIsoOffset += "0" + (dateLocaltime.getUTCMonth() + 1) + "-";
}else{
    strIsoOffset +=       (dateLocaltime.getUTCMonth() + 1) + "-";
}
if( dateLocaltime.getUTCDate() < 10 ){
    strIsoOffset += "0" + dateLocaltime.getUTCDate() + "T";
}else{
    strIsoOffset +=       dateLocaltime.getUTCDate() + "T";
}
if( dateLocaltime.getUTCHours() < 10 ){
    strIsoOffset  += "0" + dateLocaltime.getUTCHours() + ":";
}else{
    strIsoOffset  +=       dateLocaltime.getUTCHours() + ":";
}
if( dateLocaltime.getUTCMinutes() < 10 ){
    strIsoOffset  += "0" + dateLocaltime.getUTCMinutes() + ":";
}else{
    strIsoOffset  +=       dateLocaltime.getUTCMinutes() + ":";
}
if( dateLocaltime.getUTCSeconds() < 10 ){
    strIsoOffset  += "0" + dateLocaltime.getUTCSeconds() + "";
}else{
    strIsoOffset  +=       dateLocaltime.getUTCSeconds() + "";
}
strIsoOffset      += strOffset;

let strIsoUtc     = dateUniversal.getFullYear() + "-";
if( dateUniversal.getUTCMonth() + 1 < 10 ){
    strIsoUtc    += "0" + (dateUniversal.getUTCMonth() + 1) + "-";
}else{
    strIsoUtc    +=       (dateUniversal.getUTCMonth() + 1) + "-";
}
if( dateUniversal.getUTCDate() < 10 ){
    strIsoUtc    += "0" + dateUniversal.getUTCDate() + "T";
}else{
    strIsoUtc    +=       dateUniversal.getUTCDate() + "T";
}
if( dateUniversal.getUTCHours() < 10 ){
    strIsoUtc     += "0" + dateUniversal.getUTCHours() + ":";
}else{
    strIsoUtc     +=       dateUniversal.getUTCHours() + ":";
}
if( dateUniversal.getUTCMinutes() < 10 ){
    strIsoUtc     += "0" + dateUniversal.getUTCMinutes() + ":";
}else{
    strIsoUtc     +=       dateUniversal.getUTCMinutes() + ":";
}
if( dateUniversal.getUTCSeconds() < 10 ){ // always "0"
    strIsoUtc     += "0" + dateUniversal.getUTCSeconds() + "";
}else{
    strIsoUtc     +=       dateUniversal.getUTCSeconds() + "";
}
strIsoUtc         += "Z";

//// == Data Updating / ワークフローデータへの代入 ==
if( strPocketIsoOffset !== null ){
  engine.setData(strPocketIsoOffset,   strIsoOffset );
}
if( strPocketIsoUtc    !== null ){
  engine.setData(strPocketIsoUtc,      strIsoUtc );
}
if( numPocketTimestamp !== null ){
  engine.setData( numPocketTimestamp,  new java.math.BigDecimal( numMsecUniversal /1000 ) );
} // Millisec will exceed the numerical limit of workflow platform


} //////// END "main()" /////////////////////////////////////////////////////////////////



function toJsDate( bpmsDateOrDatetimeStr ){
  // BPMS Date:     "2020-04-01"  (subtype "Y/M" "M/D" "Y", not supported)
  // BPMS Datetime: "2020-04-01 23:59"
  let year       = 0;
  let monthIndex = 0;
  let day        = 0;
  let hours      = 0;
  let minutes    = 0;

  //  The ECMA/JavaScript Date object has a large number of methods.
  // "Date.parse" is danger (strongly discouraged)
  // - new Date("2014-11-10") // Mon Nov 10 2014 09:00:00 GMT+0900 (JST)
  // - new Date(2014, 10, 10) // Mon Nov 10 2014 00:00:00 GMT+0900 (JST)
  let arrDatetime = bpmsDateOrDatetimeStr.split(" ");
  if( arrDatetime.length === 1 ){
    let arrDateParts = arrDatetime[0].split("-");
    year       = parseInt(arrDateParts[0], 10);
    monthIndex = parseInt(arrDateParts[1], 10) - 1;
    day        = parseInt(arrDateParts[2], 10);
  }
  if( arrDatetime.length === 2 ){
    let arrDateParts = arrDatetime[0].split("-");
    let arrTimeParts = arrDatetime[1].split(":");
    year       = parseInt(arrDateParts[0], 10);
    monthIndex = parseInt(arrDateParts[1], 10) - 1;
    day        = parseInt(arrDateParts[2], 10);
    hours      = parseInt(arrTimeParts[0], 10);
    minutes    = parseInt(arrTimeParts[1], 10);
  }
  return new Date( year, monthIndex, day, hours, minutes );
}



/*
Notes:
- ISO Datetime strings for API Webhook etc can be automatically generated.
- You will be able to manage the time with timezone info as business data.
- ISO Datetime with Offset (YYYY-MM-DDThh:mm:ss+09:00)
    - eg "2021-12-31T20:34:00+00:00"
    - eg "2022-01-01T05:34:00+09:00"
    - eg "2021-12-31T12:34:00-08:00"
- ISO Datetime in UTC (YYYY-MM-DDThh:mm:ssZ)
    - eg "2021-12-31T20:34:00Z"

Notes-ja:
- ISO Datetime 文字列(API 通信や Webhook 受信等で利用)が自動生成されます。
- 業務データとして「タイムゾーン情報をもった時刻」をテキスト管理できるようになります。
- ISO日時文字列(オフセット表記)
    - eg "2021-12-31T20:34:00+00:00"
    - eg "2022-01-01T05:34:00+09:00"
    - eg "2021-12-31T12:34:00-08:00"
- ISO日時文字列(UTC時刻表記)
    - eg "2021-12-31T20:34:00Z"
*/


/*
APPENDIX
- "Date type + static hh:mm" is also acceptable. (processes the string "YYYY-MM-DD hh:mm")
- Wikipedia "ISO 8601"
    - https://en.wikipedia.org/wiki/ISO_8601
- The timestamp obtained by this Addon is Unix epoch.
    - Seconds since 1 January 1970 UTC
    - Not the same as the ECMAScript epoch (elapsed Milliseconds), Unix is 1/1000 of ECMAScript

APPENDIX-ja
- "日付型データ+固定時刻文字列" による指定も可能です。("YYYY-MM-DD hh:mm" の文字列を処理します)
- ウィキペディア "ISO 8601"
    - https://ja.wikipedia.org/wiki/ISO_8601
- このAddonによって取得されるタイムスタンプは Unix epoch ("経過秒数")です。
    - 協定世界時 (UTC) の1970年1月1日からの経過秒数
    - Unix epoch("経過秒数")は ECMAScript epoch("経過ミリ秒数")の1000分の1となります
*/


Download

2021-03-01 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/converter-workflow-datetime-to-iso-datetime-string/
The Addon-import feature is available with Professional edition.

Notes

  • ISO Datetime strings for API Webhooks, etc. can be automatically generated
  • You will be able to manage the time with time zone info as text in your business data
  • ISO Datetime with offset (YYYY-MM-DDThh:mm:ss+09:00)
    • e.g. 2021-12-31T20:34:00+00:00
    • e.g. 2022-01-01T05:34:00+09:00
    • e.g. 2021-12-31T12:34:00-08:00
  • ISO Datetime in UTC (YYYY-MM-DDThh:mm:ssZ)
    • e.g. 2021-12-31T20:34:00Z

Capture

Converts Datetime type data ("2021-12-31 20:34" JST) to ISO 8601 datetime string data ("2021-12-31T20:34:00+09:00" or "2021-12-31T11:34:00Z"). As a general rule, Datetime type data is regarded as the time of Workflow Platform timezone.

Appendix

  • “Date type + static hh:mm” is also acceptable (processes the string “YYYY-MM-DD hh:mm”).
  • Wikipedia “ISO 8601”
  • The timestamp obtained by this Addon is Unix epoch.
    • Seconds since 1 January 1970 UTC
    • Unix epoch (elapsed seconds) is 1/1000 of the ECMAScript epoch (elapsed Milliseconds)

See also

Converter: ISO-Datetime-String to Workflow Datetime

1 thought on “Converter: Workflow Datetime to ISO-Datetime-String”

  1. Pingback: Converter: Datetimes in difference timezones – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top