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

KDB夏令时判定逻辑咨询及跨时区时间转换函数问询

Hey there, let's tackle your two kdb+ questions clearly and practically:

1. Daylight Saving Time (DST) Detection Logic in kdb+

kdb+ handles time zones natively with datetimeTZ types, and there are two straightforward ways to check if a given time falls in DST:

  • Built-in timezone offset check: For regions like Vancouver (Pacific Time, PST/PDT), standard time uses a UTC-8 offset, while DST uses UTC-7. If you have a datetimeTZ value tied to Vancouver, you can extract its offset and compare:

    // Function to check if a Vancouver time is in DST
    isDst: {[t] -7h = `timespan$"z"$t}
    isDst[2024.06.01T12:00:00.000-07:00] // Returns 1 (true, DST active)
    isDst[2024.12.01T12:00:00.000-08:00] // Returns 0 (false, standard time)
    
  • Date range-based validation: If you're working with UTC times or need to manually validate DST windows, you can use Pacific Time's standard rules (second Sunday in March to first Sunday in November, switching at 2AM local time). Here's a function to check a UTC time against this window:

    pacificDstCheck: {[utcDt]
        y: year utcDt;
        // DST start: second Sunday in March, 2AM PST = 10AM UTC
        start: `datetime$[y].03.(10 + 7 - weekday y.03.01)T10:00:00;
        // DST end: first Sunday in November, 2AM PDT = 9AM UTC
        end: `datetime$[y].11.(7 - weekday y.11.01)T09:00:00;
        start <= utcDt < end
    }
    pacificDstCheck[2024.06.01T00:00:00.000] // Returns 1 (true)
    pacificDstCheck[2024.12.01T00:00:00.000] // Returns 0 (false)
    

2. Converting Indian Time to Vancouver Time with DST Handling

You don't need to manually code DST logic here—kdb+'s built-in timezone conversion automatically applies DST rules when using region-based timezone identifiers.

Indian Standard Time (IST) is UTC+5:30, and Vancouver uses the America/Vancouver timezone identifier, which automatically shifts between PST and PDT. Here's a reusable function for the conversion:

// Convert IST datetime (either datetimeTZ or plain datetime) to Vancouver time
istToVancouver: {[istDt]
    // Attach IST offset if input is a plain datetime (no timezone data)
    if[`datetime = type istDt; istDt: istDt + 0D0+05:30];
    // Convert to Vancouver timezone, auto-handles DST shifts
    `datetime$"P/America/Vancouver"$istDt
}

Test it with your scenario:

For a summer IST time (when Vancouver is in DST):

istSummer: 2024.06.01T11:00:00.000+05:30
istToVancouver[istSummer] // Returns 2024.06.01T03:30:00.000-07:00 (3:30 AM)

For a winter IST time (Vancouver in standard time):

istWinter: 2024.12.01T10:00:00.000+05:30
istToVancouver[istWinter] // Returns 2024.12.01T02:30:00.000-08:00 (2:30 AM)

This matches the behavior you described—automatic adjustment between 2:30 AM and 3:30 AM based on whether DST is active.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:12:04