AttributeError: 'PCF8523' object has no attribute 'tm_year'

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
billmorgan000
 
Posts: 13
Joined: Sat May 02, 2015 12:15 pm

AttributeError: 'PCF8523' object has no attribute 'tm_year'

Post by billmorgan000 »

Hi
I am trying to write a program to set the time and date
on a PCF8523 real-time-clock in circuitpython. Being an old
time FORTRAN IV programmer(back in the days of punch cards),
my scripping style is wordy. I can print out the struct_time
correctly, but the second to last print statment(line 77), I
get an AttributeError message. It seems that the rtc was not
set. What am I doing wrong? I have the REPL output listed
first, then the program. Thank you for your time.
Bill Morgan (KA5ONN)

struct_time(tm_year=2018, tm_mon=1, tm_mday=20, tm_hour=13,
tm_min=45, tm_sec=0, tm_wday=5, tm_yday=-1, tm_isdst=-1)
Traceback (most recent call last):
File "main.py", line 77, in <module>
AttributeError: 'PCF8523' object has no attribute 'tm_year'


# This program sets the time on a real time clock(rtc).

# The set up code is from: https://learn.adafruit.com/
# adafruit-pcf8523-real-time-clock?view=all ("Adafruit PCF8523
# Real Time Clock).

# To set the rtc, input each of attributes when the program
# calls for it, adding one minute to the minutes, then enter
# 0 for the seconds and hold till the minutes change- then hit
# enter.

import board
import busio
import adafruit_pcf8523
import time

# Initialize the I2C bus:
i2c = busio.I2C(board.SCL, board.SDA)
# Instantiate the RTC object
rtc = adafruit_pcf8523.PCF8523(i2c)

# Fuction to read the keyboard and
# check to see input is in the stated
# range. ll is the lower limit,
# ul is the upper limit.

def inputKeyboardData(ll, ul):
switch = False
while switch is False:
kbdata = input()
kbdata = int(kbdata)
if kbdata >= ll and kbdata <= ul:
switch = True
return kbdata

# Input date/time data by means of keyboard
print(' What is the year? (2018 to 2035)')
ll = 2018
ul = 2035
year = inputKeyboardData(2018, 2035)

print(' What is the month? ( 1-12)')
ll = 1
ul = 12
month = inputKeyboardData(1, 12)

print(' What is the day of month? ( 1-31)')
ll = 1
ul = 31
day = inputKeyboardData(1, 31)

print(' What is the day of the week? ( Mon=0,Sun=6)')
ll = 1
ul = 6
weekday = inputKeyboardData(ll, ul)

print(' What is hour of the day? ( 0-23)')
ll = 1
ul = 23
hour = inputKeyboardData(ll, ul)

print(' What are the minutes? ( 0-59)')
ll = 0
ul = 59
minutes = inputKeyboardData(ll, ul)

print(' What are the seconds? ( 0-59)')
ll = 0
ul = 59
seconds = inputKeyboardData(ll, ul)

# Flash the rtc with the current time stamp
rtc.datetime = time.struct_time((year, month, day, hour,
minutes, seconds, weekday,
-1, -1))
print(rtc.datetime)
print(rtc.tm_year, ',', rtc.tm_mon, ',', rtc.tm_mday)
print(rtc.tm_hour, ':', rtc.tm_min, ':', rtc.tm_sec)

User avatar
tannewt
 
Posts: 3315
Joined: Thu Oct 06, 2016 8:48 pm

Re: AttributeError: 'PCF8523' object has no attribute 'tm_ye

Post by tannewt »

At the end I think you want:

Code: Select all

t = rtc.datetime
print(t)
print(t.tm_year, ',', t.tm_mon, ',', t.tm_mday)
print(t.tm_hour, ':', t.tm_min, ':', t.tm_sec)
You can also use format:

Code: Select all

print("{}:{}:{}".format(t.tm_hour, t.tm_min, t.tm_sec))

User avatar
billmorgan000
 
Posts: 13
Joined: Sat May 02, 2015 12:15 pm

Re: AttributeError: 'PCF8523' object has no attribute 'tm_ye

Post by billmorgan000 »

Hi:

Thank- you that worked. Now, I need to get the SD card functions to work.
Bill

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”