# Formatting issue with gateway time

**URL:** <https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640>\
**Category:** Ignition\
**Created:** [April 2, 2020, 6:47pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640 "2020-04-02T18:47:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 2, 2020, 6:47pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/1 "2020-04-02T18:47:52Z")

</div>

Hello, I am currently working on a system that keeps track of CIP data by logging it into SQL. I am using the system.date.now() command to get the time before I log data into the SQL server. My problem is that my time isn’t always getting entered correctly. I will get several good entries but then I will get one where the time is completely off. Is there a better way to make sure my time is always correct?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 2, 2020, 8:37pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/2 "2020-04-02T20:37:36Z")

</div>

Can you give us an example of one of the incorrect times? Is there anything else going in here with your script? Is there a chance your system’s clock is actually changing?

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 2, 2020, 9:28pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/3 "2020-04-02T21:28:23Z")

</div>

So for instance we will get lets say 4 good entries and the times are reading correctly, then on the fifth one it will be two hours off. The system’s clock hasn’t changed. Is there any better option for getting time then system.date.now()?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 2, 2020, 9:33pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/4 "2020-04-02T21:33:27Z")

</div>

system.date.now() is literally just `new Date()` in Java. There only way that’s wrong is if your system clock is reporting an incorrect value.

Can you print each date out before you insert it and then compare the printed date in the logs to the one that got inserted in the database next time you notice it?

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 2, 2020, 9:39pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/5 "2020-04-02T21:39:10Z")

</div>

So we have been trying that. In Ignition the times are correct, it just seems that when they get transferred to the SQL that they change

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 2, 2020, 9:45pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/6 "2020-04-02T21:45:38Z")

</div>

We just entered in a new time log and then when I check the memory tag it showed an hour ahead

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2020, 12:30am UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/7 "2020-04-03T00:30:37Z")

</div>

How are you doing the inserts? With any script function of the form system.db.\*Prep\*(), where data is parameterized? Or the same features with named queries? Or are you constructing your queries from strings?

The latter is bad.

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 3, 2020, 1:06pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/8 "2020-04-03T13:06:34Z")

</div>

I am using mem tags to hold the data until it is ready to be put into SQL, and then running a named query to insert that data into the db

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2020, 1:09pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/9 "2020-04-03T13:09:10Z")

</div>

Are the memory tags date types, or strings? Could you show your named query? What brand of DB? What column type for the dates?

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 3, 2020, 1:13pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/10 "2020-04-03T13:13:53Z")

</div>

Some are strings, but for the times I am using date time. I am using microsoft SQL 2017. The column types are date time for the dates. Here is the script which runs the named query:

CIP\_Start = system.tag.read(‘Controller:Global/CIP 1/CIP1\_Start\_SQL’).value  
CIP\_CircuitSel = system.tag.read(‘Controller:Global/CIP 1/CIP1\_CircuitSel’).value  
T\_Stamp = system.tag.read(‘Controller:Global/CIP 1/CIP1\_TStamp’).value  
CIP\_End = system.tag.read(‘Controller:Global/CIP 1/CIP1\_End\_SQL’).value  
UniqueID = system.tag.read(‘Controller:Global/CIP 1/CIP1 UniqueID’).value  
CIP\_Number = system.tag.read(‘Controller:Global/CIP 1/CIP1\_Number’).value

if(system.tag.read(‘Controller:Global/EB/EB3-1’).value) == 1:  
system.db.runNamedQuery(“Saputo\_Fraser\_RawPast”, “Insert Data”, {“CIP\_End”:CIP\_End, “CIP\_CircuitSel”:CIP\_CircuitSel, “T\_Stamp”:T\_Stamp, “UniqueID”:UniqueID, “CIP\_Start”:CIP\_Start, “CIP\_Number”:CIP\_Number})

```
value = 0

system.tag.write('Controller:Global/EB/EB3-1', value)
```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2020, 1:17pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/11 "2020-04-03T13:17:21Z")

</div>

> [@pturmel](#):
>
> Could you show your named query?

You left this out.

Also, is your DB in a different time zone from your gateway?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 3, 2020, 1:18pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/12 "2020-04-03T13:18:56Z")

</div>

What version of Ignition are you using?

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 3, 2020, 1:19pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/13 "2020-04-03T13:19:08Z")

</div>

Here is the named query:

INSERT INTO CIP\_Data (CIP\_Start, CIP\_CircuitSel, T\_Stamp, CIP\_End, UniqueID, CIP\_Number) VALUES (:CIP\_Start, :CIP\_CircuitSel, :T\_Stamp, :CIP\_End, :UniqueID, :CIP\_Number)

The gateway and the db are in the same time zone

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2020, 1:21pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/14 "2020-04-03T13:21:07Z")

</div>

Hmm. I’m stumped. I don’t see any obvious reason for the discrepancies.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 3, 2020, 1:26pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/15 "2020-04-03T13:26:00Z")

</div>

@Tannerawbrey

> [@Kevin.Herron](#):
>
> What version of Ignition are you using?

Also - how are you transferring the data into the memory tags?

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 3, 2020, 1:53pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/16 "2020-04-03T13:53:29Z")

</div>

I am on 7.9, I am using the system.tag.write function. Basically when they start the process it loads those memory tags with the data, at the end of the process the end time is loaded into the end time memory tag and named query runs and inserts the data into SQL

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 3, 2020, 2:21pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/17 "2020-04-03T14:21:39Z")

</div>

7.9 what? If you’re not on 7.9.13 I’d upgrade to that.

You should also make sure all your system.tag.write calls are actually system.tag.writeSynchronous.

Do these 2 things and see if this keeps happening.

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 3, 2020, 2:45pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/18 "2020-04-03T14:45:06Z")

</div>

I am on 7.9.12, I can’t update at the current time but I can try the system.tag.wrtieSynchronus and see if it fixes the issue

---

<div class="post-metadata">

**Author:** ![Tannerawbrey](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tannerawbrey](https://forum.inductiveautomation.com/u/Tannerawbrey)\
**Post date:** [April 3, 2020, 5:08pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/19 "2020-04-03T17:08:45Z")

</div>

So I changed my system.tag.write calls but still the time was off when it wrote. It was supposed to be 12:53pm and instead wrote in 2:53pm

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 3, 2020, 5:14pm UTC](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640/20 "2020-04-03T17:14:06Z")

</div>

Are you writing from a client that is on different time zone from the servers?

[Next page](https://forum.inductiveautomation.com/t/formatting-issue-with-gateway-time/33640.md?page=2)
