-2

i got the same result (the correct time for Latvia) when using the following EcmaScript6 Source

in 0 A.D.

In the case of 0 A.D. that is used here, the game's source code include JavaScript code written using ES6 syntax. This code would be executed by a JavaScript engine that is bundled with the game's runtime environment. BTW there is no support for console.log()

const formatTime = (date, timeZone) => {
  const options = {
    hour: "numeric",
    minute: "numeric",
    hour12: true,
    timeZone: timeZone,
  };
  return date.toLocaleTimeString("en-US", options);
};

const getNextHalfHour = (inNextFullMinute) => {
  const now = new Date();
  const minutes = now.getMinutes();

  if (!inNextFullMinute && isNaN(inNextFullMinute)) inNextFullMinute = 30;
  else inNextFullMinute = parseInt(inNextFullMinute);

  const roundedMinutes =
    Math.ceil(minutes / inNextFullMinute) * inNextFullMinute;
  const nextHalfHour = new Date(
    now.getFullYear(),
    now.getMonth(),
    now.getDate(),
    now.getHours(),
    roundedMinutes,
    0,
  );
  if (roundedMinutes === 60) {
    nextHalfHour.setHours(now.getHours() + 1);
    nextHalfHour.setMinutes(0);
  }
  return nextHalfHour;
};

const nextHalfHour = getNextHalfHour();
console.log(
  "Europe/London:",
  formatTime(
    new Date(nextHalfHour.getTime() + 1 * 60 * 60 * 1000),
    "Europe/London",
  ),
);

console.log(
  "America/Los_Angeles:",
  formatTime(
    new Date(nextHalfHour.getTime() + 1 * 60 * 60 * 1000),
    "America/Los_Angeles",
  ),
);

These two time zones (Europe/London, America/Los_Angeles) have a significant time difference.

If i write FunPlanat/moon instead America/Los_Angeles i still get the same result and no errors.

i wonder that i cannot provocate an error here and that the results always the same correct result for Lativa.

The formatTime function in ES6 is a custom function that takes a Date object and a time zone identifier as inputs, and returns a formatted string representation of the time in the specified time zone.

If this source correct? Or how i could fix that if not?

i was reading here about formatTime function:

This is the complete source snippet that i using in https://github.com/sl5net/autocivP/commit/dd65aafff2c95d60014da8423a192cdcf68f4c70

formatTime(new Date('2023-07-08'), 'Asia/Kolkata');
// gives same result like formatTime(new Date('2023-07-08'), 'moonShip');
const tBerlinLondonSwedenDenmark = formatTime(nextHalfHour, "Europe/London");

// const tSweden = formatTime(new Date(nextHalfHour.getTime() + (1 * 60 * 60 * 1000)), 'Europe/Stockholm');
const Latvia = formatTime(
  new Date(nextHalfHour.getTime() + 1 * 60 * 60 * 1000),
  "Europe/London",
);
const RioGrandeDoSulBrasilien = formatTime(
  new Date(nextHalfHour.getTime() - 5 * 60 * 60 * 1000),
  "Europe/London",
);
const tGreece = formatTime(
  new Date(nextHalfHour.getTime() + 1 * 60 * 60 * 1000),
  "Europe/Athens",
);

const Asia_Kolkata = formatTime(
  new Date(nextHalfHour.getTime() + 3.5 * 60 * 60 * 1000),
  "Asia/Kolkata",
);
const USA_ET = formatTime(
  new Date(nextHalfHour.getTime() - 6 * 60 * 60 * 1000),
  "America/New_York",
);
const USA_Los_Angeles = formatTime(
  new Date(nextHalfHour.getTime() - 9 * 60 * 60 * 1000),
  "America/Los_Angeles",
);
const USA_Chicago = formatTime(
  new Date(nextHalfHour.getTime() - 7 * 60 * 60 * 1000),
  "America/Los_Angeles",
);
SL5net
  • 2,282
  • 4
  • 28
  • 44
  • @Phil i will add it. BTW The formatTime function in ES6 is a custom function that takes a Date object and a time zone identifier as inputs, and returns a formatted string representation of the time in the specified time zone. – SL5net Jul 26 '23 at 04:08
  • It clearly gives different results here ~ https://jsfiddle.net/oxhyrjp2/. This is **not reproducible** – Phil Jul 26 '23 at 04:23
  • @Phil but you don't use ES6 in your example – SL5net Jul 26 '23 at 04:27
  • Also, attempting to use `moonShip` throws an error... _"RangeError: invalid time zone in DateTimeFormat(): moonShip"_ – Phil Jul 26 '23 at 04:27
  • 1
    Do you even know what ES6 means? It's a language level and has nothing to do with running code – Phil Jul 26 '23 at 04:29
  • In the case of 0 A.D. that is used here, the game's source code include JavaScript code written using ES6 syntax. This code would be executed by a JavaScript engine that is bundled with the game's runtime environment. – SL5net Jul 26 '23 at 04:36
  • 1
    Stack Overflow is not here to debug broken JavaScript runtime environments. I suggest you raise a bug with the maintainers of whatever _"0 A.D."_ is ~ https://play0ad.com/community/support/ – Phil Jul 26 '23 at 04:38
  • 1
    "ES6" is jargon referring to [*ECMAScript 6th edition*](https://262.ecma-international.org/6.0/), which is more correctly named ECMAScript 2015. To say "using ES6" means using language features consistent with ECMAScript 2015. It made few if any changes to Date objects or related features. The results of *toLocaleString* are based on an entirely different specification, [*ECMA-402*](https://www.ecma-international.org/publications-and-standards/standards/ecma-402/). – RobG Jul 26 '23 at 04:41
  • 1
    BTW, `!inNextFullMinute && isNaN(inNextFullMinute)` will only ever be true if *inNextFullMinute* is `NaN`, so it's identically equivalent to `isNaN(inNextFullMinute)`. – RobG Jul 26 '23 at 08:00

1 Answers1

-3

The FunPlanat/moon timezone is a fictional timezone used for the example. In real-world usage, you should replace it with the appropriate timezone identifier. You can find the valid timezone identifiers in the IANA Time Zone Database (https://www.iana.org/time-zones)

Write the reusable function with the parameter timezone

const formatTime = (timezone) => {
  const options = {
    timeZone: timezone,
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric',
  };

  const formatter = new Intl.DateTimeFormat(undefined, options);
  const currentTime = new Date();

  return formatter.format(currentTime);
};


const asiaKolkataTime = formatTime('Asia/Kolkata');
const americaNewYorkTime = formatTime('America/New_York');

console.log('Asia/Kolkata Time:', asiaKolkataTime);
console.log('America/New_York Time:', americaNewYorkTime);
  • 1
    Did ChatGPT write this or something? This might format the current time according to the provided time zone identifier but OP is passing in a `Date` instance – Phil Jul 26 '23 at 04:16
  • Yes, ChatGPT. Is it not helpful? – Mohamed Ramadan Jul 27 '23 at 05:18
  • 1
    Not only is it incorrect, it is specifically [**not allowed on Stack Overflow**](https://stackoverflow.com/help/gpt-policy) – Phil Jul 27 '23 at 05:54