Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
How to create a file with specific date and time

Posted by hugo_owen (hugo_owen), 25 July 2005
Like in Unix using the touch cmd I just want to create a file and be able to set the time attributes on that file - this will allow me to then search for any files newer than this one (in the last hour) to then replcate to another machine.


Posted by admin (Graham Ellis), 25 July 2005
You're looking for the utime function. It differs from the Unix touch command in that it will not create the file for you in the first place - if the file does not exist, you should do that first (open for write then close is probably easiest).

To quote the manual ...

"[utime] Changes the access and modification times on each file of a list of files.  The first two elements of the list must be the NUMERICAL access and modification times, in that order.  Returns the number of files successfully changed.  The inode change time of each file is set to the current time."

Example - timestamping a list of existing files to the current date and time, file names given on the command line:

$now = time;
utime $now, $now, @ARGV;

Posted by hugo_owen (hugo_owen), 26 July 2005
Thanks Graham, I see what this does - effectivley touch'ing every file. But if I just want to change the date/time on a file. e.g have a file called test.gz and want to change it's date to 10am on the 25 july 2005.

Posted by admin (Graham Ellis), 26 July 2005
utime 1122285600,1122285600, "test.gz";

If you think that timestamp is really ugly (and you would be right!) you might want to make up a timestamp using the timegm function in the Time::Local module that's standard with the Perl distribution.   Here's an improved piece of code:

use Time::Local;
$when = timegm(0,0,10,25,6,5);
utime $when, $when, "test.gz";

Note - this is GMT rather than local time (use timelocal if you need local time on the server).

Note - timegm month numbers go from 0 (January) to 11 (December) which may not be what you expect.   Year numbers go from 0 to about 35 (for 2000 to 2035) and from about 38 to 99 (for 1938 to 1999)

Posted by hugo_owen (hugo_owen), 27 July 2005
Thanks Graham for your quick responses, its much appreciated.

Excellent that works a treat - now all I need to do is to find files newer than this file like the unix "find" cmd:

find / -newer "file" -print


Posted by admin (Graham Ellis), 27 July 2005
You might like to look at - a Perl example that recursively goes through a series or directories.  Start to adapt it to your needs by using the -M operator on each of the objects traversed to see when it was last modified ...

Posted by hugo_owen (hugo_owen), 27 July 2005
Thanks Graham, but the problem with -M and File::stat is it uses modified in days etc... the reason I want to create the file at a certian time and then find files newer than this file is so I can:

run the Perl script @ 10 o'clock, this then creates a file time stamped 9 o'clock, I can hopefully run find to find any files newer than this one to then cp to a mirror location. So effectively sync two machine every hour....



Posted by admin (Graham Ellis), 27 July 2005
Those functions do indeed report a relative time ... but the stat function on which they're based calls up an absolute time.   The great beauty of Perl is that if a function doesn't do quite what you want, there's very likely to be another that does.   In this case, stat ...

Program to report on all files in or below a directory that have been modified since a started year/month/day hour/minute/second:

#%% Find all files more recent than a specified time

use Time::Local;

$hour = 9;
$minute = 15;
$second = 0;

$day = 25;
$month = 7;
$year = 2005;

@queue = ("/Users/grahamellis");

$when = timegm($second,$minute,$hour,$day,$month-1,$year%100);

while ($current = shift @queue) {
       opendir DH,$current;
       while ($symbol = readdir DH) {
               next if ($symbol =~ /^\.{1,2}$/);  # Skip current and parent directories
               $locate = $current."/".$symbol;
               next if (-l $locate);  # Don't follow links
               if (-d _) {    # save directories
                       push @queue,$locate ;
                       next ;
               $fstamp = (stat(_))[9];
               if ($fstamp > $when) {
                       $ts = gmtime($fstamp);
                       print "$ts - $locate\n";

And here's what I got when I ran it ...

earth-wind-and-fire:~ grahamellis$ perl perl_find_recent
Wed Jul 27 10:08:34 2005 - /Users/grahamellis/.lpoptions
Tue Jul 26 15:00:40 2005 - /Users/grahamellis/.mysql_history
Wed Jul 27 13:24:04 2005 - /Users/grahamellis/.viminfo
Wed Jul 27 13:24:04 2005 - /Users/grahamellis/perl_find_recent
Tue Jul 26 16:48:18 2005 - /Users/grahamellis/Desktop/Picture 12.pdf
Tue Jul 26 16:55:46 2005 - /Users/grahamellis/Desktop/Picture 13.pdf
Wed Jul 27 11:16:51 2005 - /Users/grahamellis/jul05/aap.gif

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

Comment: "I like it very much. And i have one problem in coding in ..."
Visitor Ranking 5.0 (5=excellent, 1=poor)

Comment by Dhaval (published 2010-10-25) Suggested link.
I like it very much. And i have one problem in coding in PERL language. I want to display dat and time dynamically. 1) Tue: 1:00 PM to 2:00 PM EST 2) Fri 11:00AM to 12:00PM EST every month recurring. So for that what i have to do?? [#3808]

You can Add a comment or ranking or edit your own comments

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho