0

I am trying to create an alarm/schedule based timeclock from a string that I get returned from a Telit HE910 modem. The AT command is AT+CCLK and the string result is: +CCLK: "2015/03/26,14:23:22+40". I have broken the string down into a time stamp of variables containing - YYYY - year / MM - month / DD - day / hh - hour / mm - minute / ss - seconds / ampm - AM or PM. All variables are strings and numeric types can safely float(mm) etc. The problem I am having is I'm not sure how to compare the minutes if my program is off doing another part of the program and it misses the exact minute as per the code below. Can anyone enlighten an easier way of doing this so that minutes can be compared plus 5 minutes in case the system is elsewhere especially if the alarm set time is XX hours 59 minutes. (Another part of the system could hold the program from seeing this code for 5 minutes or so)

    def alarm_function(Time, Alarm): # Time Dictionary, Alarm Dictionary
        if Time['ampm'] == Alarm['ampm']:
            if Time['hh'] == Alarm['hh']:
                if float(Time['mm']) == ((float(Alarm['mm']) or (float(Alarm['mm'] + 1) or (float(Alarm['mm'] + 2) or (float(Alarm['mm'] + 3) or (float(Alarm['mm'] + 4) or (float(Alarm['mm'] + 5)):
                    return True
        return False

Minute comparison for visual expression only...

Baz
  • 306
  • 1
  • 5
  • 12
  • The question is not clear enough – ZdaR Mar 28 '15 at 10:28
  • there are three parts: 1. you should use an object to represent the time e.g., `datetime` object instead of comparing string parts ([use `datetime.strptime()` to convert a string to a datetime object](http://stackoverflow.com/q/466345/4279)) 2. You can do more than one thing in a program concurrently e.g., using [a thread](http://stackoverflow.com/a/22498708/4279) or [an event loop (such as provided by `tkinter`, `asyncio`)](http://stackoverflow.com/a/14040516/4279). 3. Though it is easy to [truncate `datetime` object to the nearest 5 minutes](http://stackoverflow.com/a/27924789/4279). – jfs Mar 28 '15 at 10:33

1 Answers1

0

Here's how you could parse AT+CCLK? response into a datetime object representing time in UTC:

from datetime import datetime, timedelta

cclk_answer = "+CCLK: 15/03/26,14:23:22+40"
local_time = datetime.strptime(cclk_answer[:-3], "+CCLK: %y/%m/%d,%H:%M:%S")
utc_offset = timedelta(minutes=15*int(cclk_answer[-3:]))
utc_time = local_time - utc_offset
# -> datetime.datetime(2015, 3, 26, 4, 23, 22)

Note: use %Y if the year is 4-digit, though the manual says that the year is represented using 2 digits (%y).

Once you have the utc time, you can compare it directly with other datetime objects (in UTC):

if abs(utc_time - alarm_time) < timedelta(minutes=5):
    print("the modem time is within 5 minutes from the alarm time")

btw, to get the current time in UTC, current_time = datetime.utcnow().

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thanks J.F Sebastian, unfortunately the modem does not have access to datetime. I appreciate your help though as I was unaware of how to strip the date and time down using datetime which will come in handy on other projects that do support it. – Baz Mar 30 '15 at 00:25
  • @ToddRobards: your question is tagged `python`. `datetime` is a part of standard Python library. It is *always* available. – jfs Mar 30 '15 at 04:16
  • Unfortunately, Telit decided not to include datetime in their python install package to save space. Not happy about that... To prove myself wrong I even put your code into my device and it doesn't run as soon as it see's: from datetime import datetime, timedelta – Baz Mar 31 '15 at 09:57
  • If you use a custom language with Python-like syntax then you should mention it in the question. Can you use ordinary Python (outside of the modem), to communicate with the modem? – jfs Mar 31 '15 at 22:31