Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment Timezone - Detect if given time is ambiguous

Tags:

momentjs

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 2011-11-06 01:00 can be either Central Daylight Time (CDT) or Savings Time (CST).

like image 839
Abhijit Rao Avatar asked Oct 17 '22 16:10

Abhijit Rao


1 Answers

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.

like image 85
Matt Johnson-Pint Avatar answered Oct 21 '22 03:10

Matt Johnson-Pint