Skip to content

Fix observation events times with local time dates#105

Merged
rhannequin merged 1 commit intomainfrom
fix-wrong-rising-setting-local-date
Nov 22, 2024
Merged

Fix observation events times with local time dates#105
rhannequin merged 1 commit intomainfrom
fix-wrong-rising-setting-local-date

Conversation

@rhannequin
Copy link
Owner

@rhannequin rhannequin commented Nov 8, 2024

Before, times from Astronoby::Events::ObservationEvents could produce wrong results when the location was far away from the Greenwich meridian.

The reason was a bug when converting times (hour+minute+second) into Time objects. The date was correctly added by the time was in UTC. When converting back the UTC time to local time, the date could change.

Now, the date is added depending on the time with concern of the user's UTC offset.

The offset must be provided hen instantiating the Astronoby::Observer object using a new attribute: #utc_offset.

utc_offset = "-05:00"
time = Time.new(2015, 2, 5, 0, 0, 0, utc_offset)
observer = Astronoby::Observer.new(
  latitude: Astronoby::Angle.from_degrees(38),
  longitude: Astronoby::Angle.from_degrees(-78),
  utc_offset: utc_offset
)
sun = Astronoby::Sun.new(time: time)
observation_events = sun.observation_events(observer: observer)

observation_events.rising_time.getlocal(utc_offset)
 # => 2015-02-05 07:12:59 -0500

observation_events.transit_time.getlocal(utc_offset)
 # => 2015-02-05 12:25:59 -0500

observation_events.setting_time.getlocal(utc_offset)
 # => 2015-02-05 17:39:27 -0500

Fixes #97

Before, times from `Astronoby::Events::ObservationEvents` could produce
wrong results when the location was far away from the Greenwich
meridian.

The reason was a bug when converting times (hour+minute+second) into
`Time` objects. The date was correctly added by the time was in UTC.
When converting back the UTC time to local time, the date could change.

Now, the date is added depending on the time with concern of the user's
UTC offset.

The offset must be provided hen instantiating the `Astronoby::Observer`
object using a new attribute: `#utc_offset`.

```rb
utc_offset = "-05:00"
time = Time.new(2015, 2, 5, 0, 0, 0, utc_offset)
observer = Astronoby::Observer.new(
  latitude: Astronoby::Angle.from_degrees(38),
  longitude: Astronoby::Angle.from_degrees(-78),
  utc_offset: utc_offset
)
sun = Astronoby::Sun.new(time: time)
observation_events = sun.observation_events(observer: observer)

observation_events.rising_time.getlocal(utc_offset)
 # => 2015-02-05 07:12:59 -0500

observation_events.transit_time.getlocal(utc_offset)
 # => 2015-02-05 12:25:59 -0500

observation_events.setting_time.getlocal(utc_offset)
 # => 2015-02-05 17:39:27 -0500
```
@rhannequin
Copy link
Owner Author

rhannequin commented Nov 22, 2024

Here are some tests to compare results over several days, from different sources, for different locations. The point is to notice a significant difference that would make believe that the fix is actually a bug taking the data for the next or previous day.

  • 2024-11-23 41.87,-87.62 UTC-06:00

    • Astronoby n-1: 06:49 - 16:24
    • Astronoby n: 06:50 - 16:24
    • Astronoby n+1: 06:51 - 16:23
    • Astronoby n [fixed]: 06:50 - 16:24
    • IMCCE n-1: 06:50 - 16:23
    • IMCCE n: 06:52 - 16:22
    • IMCCE n+1: 06:53 - 16:22
    • OpenWeather: 06:50 - 16:24
  • 2024-09-11 41.87,-87.62 UTC-06:00

    • Astronoby n-1: 05:26 - 18:09 (previous day)
    • Astronoby n: 05:27 - 18:08 (previous day)
    • Astronoby n+1: 05:28 - 18:06 (previous day)
    • Astronoby n [fixed]: 05:27 - 18:08
    • IMCCE n-1: 05:27 - 18:06
    • IMCCE: 05:28 - 18:05
    • IMCCE n+1: 05:29 - 18:03
  • 2000-01-02 -33.8688,151.2093 UTC+11:00

    • Astronoby n-1: 05:48 (next day) - 20:09
    • Astronoby n: 05:48 (next day) - 20:10
    • Astronoby n+1: 05:49 (next day) - 20:10
    • Astronoby n [fixed]: 05:48 - 20:10
    • OpenWeather: 05:49 - 20:09

The test don't show a significant error. This makes me think the fix is correct.

@rhannequin rhannequin marked this pull request as ready for review November 22, 2024 18:04
@rhannequin rhannequin merged commit e76815f into main Nov 22, 2024
@rhannequin rhannequin deleted the fix-wrong-rising-setting-local-date branch November 22, 2024 18:05
@trevorturk
Copy link

🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible incorrect date for Astronoby::Sun.observation_events.setting_time

2 participants