1

I have a Python script converting multiple csv lists into one xml document with time stamps for each row I already have the format set as:

TS = datetime.now().isoformat(timespec='milliseconds')+ "Z"

calling TS will give me this time stamp = 2020-02-14T14:30:59.237Z

I need to reuse this variable possibly 100k times or more in my XML document and this creates the exact same time, every time I call the variable. How can I increment the time stamp by 1 millisecond every time it is called in the script??

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Nachocoin
  • 34
  • 7
  • since your are assigning a value to TS, it wont change, unless you change it. – gsb22 Feb 14 '20 at 19:43
  • "calling TS", "I call the variable" - it's not possible to call a string. Please clarify what you're trying to do. – ForceBru Feb 14 '20 at 19:48
  • I Have several places in my xml file I need to put a timestamp but I need the first timestamp to start on the date and time that I run the python script and each timestamp after that to increment by 1 millisecond – Nachocoin Feb 15 '20 at 00:08

2 Answers2

1

Maybe use something like this generator?

from datetime import datetime as dt

def getTimestamp():
    timestamp = dt.timestamp(dt.now())
    while True:
        timestamp += 1
        yield dt.fromtimestamp((timestamp)/1000).isoformat(timespec='milliseconds') + "Z"

TS = getTimestamp()

for i in range(10):
    print(next(TS))
xana
  • 499
  • 3
  • 13
  • This did increment the time stamp in the correct format but the date and Time are not even close. I would like to know how I can get the date and time to start at the correct time. im currently getting:1970-01-19T02:22:04.766Z from this – Nachocoin Feb 15 '20 at 00:02
  • I used :def getTimestamp(): timestamp = dt.timestamp(dt.now()) while True: timestamp += .314 yield dt.fromtimestamp((timestamp)/1).isoformat(timespec='milliseconds') + "Z" – Nachocoin Feb 15 '20 at 01:58
0

You seem to be confused about the difference between a variable and a function. You do not "call" a variable, unless you have specifically assigned it a value as a function.

You did not do this. Your given code calls now and isoformat, returning a string value. You put this value into TS for safekeeping. Referring multiple times to TS does not change its value: it refers to the "once and future" string value, not to any process that would update that value.

If you want to update the time stamp, do so explicitly: add a timedelta of one millisecond to it on each loop iteration.

Prune
  • 76,765
  • 14
  • 60
  • 81