📄 ch13_07.htm
字号:
<html><head><title>Changing Timestamps (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch13_06.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch13_08.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">13.7. Changing Timestamps</h2><p>In those rare cases when you want to lie to other programs about whena file was most recently modified or accessed, you can use the<tt class="literal">utime</tt><a name="INDEX-926" /> function to fudge the books a bit. Thefirst two arguments give the new access time and modification<a name="INDEX-927" />time,while the remaining arguments are the list of filenames to alter tothose timestamps. The times are specified in internal timestampformat (the same type of values returned from the<tt class="literal">stat</tt> function that we mentioned in <a href="ch12_01.htm">Chapter 12, "Directory Operations"</a>). </p><p>One convenient value to use for the timestamps is "rightnow", returned in the proper format by the<tt class="literal">time</tt> function. So to update all the files in thecurrent directory to look like they were modified a day ago, butaccessed just now, we could simply do this:</p><blockquote><pre class="code">my $now = time;my $ago = $now - 24 * 60 * 60; # seconds per dayutime $now, $ago, glob "*"; # set access to now, mod to a day ago</pre></blockquote><p>Of course, nothing stops you from creating a file that is arbitrarilystamped far in the future or past (within the limits of the Unixtimestamp values of 1970 to 2038, or whatever your non-Unix systemuses, until we get 64-bit timestamps). Maybe you could use this tocreate a directory where you keep your notes for that time-travelnovel you're writing.</p><p>The third timestamp (the<a name="INDEX-928" /><a name="INDEX-929" /><a name="INDEX-930" />ctimevalue) is always set to "now" whenever anything alters afile, so there's no way to set it (it would have to be reset to"now" after you set it) with the<tt class="literal">utime</tt> function. That's because it'sprimary purpose is for incremental backups: if the file's ctimeis newer than the date on the backup tape, it's time to back itup again.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch13_06.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch13_08.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">13.6. Changing Ownership</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">13.8. Using Simple Modules</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -