locutus icon indicating copy to clipboard operation
locutus copied to clipboard

How to set timezone in locutus date function?

Open qwe0001 opened this issue 5 years ago • 0 comments

I've read date.js and ini_set.js source code already.

https://github.com/kvz/locutus/blob/a5e9fa0fb8bcfa50ccbd7713df60a64c1122bc0b/src/php/datetime/date.js https://github.com/kvz/locutus/blob/master/src/php/info/ini_set.js

But I couldn't understand how to set timezone cuz I am a yellow monkey u know.

How to set specified timezone?

Temporarily, I set timezone like this.

var PHP = require('locutus/php');

function getNow(myDate)
{
  var now;
  var jpnTime;

  if(! PHP.var.empty(myDate) && PHP.var.is_numeric(myDate)){
    jpnTime = new Date(myDate).toLocaleString("en-US", {timeZone: "Asia/Tokyo"});
  }
  else {
    jpnTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Tokyo"});
  }

  now = PHP.datetime.date('Y-m-d H:i:s', PHP.datetime.strtotime(jpnTime));

  return now;
};

// Run on this in Amazon AWS Lamba@Edge, Region is us-east-1
console.log(new Date()); // timezone is UTC? e.g. "2020-02-14T08:04:00.855Z"
console.log(getNow()); // timezone is Asia/Tokyo correctly e.g. "2020-02-14 17:04:00"

I know that I can set timezone with moment-timezone. But I wish to NOT use any other libraries.

Because Lambda@Edge has a source code capacity limit, And the limit is quite strict.

So I want to avoid using external libraries as much as possible. Any Ideas?

qwe0001 avatar Feb 14 '20 08:02 qwe0001