Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

COnvert to usable time using adafruit datetime
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

COnvert to usable time using adafruit datetime

by scotch1 on Thu Mar 18, 2021 2:32 am

for "check" = 2021-03-17 23:15:00

coming from an item that's a tuple from a function returnign 3 items, the constructed time is:
Code: Select all | TOGGLE FULL SIZE
class_start_time = datetime(time_now.tm_year, time_now.tm_mon, time_now.tm_mday,23, 15, 00)


from the function:
Code: Select all | TOGGLE FULL SIZE
    else:
        message = "Sleeping"
        class_on="Yes"
        class_start_time = datetime(time_now.tm_year, time_now.tm_mon, time_now.tm_mday,23, 15, 00)
    return message, class_on, class_start_time


becomes "message_ahead[2]" which I can't seem to format....

Can't seem to take the datetime and convert to usable time of "%H:%M":
last 3 lines don't seem to work....

Code: Select all | TOGGLE FULL SIZE
print("Time next class will start:", message_ahead[2])
            time_next = message_ahead[2]
            check = message_ahead[2]
            print(check)
            #print(check.datetime("%H")
            #print(time_next.strftime("%H:%M"))
            #print("Next CLass starts at {}:{:02}:{:02}".format(time_next.tm_hour, time_next.tm_min, time_next.tm_sec))


Current output
Code: Select all | TOGGLE FULL SIZE
Time next class will start: 2021-03-17 23:15:00
2021-03-17 23:15:00


just want the hour and min per clock, so 23:15 ?

this worked for the RTC:
Code: Select all | TOGGLE FULL SIZE
    time_now = rtc.datetime
    g=displayio.Group(max_size=15)
    # print(time_now)     # uncomment for debugging
    print(
        "The date is {} {}/{}/{}".format(
            days[int(time_now.tm_wday)], time_now.tm_mday, time_now.tm_mon, time_now.tm_year
        )
    )
    print("The time is {}:{:02}:{:02}".format(time_now.tm_hour, time_now.tm_min, time_now.tm_sec))
   
    if time_now.tm_hour >= 12:
        am_pm = "PM"
    else:
        am_pm = "AM"
    print("%d:%02d"    %(time_now.tm_hour, time_now.tm_min) + am_pm)

scotch1
 
Posts: 214
Joined: Mon Nov 26, 2012 12:13 am

Re: COnvert to usable time using adafruit datetime

by tannewt on Thu Mar 18, 2021 2:09 pm

I suspect the simplest way is to use string formatting with the hour and minute properties: https://circuitpython.readthedocs.io/pr ... etime.hour

Code: Select all | TOGGLE FULL SIZE
print("The time is {}:{:02}:{:02}".format(time_now.hour, time_now.minute, time_now.second))

tannewt
 
Posts: 2396
Joined: Thu Oct 06, 2016 8:48 pm

Re: COnvert to usable time using adafruit datetime

by scotch1 on Fri Mar 19, 2021 12:23 pm

amazing, thanks.

seemed .tm_hour was for one approach that didn;t work, and .hour for this.
appreciated!

scotch1
 
Posts: 214
Joined: Mon Nov 26, 2012 12:13 am

Please be positive and constructive with your questions and comments.