What does HackerNews think of libfaketime?
libfaketime modifies the system time for a single application
Language:
C
#48
in
Hacktoberfest
I can recommend libfaketime regarding this topic.
It can bei used to change the system time for a single application only.
Not a mock obviously but in the interest of getting things done on schedule I’d suggest running tests in a container with https://github.com/wolfcw/libfaketime and orchestrating edge cases like leap seconds external to the test suite itself.
One way is to override the libc time functions using LD_PRELOAD using something like https://github.com/wolfcw/libfaketime
This doesn't work with everything (for example doesn't work with Go because Go doesn't use libc for such things) but if it does then it's a very simple to use:
% LD_PRELOAD=src/libfaketime.so.1 FAKETIME='1999-12-31 23:59:59' date
Fri Dec 31 23:59:59 PST 1999
% LD_PRELOAD=src/libfaketime.so.1 FAKETIME='1999-12-31 23:59:59' python -c 'import time;print(time.ctime())'
Fri Dec 31 23:59:59 1999
or to have the fake clock ticking starting from the specified time: % LD_PRELOAD=src/libfaketime.so.1 FAKETIME='@1999-12-31 23:59:59' python -c 'import time;print(time.ctime());time.sleep(1);print(time.ctime())'
Fri Dec 31 23:59:59 1999
Sat Jan 1 00:00:00 2000
You don’t have to write that code yourself, you can just use libfaketime https://github.com/wolfcw/libfaketime
There's a well-known libfaketime library, that can forge the current system time.
https://github.com/wolfcw/libfaketime
Here's my friend's LD_PRELOAD hack, it pushes the idea further: hooking gettimeofday() to make a program to think that the time goes faster or slower. Useful for testing.
It's worth mentioning that libfaketime [1] is a more mature alternative with macOS support and more complete coverage of the relevant system calls. Nothing against the current project but that might be a better choice for many people.
You can use `libfaketime` to test your software:
* https://github.com/wolfcw/libfaketime
Shameless plug: my project `fluxcapacitor` may also be useful for testing:
See also libfaketime, which does all this and more besides: https://github.com/wolfcw/libfaketime