"إذا أراد العامل أن يؤدي عمله بشكل جيد، فعليه أولاً أن يشحذ أدواته." - كونفوشيوس، "مختارات كونفوشيوس. لو لينجونج"
الصفحة الأمامية > برمجة > Why Does pytz Show Unexpected Time Zone Offsets Initially?

Why Does pytz Show Unexpected Time Zone Offsets Initially?

نشر في 2025-04-18
تصفح:688

Why Does pytz Show Unexpected Time Zone Offsets Initially?

Time Zone Discrepancy with pytz

Certain time zones exhibit peculiar offsets when initially obtained from pytz. For instance, Asia/Hong_Kong initially shows a seven hour and 37 minute offset:

<pre>
import pytz
pytz.timezone('Asia/Hong_Kong')
<DstTzInfo 'Asia/Hong_Kong' LMT+7:37:00 STD>
</pre>

Discrepancy Source

Time zones and offsets fluctuate throughout history. The default time zone name and offset provided by pytz represent the earliest ones available for that zone, which may appear unusual.

When utilizing localize to assign the time zone to a date, the appropriate time zone name and offset are used. However, directly using the datetime constructor to assign the time zone does not allow for proper adjustment.

Example

Consider the following code:

<pre>
import pytz
from datetime import datetime
hk = pytz.timezone('Asia/Hong_Kong')

dt1 = datetime(2012,1,1,tzinfo=hk)
dt2 = hk.localize(datetime(2012,1,1))
if dt1 > dt2:
print "Why?"
</pre>

In this example, dt1 will be greater than dt2 due to the initial offset discrepancy. By using localize, dt2 obtains the accurate offset, resulting in a logical comparison between the two dates.

أحدث البرنامج التعليمي أكثر>

تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.

Copyright© 2022 湘ICP备2022001581号-3