# Database datetime being stored as just time

**URL:** <https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183>\
**Category:** Ignition\
**Created:** [October 16, 2014, 7:27pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183 "2014-10-16T19:27:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwj](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@kwj](https://forum.inductiveautomation.com/u/kwj)\
**Post date:** [October 16, 2014, 7:27pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/1 "2014-10-16T19:27:56Z")

</div>

I am storing some datetime values in a SQL table using the following code:

`
system.tag.write("gateway/last_cycle_start", system.tag.getTagValue("[System]Gateway/CurrentDateTime"))
system.tag.write("gateway/last_cycle_stop", system.tag.getTagValue("[System]Gateway/CurrentDateTime"))
stop_time = system.tag.getTagValue("gateway/last_cycle_stop")
start_time = system.tag.getTagValue("gateway/last_cycle_start")
uname = system.tag.getTagValue("gateway/last_user_start")
system.db.runPrepUpdate("INSERT INTO rundata (run, start, stop, username) " + "VALUES (?,?,?,?)", ["0",start_time, stop_time, uname])`

When I look at those values in the database, only the time is written. Can somebody tell me what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![kwj](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@kwj](https://forum.inductiveautomation.com/u/kwj)\
**Post date:** [October 16, 2014, 7:43pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/2 "2014-10-16T19:43:50Z")

</div>

This is being stored in a postgres 9.3 database. start and stop time fields are “timestamp without time zone”

---

<div class="post-metadata">

**Author:** ![kwj](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@kwj](https://forum.inductiveautomation.com/u/kwj)\
**Post date:** [October 20, 2014, 9:47pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/3 "2014-10-20T21:47:00Z")

</div>

This also has the unfortunate side-effect that when those datetime values are pulled out of the database, all my dates appear as January 1, 1970.

---

<div class="post-metadata">

**Author:** ![kwj](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@kwj](https://forum.inductiveautomation.com/u/kwj)\
**Post date:** [October 21, 2014, 1:34pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/4 "2014-10-21T13:34:52Z")

</div>

I considered maybe I was stuck in some sort of space time anomaly and it really was just constantly January 1, 1970, but then I realized I’m still using the internet, so no joy.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [October 21, 2014, 3:23pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/5 "2014-10-21T15:23:17Z")

</div>

I know almost nothing about Postgres but I found these links:

[http://stackoverflow.com/questions/6018214/how-to-insert-current-timestamp-into-postgres-via-python](http://stackoverflow.com/questions/6018214/how-to-insert-current-timestamp-into-postgres-via-python)

[https://wiki.postgresql.org/wiki/Working\_with\_Dates\_and\_Times\_in\_PostgreSQL](https://wiki.postgresql.org/wiki/Working_with_Dates_and_Times_in_PostgreSQL)

Maybe you need to use the TIMESTAMP keyword in your query?

Also just a suggestion but you appear to be doing doing unnecessary tag reads in the script. You could do it like this instead.

```auto
stop_time = system.tag.getTagValue("[System]Gateway/CurrentDateTime")

# The script was reading the same value for this so why read again?
start_time = stop_time

# Doing the write after the read
system.tag.write("gateway/last_cycle_start", start_time)
system.tag.write("gateway/last_cycle_stop", stop_time)

uname = system.tag.getTagValue("gateway/last_user_start")
system.db.runPrepUpdate("INSERT INTO rundata (run, start, stop, username) " + "VALUES (?,?,?,?)", ["0",start_time, stop_time, uname])
```

---

<div class="post-metadata">

**Author:** ![kwj](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@kwj](https://forum.inductiveautomation.com/u/kwj)\
**Post date:** [October 23, 2014, 1:30pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/6 "2014-10-23T13:30:09Z")

</div>

Thanks! I’m going to try the timestamp keyword. Good idea.

In my defense, the stop\_time normally uses different logic, I just changed it for testing.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [October 23, 2014, 1:50pm UTC](https://forum.inductiveautomation.com/t/database-datetime-being-stored-as-just-time/9183/7 "2014-10-23T13:50:07Z")

</div>

[quote=“kwj”]  
In my defense, the stop\_time normally uses different logic, I just changed it for testing.[/quote]

I thought that might be the case but figured it was worth mentioning.

I’ve had those "what the hell was I thinking? 😮😒 " moments before when looking at old code that I’ve written.
