Problem with calculating differences between dates in python

Question:

Basicaly, I’m trying to iterate over 100 dates, calculate difference between these dates and today’s date, and print the differences. The problem is, that after 41 there isn’t 42, but 41.958333333333336. I’m not sure why this error occurs, so anyone knows why is it happening?

Code:

import time
from datetime import date

today=time.mktime(date.today().timetuple())

day=14
month=2

for _ in range(100):
    print((time.mktime(date(2022,month,day).timetuple())-today)/86400)
        
    if month==2 and day==28:
        day=1
        month+=1
    elif month<=7 and month%2==1 and day==31:
        day=1
        month+=1
    elif month>=8 and month%2==0 and day==31:
        day=1
        month+=1
    elif month<=7 and month%2==0 and day==30:
        day=1
        month+=1
    elif month>=8 and month%2==1 and day==30:
        day=1
        month+=1
    else:
        day+=1

Output:

0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
11.0
12.0
13.0
14.0
15.0
16.0
17.0
18.0
19.0
20.0
21.0
22.0
23.0
24.0
25.0
26.0
27.0
28.0
29.0
30.0
31.0
32.0
33.0
34.0
35.0
36.0
37.0
38.0
39.0
40.0
41.0
41.958333333333336
42.958333333333336
43.958333333333336
44.958333333333336
45.958333333333336
46.958333333333336
47.958333333333336
48.958333333333336
49.958333333333336
50.958333333333336
51.958333333333336
52.958333333333336
53.958333333333336
54.958333333333336
55.958333333333336
56.958333333333336
57.958333333333336
58.958333333333336
59.958333333333336
60.958333333333336
61.958333333333336
62.958333333333336
63.958333333333336
64.95833333333333
65.95833333333333
66.95833333333333
67.95833333333333
68.95833333333333
69.95833333333333
70.95833333333333
71.95833333333333
72.95833333333333
73.95833333333333
74.95833333333333
75.95833333333333
76.95833333333333
77.95833333333333
78.95833333333333
79.95833333333333
80.95833333333333
81.95833333333333
82.95833333333333
83.95833333333333
84.95833333333333
85.95833333333333
86.95833333333333
87.95833333333333
88.95833333333333
89.95833333333333
90.95833333333333
91.95833333333333
92.95833333333333
93.95833333333333
94.95833333333333
95.95833333333333
96.95833333333333
97.95833333333333
98.95833333333333

ignore it: aaaa

Asked By: BlayUng

||

Answers:

time.mktime is relative to timezone, so the returned time may be ambiguous. It may return an unexpected result depending on the period, especially because of time changes during DST transition.

Convert a time tuple in local time to seconds since the Epoch.

In your example, we notice that the time changes by one hour between March 27 and 28, which explains the division with remainder.

To avoid this, instead of using mktime, which requires the date in local time, you can instead use timegm which calculates the seconds since the epoch of a date in the UTC timezone.

import calendar

today = calendar.timegm(date.today().timetuple())
Answered By: Cubix48
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.