Moment Timezone - Detect if given time is ambiguous

前端 未结 1 1394
执念已碎
执念已碎 2021-01-19 06:04

Is there a way to check if an epoch is ambiguous or not in momentjs?

In America/Chicago zone, 2011-11-06 00:00 is not ambiguous but

相关标签:
1条回答
  • 2021-01-19 06:15

    I think something like this will work:

    function hasAmbiguousWallTime(m) {
        var t = [60, -60, 30, -30];
        var a = t.map(function(x) { return moment(m).add(x, 'm').format('HH:mm'); });
        return a.indexOf(m.format('HH:mm')) > -1;
    }
    

    Examples:

    hasAmbiguousWallTime(moment.tz("2011-11-06 01:00", "America/Chicago")) // true
    hasAmbiguousWallTime(moment.tz("2011-11-06 00:00", "America/Chicago")) // false
    

    Note that this might fail for transitions that are not either 30 or 60 minutes change in offset, which have occurred historically. A better implementation would test the known transition points in the moment-timezone data, or scan for them against a locally derived moment. That said, the above is sufficient for most modern usage.

    0 讨论(0)
提交回复
热议问题