getAntimeridianAdjustment method

int getAntimeridianAdjustment()

Adjust the date for antimeridian crossover. This is needed to deal with edge cases such as Samoa that use a different calendar date than expected based on their geographic location.

The actual Time Zone offset may deviate from the expected offset based on the longitude. Since the 'absolute time' calculations are always based on longitudinal offset from UTC for a given date, the date is presumed to only increase East of the Prime Meridian, and to only decrease West of it. For Time Zones that cross the antimeridian, the date will be artificially adjusted before calculation to conform with this presumption.

For example, Apia, Samoa with a longitude of -171.75 uses a local offset of +14:00. When calculating sunrise for 2018-02-03, the calculator should operate using 2018-02-02 since the expected zone is -11. After determining the UTC time, the local DST offset of UTC+14:00 should be applied to bring the date back to 2018-02-03.

@return the number of days to adjust the date This will typically be 0 unless the date crosses the antimeridian

Implementation

int getAntimeridianAdjustment() {
  double localHoursOffset = getLocalMeanTimeOffset() / _HOUR_MILLIS;

  if (localHoursOffset >= 20) {
    // if the offset is 20 hours or more in the future (never expected anywhere other
    // than a location using a timezone across the anti meridian to the east such as Samoa)
    return 1; // roll the date forward a day
  } else if (localHoursOffset <= -20) {
    // if the offset is 20 hours or more in the past (no current location is known
    //that crosses the antimeridian to the west, but better safe than sorry)
    return -1; // roll the date back a day
  }
  return 0; //99.999% of the world will have no adjustment
}