0

I am trying to create some simple code that given a datetime, it would tell me if I would miss the train or not, based on the time (hour/minute) of a train timetable stored in the EDT time.

For example the time one of the entries in the timetabe is 1000 (got that time in June).

Now for example if I put my time to be t1 = datetime(2022,7,1,9,30, tzinfo=pytz('America/New York')), function would return true (I should be able to catch the train since I get to the train station at 9:30am).

And if I input the time as t2=datetime(2022,12,15,9,30, tzinfo = pytz('America/New York')) , it should return false for me since now the train is leaving at 0900 in New York time in December (but I do not want to manage all the messy conversion in my code).

One way I can think of is to look at the utcoffset() of the times in June and then add that to the UTC date in December to do that comparison, but I am not sure if there is something that's even simpler and do not need to involve conversion to UTC.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
AZhu
  • 1,312
  • 6
  • 22
  • 40
  • 1
    You wouldn't miss the train. The 9:30 train leaves at 9:30 **in the current timezone**. When wintertime starts, train services generally don't change the schedule, their times instead stay the same, just adjusted for the alternate timezone. There are exceptions, when a train has to go through several timezones where the transition dates don't alingn, but then their time table will include explicit exceptions. – Martijn Pieters Nov 26 '22 at 15:19
  • 2
    Otherwise, look at the [`timezone.dst()` method](https://docs.python.org/3/library/datetime.html#datetime.datetime.dst) and base your logic on that. – Martijn Pieters Nov 26 '22 at 15:21
  • @MartijnPieters - That would certainly makes my life easier :), but in this particular case for argument sake let's assume the train sticks with the train schedule based on time offset in the summer. So a train leaves at 10am (10am in my current timezone) would be leaving at 9am in December (again 9am in my current timezone), sorry if I weren't very clear – AZhu Nov 26 '22 at 15:58
  • Not sure if I am not thinking straight or something (the problem itself is kinda confusing) but tried the dst method but not sure if it gets what I want....`tzNYC = pytz.timezone('America/New_York') tzEST = pytz.timezone('EST')` – AZhu Nov 26 '22 at 16:11
  • and these are my outputs: `datetime(2022, 12, 1, 10, tzinfo=tzEST).dst() Out[11]: datetime.timedelta(0) datetime(2022, 12, 1, 10, tzinfo=tzNYC).dst() Out[12]: datetime.timedelta(0) datetime(2022, 7, 1, 10, tzinfo=tzEST).dst() Out[14]: datetime.timedelta(0) datetime(2022, 7, 1, 10, tzinfo=tzNYC).dst() Out[15]: datetime.timedelta(0)`, it seems everything is returning no DST offset – AZhu Nov 26 '22 at 16:13
  • You need to edit your question to make it clearer then. If you have a fixed-offset timezone, just map between the timezones with `datetime.astimezone()`. – Martijn Pieters Nov 26 '22 at 16:15
  • 3
    @MartijnPieters `pytz` definitely knows about DST, but it doesn't work in the `datetime` constructor. You need to use `localize`. – Mark Ransom Nov 26 '22 at 18:21
  • Does this answer your question? [Weird timezone issue with pytz](https://stackoverflow.com/questions/11473721/weird-timezone-issue-with-pytz) – FObersteiner Nov 28 '22 at 09:52
  • 1
    Consider using [zoneinfo](https://docs.python.org/3/library/zoneinfo.html) instead of pytz. Available in Python 3.9+. – Matt Johnson-Pint Nov 28 '22 at 22:30

0 Answers0