![]() |
| Pendulum Help Please - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Pendulum Help Please (/thread-36247.html) |
Pendulum Help Please - bill_z - Feb-01-2022 In a previous thread, it was suggested I use pendulum to get daylight saving time information. At that time, I was having issues with my Python version. However, version 3.9.2 does have pendulum. I must be misunderstanding the examples given in the Pendulum documentation or I'm still having version issues. I'm running Python3.9.2 on Jan 28, 2022 release of Raspberry OS on a RPi3B+ Just copying some examples from the documentation into a python3 program, I'm getting circular import issues. Also, the error indicates I'm trying to parse, but I don't have parse in my program. What is it that I'm doing wrong or misunderstanding? Below is the short program and then the errors. import pendulum # Gets the timezone instance print(pendulum.now().timezone) print(pendulum.now().tz) # Gets the timezone name print(pendulum.now().timezone_name) # Indicates if daylight savings time is on dt = pendulum.datetime(2012, 1, 1, tz='America/Toronto') print(dt.is_dst()) dt = pendulum.datetime(2012, 9, 1, tz='America/Toronto') print(dt.is_dst()) RE: Pendulum Help Please - ibreeden - Feb-01-2022 I have the impression there is a file "/home/pi/pendulum.py" that gets imported instead of pendulum. Can you try to remove or rename the file "/home/pi/pendulum.py"? RE: Pendulum Help Please - bill_z - Feb-01-2022 Thanks for that input. It has been awhile since I have been programming in Python and I had forgotten about this. However, in the first attempt to run my program after removing pendulum.py, the new release of Rasp OS doesn't contain pendulum because I had to pip3 install it. Now, my program runs fine. Thanks again for your suggestion. RE: Pendulum Help Please - bill_z - Feb-01-2022 Now that pendulum is working, I have a question about daylight savings time & pendulum. The program that I am writing fires off tasks ever 15 minutes between sunrise and sunset and currently used datetime. What pendulum command would I run to get the current time when in daylight savings time or not? As I read the pendulum documentation and run my test, I can get hours but not minutes. Here is one of my test & output. The time I ran it was 15:18 local time today. I was expecting something like 15:18 or 3:18 but none of these results seem to be correct. import pendulum # Gets the timezone instance print(pendulum.now().timezone) print(pendulum.now().tz) # Gets the timezone name print(pendulum.now().timezone_name) # Indicates if daylight savings time is on dt = pendulum.datetime(2022, 1, 1, tz='America/Chicago') print(dt) dt = pendulum.datetime(2022, 7, 1, tz='America/Chicago') print(dt) RE: Pendulum Help Please - bowlofred - Feb-02-2022 The current time is .now(). You're printing out the time associated with datetime(2022, 1, 1, tz='America/Chicago'). Since you didn't provide hours/minutes/seconds, that datetime object defaults to midnight localtime. RE: Pendulum Help Please - snippsat - Feb-02-2022 (Feb-01-2022, 09:50 PM)bill_z Wrote: Here is one of my test & output. The time I ran it was 15:18 local time today. I was expecting something like 15:18 or 3:18 but none of these results seem to be correct.You most assign .now() to a variable.>>> import pendulum >>> >>> dt_now = pendulum.now() >>> dt_now DateTime(2022, 2, 2, 7, 16, 32, 145728, tzinfo=Timezone('Europe/Berlin')) >>> print(dt_now) 2022-02-02T07:16:32.145728+01:00(Feb-01-2022, 09:50 PM)bill_z Wrote: What pendulum command would I run to get the current time when in daylight savings time or not? >>> dt_now.is_dst() False >>> for day in range(1, 60): ... if dt_now.add(days=day).is_dst(): ... print(day) ... break ... 53So in Norway we have 53 days more before we switch daylight saving time(summer). Different time formats. >>> dt_now.to_datetime_string() '2022-02-02 07:16:32' >>> dt_now.to_date_string() '2022-02-02' >>> dt_now.to_iso8601_string() '2022-02-02T07:16:32.145728+01:00' >>> dt_now.to_formatted_date_string() 'Feb 02, 2022' >>> dt_now.to_cookie_string() 'Wednesday, 02-Feb-2022 07:16:32 CET' >>> dt_now.to_w3c_string() '2022-02-02T07:16:32+01:00' >>> dt_now.to_rss_string() 'Wed, 02 Feb 2022 07:16:32 +0100' # how long since a made dt_now >>> dt_now.diff_for_humans() '12 minutes ago' RE: Pendulum Help Please - ibreeden - Feb-02-2022 (Feb-01-2022, 09:50 PM)bill_z Wrote: I was expecting something like 15:18 or 3:18 but none of these results seem to be correctAlso have a look at Pendulum Formatter. |