Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
3  
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
add comment

5 Answers

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
add comment

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)
{
    if(t!=NULL)
        *t=fixedTime;
    return fixedTime;
}

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    if(tz!=NULL)
    {
        /* forward requests about tz to the original gettimeofday */
        if(gettimeofday_orig==NULL)
        {
            gettimeofday_orig=(gettimeofday_t) dlsym(RTLD_NEXT, "gettimeofday");
            if(gettimeofday_orig==NULL)
                return -1;
        }
        int ret=gettimeofday_orig(NULL, tz);
        if(!ret)
            return ret;
    }
    if(tv!=NULL)
    {
        tv->tv_sec=fixedTime;
        tv->tv_usec=0;
    }
    return 0;
}

int clock_gettime(clockid_t clk_id, struct timespec *tp)
{
    (void)clk_id;
    if(tp!=NULL)
    {
        tp->tv_sec=fixedTime;
        tp->tv_nsec=0;
    }
    return 0;
}

Test:

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

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
add comment

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
add comment

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
add comment

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.