2

Hello I am analyzing vibration data, the output form my sensors just gives my the array values and cannot export the time stamping. Its a 10 Khz data which is 0.0001 ms.

Is there a way to generate timestamp values in python with this interval.

For example: 2018-05-21 00:00:00:0001, 2018-05-21 00:00:00:0002

  • Can you post the exact format you read the data in? – user3483203 May 22 '18 at 03:45
  • Possible duplicate of [Format a datetime into a string with milliseconds](https://stackoverflow.com/questions/7588511/format-a-datetime-into-a-string-with-milliseconds) – l'L'l May 22 '18 at 03:48

1 Answers1

1

You can generate time stamp in your aforementioned format: YYYY-MM-DD hh:mm:ss:uuuuuu where u represents microseconds.

For dt being a datetime timestamp, you can use isoformat() function. Here, dt.isoformat(timespec='microseconds') will return your desired result (except that the final colon is turned into a decimal point, and that there is a 'T' between date and time).

If you really want that colon, use formatted output and fields from that datetime object:

"%4d-%02d-%02d %02d:%02d:%02d:%04d"
      %(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond // 100)

Here are some sample code with inputs and outputs:

dt = datetime(2018, 5, 21, 0, 0, 0, 100)
print(dt.isoformat(timespec='microseconds'))
print("%4d-%02d-%02d %02d:%02d:%02d:%04d"
      %(dt.year, dt.month, dt.day,
        dt.hour, dt.minute, dt.second, dt.microsecond // 100))

This outputs

2018-05-21T00:00:00.000100
2018-05-21 00:00:00:0001
  • @RajeshKumar Sugumar Do you want the first solution or the second? Does your program have to be of the form `[year]-[month]-[day] hour:minute:second:tttt` where `tttt` represent `0.1` of a millisecond? – Ṃųỻịgǻňạcểơửṩ May 22 '18 at 04:10