You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将microsoft.graph.customTimeZone转换为TimeZoneInfo

Microsoft Graph customTimeZone 转 TimeZoneInfo 实现方案

目前没有官方提供的可直接完成该转换的内置函数或API,需要手动根据两个类型的字段映射关系实现转换逻辑。

字段映射关系

  • Graph customTimeZone.bias:时区相对于UTC的基础偏移(单位:分钟),对应TimeZoneInfo的基础UTC偏移
  • Graph customTimeZone.standardOffset:标准时间切换规则,对应TimeZoneInfo的标准时间调整规则
    • time:切换生效的当日时间
    • month:切换的月份
    • dayOfWeek:切换的星期几
    • dayOccurrence:当月的第几个星期几(1=第一个,2=第二个,5=最后一个)
    • year:生效年份,0代表每年生效
  • Graph customTimeZone.daylightOffset:夏令时切换规则
    • daylightBias:夏令时相对于基础偏移的调整值(单位:分钟,通常为-60表示夏令时比标准时间早1小时)
    • 其余字段含义同standardOffset

代码实现示例(C#)

using System;
using Microsoft.Graph; // 需引入Graph SDK包

public static TimeZoneInfo ConvertCustomTimeZoneToTimeZoneInfo(CustomTimeZone graphTimeZone)
{
    // 基础UTC偏移:可根据实际返回数据的bias定义调整正负符号
    var baseUtcOffset = TimeSpan.FromMinutes(graphTimeZone.Bias.Value);
    
    // 构造标准时间调整规则
    var standardTransitionTime = TransitionTime.CreateFloatingDateRule(
        timeOfDay: TimeSpan.Parse(graphTimeZone.StandardOffset.Time),
        month: graphTimeZone.StandardOffset.Month.Value,
        week: graphTimeZone.StandardOffset.DayOccurrence.Value,
        dayOfWeek: (DayOfWeek)Enum.Parse(typeof(DayOfWeek), graphTimeZone.StandardOffset.DayOfWeek.ToString(), true)
    );
    
    // 构造夏令时调整规则
    var daylightTransitionTime = TransitionTime.CreateFloatingDateRule(
        timeOfDay: TimeSpan.Parse(graphTimeZone.DaylightOffset.Time),
        month: graphTimeZone.DaylightOffset.Month.Value,
        week: graphTimeZone.DaylightOffset.DayOccurrence.Value,
        dayOfWeek: (DayOfWeek)Enum.Parse(typeof(DayOfWeek), graphTimeZone.DaylightOffset.DayOfWeek.ToString(), true)
    );
    
    // 夏令时偏移调整
    var daylightDelta = TimeSpan.FromMinutes(graphTimeZone.DaylightOffset.DaylightBias.Value);
    
    // 构造调整规则,适配year=0的全年生效场景
    var adjustmentRule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(
        dateStart: DateTime.MinValue.Date,
        dateEnd: DateTime.MaxValue.Date,
        daylightDelta: daylightDelta,
        daylightTransitionStart: daylightTransitionTime,
        daylightTransitionEnd: standardTransitionTime
    );
    
    // 创建自定义时区
    return TimeZoneInfo.CreateCustomTimeZone(
        id: graphTimeZone.Name,
        baseUtcOffset: baseUtcOffset,
        displayName: graphTimeZone.Name,
        standardDisplayName: graphTimeZone.Name,
        daylightDisplayName: $"{graphTimeZone.Name} (夏令时)",
        adjustmentRules: new[] { adjustmentRule }
    );
}

注意事项

  • 上述代码仅适配year=0(每年循环生效)的规则,若有指定年份的特殊规则需要额外扩展判断逻辑
  • 若时区不支持夏令时,可跳过调整规则构造,直接使用TimeZoneInfo.CreateCustomTimeZone的无调整规则重载即可
  • 注意枚举值的大小写兼容处理,避免Graph返回的星期几字符串大小写不匹配导致解析失败
  • 若转换后时区偏移与预期不符,可调整baseUtcOffset的正负符号,不同场景下bias的定义可能存在UTC减本地、本地减UTC两种差异,可根据实际业务返回数据验证后调整

内容的提问来源于stack exchange,提问作者CloudAnywhere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 02:27:05