getSystemTimezone (utils/datetime-utils.ts)

deepbot · src/shared/utils/datetime-utils.ts · typescript · MIT
ui, tag:knn-classified

Utility functions for timezone detection and date/time formatting across locales.

// Source: https://github.com/kevinluosl/deepbot | License: MIT | Copyright 2024 Kevin Luo
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * 日期时间工具
 * 
 * 统一管理时区获取、时间格式化等功能
 */

/**
 * 获取系统时区
 * 
 * @returns 系统时区字符串(如:Asia/Shanghai)
 */
export function getSystemTimezone(): string {
  try {
    return Intl.DateTimeFormat().resolvedOptions().timeZone;
  } catch {
    // 降级到默认时区
    return 'Asia/Shanghai';
  }
}

/**
 * 计算时区偏移信息
 * 
 * @param date 日期对象
 * @returns 时区偏移信息
 */
export function getTimezoneOffset(date: Date = new Date()) {
  const timezoneOffset = date.getTimezoneOffset();
  const offsetHours = Math.floor(Math.abs(timezoneOffset) / 60);
  const offsetMinutes = Math.abs(timezoneOffset) % 60;
  const offsetSign = timezoneOffset <= 0 ? '+' : '-';
  const offsetS

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key