Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to test aspects of my software that only happen at certain times of the day. Rather than waiting whole days (and getting here at 2:00 AM), I'd like to change the time.

But I'd rather not change it permanently.

I know I can change the time using date, and then change it back again, but is there a better way?

OS in question is RHEL6 running in a VM.

share|improve this question
    
For what it's worth, I had trouble temporarily changing the time inside my VM, but I didn't have any problem with a stand-alone system. It might be worth reading this: Disable Time Synchronization on Ubuntu –  GrandAdmiral Oct 21 '13 at 20:36

2 Answers 2

There's a library called libfaketime which allows you to make the system report a given time to your application. You can either have the system report a fixed time for the duration of the program execution, or start the clock at some specific time (for example, 01:59:30). Basically, you hook the faketime library into your program's in-memory image through the library loader, and it captures and handles in its own way all system calls which relate to system time. It doesn't exactly change the system time, but it changes what time is reported to your specific application without affecting anything else that is running, which is probably what you really are after (otherwise, I see no reason to not just change the global system time).

There's a number of possible variants on how to use it, but it looks like Changing what time a process thinks it is with libfaketime has a pretty thorough listing along with sample code to try them out. Google should also be able to unearth some examples given that you know what to search for.

It would appear that it isn't available prepackaged through the RHEL repositories, but for example Debian provides it under the package name faketime. It also looks straight forward to build from source code (it apparently doesn't even need a configure step or anything like that).

share|improve this answer
1  
There isn't a prebuild of this in any Fedora/CentOS/RHEL repos that I could find. There is this RPM available here but it will need to be rebuilt for CentOS. –  slm Aug 2 '13 at 16:49
    
@slm Pity, that, but it shouldn't be hard to build from source (said without having tried). –  Michael Kjörling Aug 2 '13 at 16:50
1  
Nope I was just letting everyone know to save them from looking. It looks pretty straight forward. –  slm Aug 2 '13 at 16:52

What I would do is first install an ntp service to synchronize time with a remote server. Then, stop the service, manually change the date, play around and restart the service:

service ntpd stop
date -s "Aug 11 2012"
[ do stuff ]
service ntpd start
share|improve this answer
    
"As far as I know, you cannot change the time for only one process because it is a system-wide thing." Actually, you can, in a way; see my answer. –  Michael Kjörling Aug 2 '13 at 16:45
2  
@MichaelKjörling indeed, but my knowledge at the time of writing did not extend to your answer. As far as I knew there is no way :). –  terdon Aug 2 '13 at 17:27
    
I posted my answer approximately 90 seconds before you posted yours. ;) –  Michael Kjörling Aug 2 '13 at 17:41
1  
@MichaelKjörling yup, and I actually edited mine (you can't see it, it was done right after posting when I say yours) to add the "as far as I know" :). –  terdon Aug 2 '13 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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