slashmili / python-jalali

Jalali calendar binding for Python based on Python's datetime module
http://pypi.python.org/pypi/jdatetime/
Other
343 stars 49 forks source link

Bug to convert jalali to gegorian and again convert it to jalali #156

Open vahidtwo opened 6 months ago

vahidtwo commented 6 months ago

Please include your runtime information

Hi i have AJIB issue i try to use jdatetime to convert tehran jalali time to geogorian time (utc) i wtite this code

tehran_timezone = pytz.timezone('Asia/Tehran')
z=jdatetime.datetime(1403, 2, 28, 15, 16, 13, tzinfo=tehran_timezone).togregorian().astimezone(tz=UTC)
jdatetime.datetime.fromgregorian(datetime=z).astimezone(tehran_timezone)
>>> jdatetime.datetime(1403, 2, 28, 15, 20, 13, 0, tzinfo=Asia/Tehran)

as u see the minute convert from 16 to 20 and it is a bug

5j9 commented 6 months ago

I don't use pytz, but this could be a pytz issue because if you run the same code using datetime instead of jdatetime you'll get the same result.

If you can upgrade your Python, try using the zoneinfo module from the standard library, it was added in Python 3.9. It works as expected for me. Sample code:

import datetime
import pytz
from datetime import UTC
from zoneinfo import ZoneInfo

def try_tz(tehran_timezone):
    tehran_dt = datetime.datetime(2024, 2, 28, 15, 16, 13, tzinfo=tehran_timezone)
    print(tehran_dt)
    tehran_to_utc = tehran_dt.astimezone(UTC)
    print(tehran_to_utc)
    back_to_tehran = tehran_to_utc.astimezone(tehran_timezone)
    print(back_to_tehran)

try_tz(pytz.timezone("Asia/Tehran"))
print()
try_tz(ZoneInfo("Asia/Tehran"))
5j9 commented 6 months ago

Similar pytz issues: