-3

Can anyone help me covert the time into a format that influx db accepts. The aim is to pass time (including milliseconds) to influxdb.

Thanks

resolver101
  • 2,155
  • 11
  • 41
  • 53

2 Answers2

2

You probably need epoch time for influxDB

Try:

import datetime
import time

t = "2009-11-10T23:00:00Z"
print( time.mktime(datetime.datetime.strptime(t, "%Y-%m-%dT%H:%M:%SZ").timetuple()) )

Output:

1257874200.0
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Thanks. I need it down to the milliseconds. I'm reading sensor data and taking many readings per a second. – resolver101 Jul 02 '18 at 11:44
  • But your source only has till seconds – Rakesh Jul 02 '18 at 11:46
  • Sorry, my bad. I know from the example on InfluxDB website that that format is accepted but I need to get it down to milliseconds. Sorry for any confusion. I'm creating the time on the fly within a python script. – resolver101 Jul 02 '18 at 11:51
  • You can use `%f` if you have milliseconds. Ex: `print( time.mktime(datetime.datetime.strptime(t, "%Y-%m-%dT%H:%M:%S.%fZ").timetuple()) )` – Rakesh Jul 02 '18 at 11:58
1

In Python you can convert the string representation to a datetime object, convert that to a struct_time, then convert that to an integer representing the UNIX epoch time.

from datetime import datetime
from time import mktime

time_str = "2009-11-10T23:00:00Z"
time_fmt = "%Y-%m-%dT%H:%M:%SZ"
dt = datetime.strptime(time_str, time_fmt)
time_tuple = dt.timetuple()
epoch = mktime(time_tuple)
print(epoch)

Output:

1257912000.0
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880