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?

link|improve this question

2  
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
feedback

4 Answers

up vote 1 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.

link|improve this answer
I'll play around the environ LD_PRELOAD, thanks! – Xie Jilei Dec 27 '10 at 16:48
feedback

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
link|improve this answer
feedback

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

link|improve this answer
Thanks. The task may involves other things then create files, I've changed the example to reflect this. – Xie Jilei Dec 27 '10 at 9:27
feedback

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
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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