Skip to content

Figure.solar: Fix the bug that terminator datetime doesn't support fractional timezone offsets - #4916

Open
seisman wants to merge 5 commits into
mainfrom
solar/timzone
Open

seisman wants to merge 5 commits into
mainfrom
solar/timzone

Conversation

@seisman

@seisman seisman commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Description of proposed changes

As reported in #4112 amd GenericMappingTools/gmt#9220, GMT's solar module doesn't accept fractional timezone offsets like 08:30. GMT always casts it to 8.

GMT already has a fix at GenericMappingTools/gmt#9221. This PR adds a workaround on the PyGMT side, by always converting timezone-aware datetime to UTC datetime before passing to GMT. With this workaround, PyGMT works well for any GMT versions.

This bug can be confirmed by the script below:

import pygmt
import datetime

fig = pygmt.Figure()
fig.basemap(region="d", projection="W0/15c", frame=True)
fig.solar(terminator_datetime="2020-01-01T01:02:03", pen="1p,black")
fig.solar(terminator_datetime="2020-01-01T01:02:03+00:30", pen="1p,yellow,-")
fig.solar(terminator_datetime="2020-01-01T01:02:03+01:00", pen="1p,red")
fig.solar(terminator_datetime="2020-01-01T01:02:03-01:00", pen="1p,blue")
fig.solar(
    terminator_datetime=datetime.datetime(
        2020, 1, 1, 1, 2, 3, tzinfo=datetime.timezone(datetime.timedelta(hours=2))
    ),
    pen="1p,lightred",
)
fig.solar(
    terminator_datetime=datetime.datetime(
        2020, 1, 1, 1, 2, 3, tzinfo=datetime.timezone(datetime.timedelta(hours=-2))
    ),
    pen="1p,lightblue",
)
fig.show()

Note the position of the yellow dashed line for a timezone offset +00:30.

Current main branch This branch
map map

Patches #4112.

@seisman seisman added this to the 0.20.0 milestone Sep 23, 2026
@seisman seisman added bug Something isn't working needs review This PR has higher priority and needs review. labels Sep 23, 2026
Comment thread pygmt/src/solar.py
Comment on lines +120 to +122
# Convert a timezone-aware datetime to UTC, before passing to GMT.
if _datetime.tzinfo is not None:
_datetime = _datetime.tz_convert("UTC")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this workaround, we don't need the +z modifier (line 143), so this workaround can simplify the code and is not temporary. So I don't add a # TODO comment to remove it in future versions.

@seisman
seisman requested a review from a team September 24, 2026 03:40
Comment thread pygmt/tests/test_solar.py

This branch has not been deployed

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

Labels

bug Something isn't working needs review This PR has higher priority and needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants