File Timestamp wrong when writing it to DateTime Tag

Can somebody explain this to me: :thinking:

I read the time of the last modification of the file and write it to the DateTime Memory Tag.
But the value in the tag is wrong. It is two hours ahead… Why?

Try with Java’s File object and related infrastructure.

1 Like

Thanks, @pturmel.
I ended up with this:

import os
import datetime
from import File
from java.text import SimpleDateFormat

path = "c:\\temp\\test\\wrapper.log"
logfile = File(path)
t = logfile.lastModified()
print "t:", 
sdf = SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
modified = sdf.format(t)
print "modified: ", modified
path = ["[realtimeBulky1]datetime"]
value = [modified]
system.tag.writeBlocking(path, value)

Now the tag value it’s OK.
But I still don’t understand why…?

It’s because fromtimestamp() returns None for the timezone in the datetime object.

You can also write the timestamp in millis directly to your tag.

import os

path = 'C:/path/to/file'

# Get modified time of the file in milliseconds
t = long(os.path.getmtime(path)) * 1000

tagPath = ['[default]Test/datetime']
value = [t]

system.tag.writeBlocking(tagPath, value)

Very good, @JordanCClark.
I ended up with your ‘solution’… :+1:

1 Like