Parsing time strings on Digi ConnectPort

I’m trying to parse a time string returned by a web service to a python time (floating-point number).

So using the command line, I tried this simple test using an example from the Python docs:


import time
time.strptime("30 Nov 00", "%d %b %y")

To which I get the following error:


Traceback (most recent call last):
  File "", line 1, in ?
ImportError: No module named _strptime

Any ideas as to whether there’s any support to help me. The actual strings will be of the form “20110506114623.783Z”.

Thanks for any assistance.

Rob.

Hi…,

you can get te local or GMT time in the list format by the following way.

import time
Local=list(time.localtime())
#This will give built in RTC time.

or

import time
GMT = list(time.gmtime())

Thanks for taking the time to reply, but that’s not really what I’m after.

The web service is returning the time that an event happened which is needed by the Python code running on the module; this is the time which is needed, not the time set on the module.

To date, this is my best attempt:


import time
import re

isotime_re_str = r"(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})\.(\d{3})Z"
isotime_re	    = re.compile(isotime_re_str)

def iso_to_python_epoch(isotime):
	
		time_in_epoch	= None
	
		match_object	= isotime_re.search(isotime)
		
		year		= int(match_object.group(1))
		month	= int(match_object.group(2))
		day 		= int(match_object.group(3))
		hours		= int(match_object.group(4))
		minutes	= int(match_object.group(5))
		seconds	= int(match_object.group(6))
		ms		= int(match_object.group(7))
		
		time_in_epoch	= time.mktime((year, month, day, hours, minutes, seconds, 0, 0, -1))
		time_in_epoch	+= (float(ms)/1000.0)
		
		return time_in_epoch

Can anyone do anything better?

Which version of Python is this - be aware RE is a RAM-hungry module if running on a gateway. I wonder why the strptime isn’t found, it existed back in 2.4.x

I’d probably just chop the string up as sub-strings, so int(st[:4]) is the year and so on, but that’s just me.