2

I have a simple Python program that collect data from a sensor. The program will then send the data together with the system's timestamp to a remote server via REST POST request.

If the POST request fail, the data together with the timestamp will be saved into a CSV file. Once Internet connection is available again, data on the CSV file will be sent to the remote server.

On the remote server, I have a simple PHP app that will generate a graph from the collected data.

My question: is Unix timestamp incremental and unique? Is it safe to sort the timestamp in ascending order to see the sequence of data collected?

  • 1
    If by timestamp you mean epoch, then yes. Number of seconds since a given date/time would only increase. – devnull May 23 '14 at 10:37
  • 1
    But it isn't necessarily unique, if you could retry within the same second (unless you're using Python's fractional time, which is probably fine). – Useless May 23 '14 at 10:40
  • 2
    big adjustments of the clock (like when it is off by several seconds) may cause that value to go backward, but generally time is synchronised by adjusting the speed of the clock, those big adjustments would only occur on boot or possibly when the hardware clock is deffective. – Stéphane Chazelas May 23 '14 at 10:55
  • Can you show us the timestamps in question along with some code? – slm May 23 '14 at 11:56
1

There is no guarantee that timestamps are incremental. The normal way to get the system time is gettimeofday which provides no guarantee at all.

In practice, gettimeofday will be monotonic unless the system administrator or a configuration error causes the system clock to change. gettimeofday has at best a microsecond resolution. This doesn't guarantee that the value will be updated if between two calls to gettimeofday that are more than 1µs apart, but the longer the interval between calls, the more likely the value is to have changed.

Some systems support a monotonic clock, read by calling clock_gettime with the clock id CLOCK_MONOTONIC. Python provides access to this time via time.CLOCK_MONOTONIC since version 3.3. This clock is only monotonic between reboots, and there is no guarantee that successive calls return distinct values, though both properties are highly likely.

If you want an absolute guarantee that some field is incremental, maintain a counter in a separate field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.