# Parsing time strings on Digi ConnectPort

**URL:** <https://forums.digi.com/t/parsing-time-strings-on-digi-connectport/2783>\
**Category:** Python\
**Created:** [May 28, 2011, 9:21am UTC](https://forums.digi.com/t/parsing-time-strings-on-digi-connectport/2783 "2011-05-28T09:21:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robwestwood](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@robwestwood](https://forums.digi.com/u/robwestwood)\
**Post date:** [May 28, 2011, 9:21am UTC](https://forums.digi.com/t/parsing-time-strings-on-digi-connectport/2783/1 "2011-05-28T09:21:51Z")

</div>

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:

```auto

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

```

To which I get the following error:

```auto

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.

---

<div class="post-metadata">

**Author:** ![shahrj1988](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@shahrj1988](https://forums.digi.com/u/shahrj1988)\
**Post date:** [May 30, 2011, 3:44pm UTC](https://forums.digi.com/t/parsing-time-strings-on-digi-connectport/2783/2 "2011-05-30T15:44:59Z")

</div>

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())

---

<div class="post-metadata">

**Author:** ![robwestwood](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@robwestwood](https://forums.digi.com/u/robwestwood)\
**Post date:** [May 30, 2011, 4:16pm UTC](https://forums.digi.com/t/parsing-time-strings-on-digi-connectport/2783/3 "2011-05-30T16:16:15Z")

</div>

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:

```auto

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?

---

<div class="post-metadata">

**Author:** ![lynnl](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@lynnl](https://forums.digi.com/u/lynnl)\
**Post date:** [May 31, 2011, 1:19pm UTC](https://forums.digi.com/t/parsing-time-strings-on-digi-connectport/2783/4 "2011-05-31T13:19:27Z")

</div>

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.
