# Datetime.strptime causing infinite loop

**URL:** <https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 7, 2022, 6:22pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643 "2022-07-07T18:22:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntheuer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@ntheuer](https://forum.inductiveautomation.com/u/ntheuer)\
**Post date:** [July 7, 2022, 6:22pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/1 "2022-07-07T18:22:55Z")

</div>

I’m not sure what causes this, but for some reason datetime.strptime is causing this to create an infinite loop. If I do not comment out the strptime line, it will run with no issue

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/4/24ec74566ec9ef7500f8097527cb217ef62a4192.png)

With line commented out, first line prints, then nothing else. No error thrown

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/3/93f92d283607f94e105ab4891dd6680505171dff.png)

Infninite loops running on the gateway

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/4/441da9beadfe829a016c56e461a3975127a07710.png)

It’s very weird, I have never had issue working with strptime before.  
Here is a copy of the script:

```
#Get results of query
oldData = system.db.runPrepQuery(query, [])
oldData = system.dataset.toPyDataSet(oldData)

#Will use historical data for comparison later	
histData = []

#Loop through results of query to build dataset
for row in range(oldData.rowCount):
	
	status = oldData.getValueAt(row, 'Status')
	timestamp = oldData.getValueAt(row, 'Timestamp')
	timestamp = str(timestamp)

	#Manual entries do not contain milliseconds
	if '.' in timestamp: 
		timestamp = timestamp[0:timestamp.find('.')]

	#Print before loop to see if anything is weird
	system.perspective.print(timestamp)
	
	#Causes infinite loop >:(
	timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
	
	histData.append({"Timestamp":timestamp, "Status":status})

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [July 7, 2022, 6:28pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/2 "2022-07-07T18:28:34Z")

</div>

Where is this script being called from? A property change event? Most infinite loops I’ve run into (or caused) have been circular property change events. Property Change Event A → changes some property that triggers Property Change Event B → changes some property that triggers Property Change Event A and so on

Though looking at your script, I don’t see how that would be the case. What does your script end up doing with `histData`? Is there more to the script? If so post it.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 7, 2022, 7:12pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/3 "2022-07-07T19:12:33Z")

</div>

Just use the `system.date.*` functions instead?

Also couple of questions:

1. Why use `system.db.runPrepQuery() ` with no parameters? Just use `system.db.runQuery()` and the results will be returned as a `PyDataSet` removing the need for the conversion.
2. Why convert oldData to a `PyDataSet` and then not use it that way?

```python
#Get results of query
oldData = sysetm.db.runQuery(query)

#Will use historical data for comparison later, List of Dictionaries
histData = []

for row in oldData:
    system.perspective.print(system.date.format(row['Timestamp'])
    timestamp = system.date.addMillis(row['Timestamp'],-1 * system.date.getMillis(row['Timestamp']))
    histData.append({'Timestamp':timestamp,'Status':row['Status']})

```

That being said, it’s obvious that you haven’t shown the entire script, as there is no Import statement and you would need at least that. I suspect that it looks something like `import datetime` if you changed that to `from datetime import datetime` you could get rid of the redundant `datetime.datetime.*` syntax.

I’m also assuming that there is script following this loop, as it doesn’t do anything aside from printing the timestamp. Is it possible that the query is only returning 1 row? What is supposed to happen after this loop?

It looks like this is in an onStartup script on the view `Embed/StatusDetail`, I’m not sure that is the best place for it, but then I also don’t know what it’s trying to accomplish.

---

<div class="post-metadata">

**Author:** ![ntheuer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@ntheuer](https://forum.inductiveautomation.com/u/ntheuer)\
**Post date:** [July 7, 2022, 7:59pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/4 "2022-07-07T19:59:47Z")

</div>

1. I have an excel vba that generates script transforms for me to save me some typing. This defaults to prep query, its not a big deal really (doesn’t affect my issue)

2. Yes I use import datetime and yes there are other ways to do this. This does not affect my issue either

3. I removed all the code after this because its irrelevant. It gets stuck on that line that I mentioned. I did mess up my description. I meant that it runs fine with a comment out through the strptime command, yet with no comment it gets stuck there. Thats why it prints 4 lines (there are 4 rows in the data), yet only prints one with no comment on that line.

4. I need this on a startup script because it is generated on startup and I need a one time run on startup. Explaining everything would be too detailed and irrelevant, every code after the one mentioned does not run

---

<div class="post-metadata">

**Author:** ![ntheuer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@ntheuer](https://forum.inductiveautomation.com/u/ntheuer)\
**Post date:** [July 7, 2022, 8:14pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/5 "2022-07-07T20:14:11Z")

</div>

I extracted the code to a button to test and got the same results from a button push

I deleted all the code after this, it gets stuck on that strptme command and nothing will run after

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 7, 2022, 8:15pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/6 "2022-07-07T20:15:26Z")

</div>

Get a thread dump from whatever scope you’re executing from while in this “infinite loop”.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 7, 2022, 8:18pm UTC](https://forum.inductiveautomation.com/t/datetime-strptime-causing-infinite-loop/61643/7 "2022-07-07T20:18:29Z")

</div>

> [@ntheuer](#):
>
> - I have an excel vba that generates script transforms for me to save me some typing. This defaults to prep query, its not a big deal really (doesn’t affect my issue)
> - Yes I use import datetime and yes there are other ways to do this. This does not affect my issue either

Okay, I'll agree, won't effect the outcome of the script.

> [@ntheuer](#):
>
> I meant that it runs fine with a comment out through the strptime command, yet with no comment it gets stuck there. Thats why it prints 4 lines (there are 4 rows in the data), yet only prints one with no comment on that line.

There is no reason from this script that I can see that `datetime.strptime()` should not work, from what I can tell you have used the correct format string, and timestamp is a string which should have a matching format (Based on the print in the provided screen shot).

Perhaps wrap it in a `try...except` to see if it might through an error there?
