Nachocoin
Nachocoin

Reputation: 38

Incrementing Timestamps

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??

Upvotes: 1

Views: 809

Answers (2)

xana
xana

Reputation: 499

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))

Upvotes: 1

Prune
Prune

Reputation: 77885

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.

Upvotes: 0

Related Questions