Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pendulum Help Please
#1
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())
Error:
pi@raspberrypi:~ $ python3 pendulum1.py Traceback (most recent call last): File "/home/pi/pendulum1.py", line 2, in <module> import pendulum File "/home/pi/pendulum.py", line 3, in <module> dt = pendulum.parse('2012-09-05T23:26:11.123789') AttributeError: partially initialized module 'pendulum' has no attribute 'parse' (most likely due to a circular import) pi@raspberrypi:~ $
Reply
#2
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"?
Reply
#3
Thanks for that input. It has been awhile since I have been programming in Python and I had forgotten about this. Doh

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. Smile

Thanks again for your suggestion.
Reply
#4
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)


Output:
pi@raspberrypi:~ $ python3 pendulum1.py Timezone('America/Chicago') Timezone('America/Chicago') America/Chicago 2022-01-01T00:00:00-06:00 2022-07-01T00:00:00-05:00 pi@raspberrypi:~ $
Reply
#5
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.
Reply
#6
(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
...     
53
So 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'
Reply
#7
(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 correct
Also have a look at Pendulum Formatter.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020