Phalgun
Phalgun

Reputation: 1494

Timezone of the date returned by Date() constructor in chromium browser

As per MDN, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

It's important to keep in mind that while the time value at the heart of a Date object is UTC, the basic methods to fetch the date and time or its components all work in the local (i.e. host system) time zone and offset.

How does chromium browser determine the local time zone and offset?

Upvotes: 0

Views: 71

Answers (2)

Jared Smith
Jared Smith

Reputation: 21926

The browser queries the operating system (whichever one you are using) to determine the user's timezone. For instance on the Mac I'm typing this answer on:

#if V8_OS_DARWIN
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <pthread.h>
#endif

#if V8_OS_DARWIN
int64_t ComputeThreadTicks() {
  mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
  thread_basic_info_data_t thread_info_data;
  kern_return_t kr = thread_info(
      pthread_mach_thread_np(pthread_self()),
      THREAD_BASIC_INFO,
      reinterpret_cast<thread_info_t>(&thread_info_data),
      &thread_info_count);
  CHECK_EQ(kr, KERN_SUCCESS);

  // We can add the seconds into a {int64_t} without overflow.
  CHECK_LE(thread_info_data.user_time.seconds,
           std::numeric_limits<int64_t>::max() -
               thread_info_data.system_time.seconds);
  int64_t seconds =
      thread_info_data.user_time.seconds + thread_info_data.system_time.seconds;
  // Multiplying the seconds by {kMicrosecondsPerSecond}, and adding something
  // in [0, 2 * kMicrosecondsPerSecond) must result in a valid {int64_t}.
  static constexpr int64_t kSecondsLimit =
      (std::numeric_limits<int64_t>::max() /
       v8::base::Time::kMicrosecondsPerSecond) -
      2;
  CHECK_GT(kSecondsLimit, seconds);
  int64_t micros = seconds * v8::base::Time::kMicrosecondsPerSecond;
  micros += (thread_info_data.user_time.microseconds +
             thread_info_data.system_time.microseconds);
  return micros;
}

You can see here where chromium queries the (again OS-specific) system time.

Source

If you look at the linked file in the codebase you can see where it has ifdefs for all the various supported OS flavors: win32, POSIX, Fuschia, etc.

Upvotes: 1

CesareIsHere
CesareIsHere

Reputation: 233

The timezone is given by the user device (OS). You can find this information in docs(you linked it in your question): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_format_and_time_zone_conversions

Upvotes: 2

Related Questions