CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help with setting unix file timestamp in perl

 



drakal30
New User

Jan 8, 2013, 7:02 AM

Post #1 of 3 (800 views)
Help with setting unix file timestamp in perl Can't Post

I want to set a time stamp of a file to 30 minutes before the current time. I have successfully created the script to calculate the time but I can't seem to change my dummy file's timestamps correctly.

Here is my code. The output just creates the file with the current time stamp instead of the calculated one.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use POSIX;

my $file = "file";
my $now = time();
my $before1 = $now - (30*60); # 15 minutes ago
#my $before2 = $now - (3*60*60); # 3 hours ago
#my $before3 = $now - (2*24*60*60); # 2 days ago

my $timestamp = POSIX::strftime( '%Y%m%d%H%M', localtime($before1) );

open my $fh, '>', 'file'
or die "Can't create /tmp/file: $!\n";

print $fh $timestamp;

close $fh;



FishMonger
Veteran / Moderator

Jan 8, 2013, 7:46 AM

Post #2 of 3 (789 views)
Re: [drakal30] Help with setting unix file timestamp in perl [In reply to] Can't Post

Your code does not make any attempt to alter the file's timestamp.

You can use the touch system command to alter the mtime or atime, but not the ctime.

If you want the ctime to be in the past, then you'll need to change the system time before creating the file and then reset it after the file has been created. Doing this is not recommended because it could have unwanted side effects with other processes.

Why do you want to do this?


drakal30
New User

Jan 8, 2013, 7:48 AM

Post #3 of 3 (787 views)
Re: [FishMonger] Help with setting unix file timestamp in perl [In reply to] Can't Post

Thanks for the reply, I realized my newbie mistake and fixed it.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use POSIX;

my $file = "/var/tmp/testtime";
my $now = time();
my $before1 = $now - (30*60); # 15 minutes ago
#my $before2 = $now - (3*60*60); # 3 hours ago
#my $before3 = $now - (2*24*60*60); # 2 days ago

open my $fh, '>', $file
or die "Can't create /tmp/file: $!\n";
utime($before1, $before1, $file);
close $fh;


I am using the dummy file's time stamp to test against for another script.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives