2

I am unit testing a python script and want to simulate the running of the script on different days of the month.

Is there any way to set the date/time for when launching the script from the command line?

I do not have access to change the system date/time, and would prefer to not hack the script with the dates I want to test for.

Cheers, Donal

Donal M
  • 1,305
  • 2
  • 11
  • 23

3 Answers3

6

Just found the solution, use a linux tool called faketime.

[10:45:03] $ faketime '2008-12-24 08:15:42' /bin/date
Wed Dec 24 08:15:42 GMT 2008
Donal M
  • 1,305
  • 2
  • 11
  • 23
1

You may probably achieve this by some LD_PRELOAD trick, but in my opinion much cleaner solution is to mock datetime.date class. For more details you may check answers to this question

Community
  • 1
  • 1
RobertT
  • 4,300
  • 3
  • 29
  • 36
0

I don't think there is a way to directly set the date/time in a shell so that the system time is unaltered but different for a sub shell

However there are two solutions

1) for a limited set of different times you can use TZ, example

TZ="GMT+6" date

2) run the python programs in a VM like Virtualbox and do set the system time. In the VM!

Vorsprung
  • 32,923
  • 5
  • 39
  • 63