Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example,

# clock-pause
# touch a
# touch b
# convert -draw `date` text_draw.gif
# clock-resume

Or, simply

# freeze-exec sh -c 'touch a; touch b; convert -draw `date` text_draw.gif'

The expected result is the timestamps of a and b, and the text drawn to the image, are exactly the same.

Is it possible?

share|improve this question
What is it that you're really trying to do? You can use touch after the fact to change the dates of an existing file. – Dennis Williamson Dec 27 '10 at 15:35
up vote 2 down vote accepted

On Linux, it might be possible to write a shared library that overrides time(), gettimeofday(), and clock_gettime() to use static values, and add the library to $LD_PRELOAD, which would fool many programs.

Update: Someone implemented this as datefudge .

share|improve this answer
I'll play around the environ LD_PRELOAD, thanks! – Xiè Jìléi Dec 27 '10 at 16:48

Since I had to implement @grawity's solution, I think it could be nice to share it here:

#define _GNU_SOURCE
#include <sys/types.h>
#include <dlfcn.h>
#include <stddef.h>

/* Frozen time (usual time_t, from UNIX epoch) */
const time_t fixedTime=652789800;

/* Deprecated, thus it seems not to be present in sys/types.h */
struct timezone
   int tz_minuteswest;     /* minutes west of Greenwich */
   int tz_dsttime;         /* type of DST correction */

/* Typedef for the function ptr to the original gettimeofday */
typedef int (*gettimeofday_t)(struct timeval *tv, struct timezone *tz);

/* original gettimeofday */
gettimeofday_t gettimeofday_orig = NULL;

time_t time(time_t * t)
    return fixedTime;

int gettimeofday(struct timeval *tv, struct timezone *tz)
        /* forward requests about tz to the original gettimeofday */
            gettimeofday_orig=(gettimeofday_t) dlsym(RTLD_NEXT, "gettimeofday");
                return -1;
        int ret=gettimeofday_orig(NULL, tz);
            return ret;
    return 0;

int clock_gettime(clockid_t clk_id, struct timespec *tp)
    return 0;


matteo@teoxubuntu:~/cpp/detourtest$ gcc -O3 -fPIC -Wall -Wextra -shared timedetour.c -o
matteo@teoxubuntu:~/cpp/detourtest$ LD_PRELOAD=/home/matteo/cpp/detourtest/ date
sab  8 set 1990, 12.30.00, CEST
share|improve this answer

You cannot stop the system clock. However, you can do this:

now=$(date +%s.%N)
# $now now contains a Unix timestamp at nanosecond precision
touch -d @$now a b c
convert -draw "$(date -d @$now)" text_draw.gif
share|improve this answer

You can do touch a b. You can also do touch a; touch --reference=a b.

share|improve this answer
Thanks. The task may involves other things then create files, I've changed the example to reflect this. – Xiè Jìléi Dec 27 '10 at 9:27

There is a project called faketime that does this for you. It uses the LD_PRELOAD and it uses absolute/relative times.

libfaketime intercepts various system calls which programs use to
retrieve the current date and time. It can then report faked dates and times
(as specified by you, the user) to these programs. This means you can modify
the system time a program sees without having to change the time system-wide.

libfaketime allows you to specify both absolute dates (e.g., 01/01/2004) and
relative dates (e.g., 10 days ago).

This answer is for future references as you already have a working solution.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .