'(Micropython) Creating and comparing time objects

Using Micropython for the ESP32 microcontroller, flashed with the latest firmware at time of writing (v1.18)

I'm making an alarm (sort-of) system where I get multiple time values ("13:15" for example) from my website, and then I have to ring an alarm bell at those times.

I've done the website and I can do the ring stuff, but I don't know how to actually create time objects from the previously mentioned strings ("13:15"), and then check if any of the times inputted match the current time, the date is irrelevant.

From reading the documentation, im getting the sense that this cant be done, since ive looked through the micropython module github, and you apparently cant get datetime in micropython, and i know that in regular python my problem can be solved with datetime.

import ntptime
import time
import network

# Set esp as a wifi station
station = network.WLAN(network.STA_IF)
# Activate wifi station
station.active(True)
# Connect to wifi ap
station.connect(ssid,passwd)
while station.isconnected() == False:
        print('.')
        time.sleep(1)
print(station.ifconfig())
try:
        print("Local time before synchronization: %s" %str(time.localtime()))
        ntptime.settime()
        print("Local time after synchronization: %s" %str(time.localtime()))
    except:
        print("Error syncing time, exiting...")

this is the shortened code from my project, with only the time parts, now comes into play the time comparison thing I don't know how to do.



Solution 1:[1]

After running ntptime.settime() you can do the following to retrieve the time, keep in mind this is in UTC:

rtc = machine.RTC()
hour = rtc.datetime()[4] if (rtc.datetime()[4]) > 9 else "0%s" % rtc.datetime()[4]
minute = rtc.datetime()[5] if rtc.datetime()[5] > 9 else "0%s" % rtc.datetime()[5]

The if else statement makes sure that numbers lower or equal to 9 are padded with a zero.

Solution 2:[2]

Using ntptime to get time from server. I use "time.google.com", to get the time. Then, I transform it into seconds (st) to be more accurate. And set my targets hour in seconds 1 hour = 3600 s.

import utime
import ntptime

def server_time():
    try:
        # Ask to time.google.com server the current time.
        ntptime.host = "time.google.com"
        ntptime.settime()
        t = time.localtime()
        # print(t)
        # transform tuple time 't' to seconds value. 1 hour = 
        st = t[3]*3600 + t[4]*60 + t[5]
        return st  
    except:
        # print('no time')
        st = -1
        return st 


while True:
      # Returns an increasing millisecond counter since the Board reset.
      now = utime.ticks_ms()

      # Check current time every 5000 ms (5s) without going to sleep or stop any other process.
      if now >= period + 5000:
          period += 5000
          
          # call your servertime function
          st = server_time()
              if  ((st > 0) and (st < 39600)) or (st > 82800):    # Turn On 17:00 Mexico Time.
                    # something will be On between 17:00 - 06:00
              elif ((st <82800) and (st > 39600)):                # Turn Off 6:00.
                  # something will be Off between 06:00 - 17:00
              else:
                  pass

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 edgarpecero