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?
Is it Unix timestamp incremental?
timestamps
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 1s 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.