2

What is the most efficient or most readable or shortest way to generate a timestamp in a range on timestamps. Basically a function that will return a timestamp.

Lets say timestamp has to be later than 1999-04-01T07:00:00Z and earlier than 2016-06-03T15:18:07Z in this exact format.

This timestamp will be then used as a url param to GET some entities created either earlier or later than this. Also, is this some certain standard of a format? Does it have name?

raitisd
  • 3,875
  • 5
  • 26
  • 37
  • Instantiate `datetime` objects, subtract them to get a `timedelta`, generate a random number between `0` and the seconds in the `timedelta`, add `timedelta(seconds=my_random_number)` to your smaller timestamp. – deceze Jul 06 '16 at 15:20
  • Possible duplicate of [Generate a random date between two other dates](http://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates) – Daerdemandt Jul 06 '16 at 15:53

1 Answers1

1

as a url param to GET some entities

It seems that you need a string of a certain format.

is this some certain standard of a format? Does it have name?

Take a look at ISO_8601. You can use .isoformat() from here to get exactly that.

Lets say timestamp has to be later than 1999-04-01T07:00:00Z and earlier than 2016-06-03T15:18:07Z in this exact format.

Shortest and most efficient way would be to simply return "1999-04-01T07:00:01Z", for example. Technically, that does the job.

However, I suppose you need something more, like returning a random new one each time or incrementing date with some step or whatnot.

EDIT: question about generating random timestamps in a given range was answered here

Community
  • 1
  • 1
Daerdemandt
  • 2,281
  • 18
  • 19
  • Ahh, thanks for the ISO_8601 reference. Still, I wanted to see what the best way to generate random values in this range. I will the use this in a performance test for an API that gets documents from solr based on when documents were created. – raitisd Jul 06 '16 at 15:37