Converter: Datetimes in different time zones

Converter: Datetimes in difference timezones
Converts the datetime in time zone A to the datetime in time zone B. Any time zone can be set for either datetimes. If nothing is set the Workflow Platform time zone will be applied.
Configs
  • A1: Set Datetime data (eg “2021-12-31 20:34”) *#{EL}
  • A2: To consider as time other than Platform, Set Offset (eg”-8″)#{EL}
  • B1: Select DATETIME that stores Converted Datetime (update) *
  • B2: Set Offset Hour for Converted Datetime (eg “+1” “9.5”)#{EL}
  • C: Select NUMERIC that stores Timestamp sec (update)
Script (click to open)
// GraalJS Script (engine type: 2)

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

//// == Config Retrieving / 工程コンフィグの参照 ==
const strInputDt         = configs.get( "StrConfA1" ); // required
  if( strInputDt       === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: Input Datetime data} is empty \n" );
  }
const strInputTz         = configs.get( "StrConfA2" ); // hour
  let numInputTz         = engine.getTimeZoneOffsetInMinutes() /60;
  if( strInputTz       !== "" ){
      numInputTz         = parseFloat( strInputTz );
  }
const dtPocketOutput     = configs.getObject( "SelectConfB1" );
const strOutputTz        = configs.get( "StrConfB2" ); // hour
  let numOutputTz        = engine.getTimeZoneOffsetInMinutes() /60;
  if( strOutputTz      !== "" ){
      numOutputTz        = parseFloat( strOutputTz );
  }
engine.log( " AutomatedTask: Workflow Platform offset: " +
            (engine.getTimeZoneOffsetInMinutes() /60) );
engine.log( " AutomatedTask: Input Datetime as: " + numInputTz );
engine.log( " AutomatedTask: Output Datetime as: " + numOutputTz );
const numPocketTimestamp = configs.getObject( "SelectConfC" );


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


//// == Calculating / 演算 ==
let dateUniversalTmp = toJsDate( strInputDt );
                                 // DATE Data in Platform time
let dateUniversal    = new Date( dateUniversalTmp.getTime() +
                                 engine.getTimeZoneOffsetInMinutes() *60 *1000 -
                                 numInputTz *60 *60 *1000 );
                                 // Corrects the difference from the Platform time
let numMsecUniversal = dateUniversal.getTime();
engine.log( " AutomatedTask: Timestamp: " + numMsecUniversal );
let dateShifted      = new Date( dateUniversal.getTime() -
                                 engine.getTimeZoneOffsetInMinutes() *60 *1000 +
                                 numOutputTz *60 *60 *1000 );
                                 // Shifts the time forward/back with timezone

//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( dtPocketOutput,      new java.sql.Timestamp( dateShifted.getTime() ) );
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:
- Convert datetime data using two time zone offsets.
    - Offset values are set in Hour. Decimal notation is also possible. (PST:"-8", JST:"+9")
    - https://en.wikipedia.org/wiki/Time_zone
- To customize the notification text according to the recipient's timezone, etc.
- Can be converted even in the standard automation "Update Data".
    - Setting Example of "Output Datetime" (EL syntax)
        - "#q_dtInputDatetime.addHours( #q_numOutputOffset - #q_numInputOffset )"
        - Assumption: Integer Hour is input in Numeric type data
    - M227: Auto Executing Data Binding, Arithmetic Operations
        - https://questetra.zendesk.com/hc/en-us/articles/360002260571-M227
    - R2270: Numeric Output via EL syntax
        - https://questetra.zendesk.com/hc/en-us/articles/360024292572-R2270
    - Update Data
        - https://support.questetra.com/bpmn-icons/service-task-data-assignment/

Notes-ja:
- 2つのタイムゾーンOffset値を用いて、時刻型データを変換します。
    - Offset値はHour単位で設定します。小数表記も可能です。(日本標準時: "+9" 太平洋標準時: "-8")
    - https://ja.wikipedia.org/wiki/%E6%99%82%E9%96%93%E5%B8%AF_(%E6%A8%99%E6%BA%96%E6%99%82)
- 受信者のタイムゾーンにあわせて通知文をカスタマイズしたい場合などに。
- 標準で組み込まれている自動工程[データ更新]でも、時刻の自動変換は可能です。
    - "Output Datetime" の設定例 (EL式)
        - "#q_dtInputDatetime.addHours( #q_numOutputOffset - #q_numInputOffset )"
        - (数値型データに整数Hourが入力される前提)
    - M227: 業務データの結合や四則演算が自動実行されるように設定する
        - https://questetra.zendesk.com/hc/ja/articles/360002260571-M227
    - R2270: EL式による数値としての出力(データ設定式)
        - https://questetra.zendesk.com/hc/ja/articles/360024292572-R2270
    - データ更新
        - https://support.questetra.com/ja/bpmn-icons/service-task-data-assignment/
*/


/*
APPENDIX
- Not only Datetime type data but also process start datetime can be converted.
    - "#{#q_dtInputDatetime}"
    - "#{processInstanceStartDatetime}"
- "Date type + static hh:mm" is also acceptable. (processes the string "YYYY-MM-DD hh:mm")
    - "#{#q_date} 00:00"
- 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
- 日時型データだけでなく、プロセス開始時刻等も変換できます。
    - "#{#q_dtInputDatetime}"
    - "#{processInstanceStartDatetime}"
- "日付型データ+固定時刻文字列" による指定も可能です。("YYYY-MM-DD hh:mm" の文字列を処理します)
    - "#{#q_date} 00:00"
    - "#{processInstanceStartDatetime}"
- このAddonによって取得されるタイムスタンプは Unix epoch ("経過秒数")です。
    - 協定世界時 (UTC) の1970年1月1日からの経過秒数
    - Unix epoch("経過秒数")は ECMAScript epoch("経過ミリ秒数")の1000分の1となります
*/


Download

2021-03-02 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/converter-datetimes-in-difference-timezones/
The Addon-import feature is available with Professional edition.

Notes

Capture

Converts datetime in timezone A to datetime in timezone B. Any timezone can be set for either datetimes. If not set, the Workflow Platform timezone will be applied.

Appendix

  • Not only Datetime type data but also process start datetime can be converted
    • “#{#q_dtInputDatetime}”
    • “#{processInstanceStartDatetime}”
  • “Date type + static hh:mm” is also acceptable (processes the string “YYYY-MM-DD hh:mm”)
    • “#{#q_date} 00:00”
  • 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

2 thoughts on “Converter: Datetimes in different time zones”

  1. Pingback: Converter: Timestamp-Number to Datetime – Questetra Support

  2. Pingback: Google Translate: Translation API Basic, Translate – Questetra Support

Leave a Reply

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

Scroll to Top

Discover more from Questetra Support

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

Continue reading