ch09_02.htm
来自「By Tom Christiansen and Nathan Torkingto」· HTM 代码 · 共 416 行
HTM
416 行
<HTML><HEAD><TITLE>Recipe 9.1. Getting and Setting Timestamps (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:39:04Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch09_01.htm"TITLE="9. Directories"><LINKREL="prev"HREF="ch09_01.htm"TITLE="9.0. Introduction"><LINKREL="next"HREF="ch09_03.htm"TITLE="9.2. Deleting a File"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_01.htm"TITLE="9.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch09_01.htm"TITLE="9. Directories"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_03.htm"TITLE="9.2. Deleting a File"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.2. Deleting a File"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch09-chap09_getting_0">9.1. Getting and Setting Timestamps</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-238">Problem<ACLASS="indexterm"NAME="ch09-idx-1000004351-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004351-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004351-2"></A><ACLASS="indexterm"NAME="ch09-idx-1000004351-3"></A></A></H3><PCLASS="para">You need to retrieve or alter when a file was last modified (written or changed) or accessed (read).</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-244">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">stat</CODE><ACLASS="indexterm"NAME="ch09-idx-1000004361-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004361-1"></A> to get those times and <CODECLASS="literal">utime</CODE> to set them. Both functions are built into Perl:</P><PRECLASS="programlisting">($READTIME, $WRITETIME) = (stat($filename))[8,9];utime($NEWREADTIME, $NEWWRITETIME, $filename);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-256">Discussion</A></H3><PCLASS="para">As explained in the Introduction, three different times are associated with an inode in the traditional Unix filesystem. Of these, any user can set the <CODECLASS="literal">atime</CODE> and <CODECLASS="literal">mtime</CODE> with <CODECLASS="literal">utime</CODE>, assuming the user has write access to the parent directory of the file. There is effectively no way to change the <CODECLASS="literal">ctime</CODE>. This example shows how to call <CODECLASS="literal">utime</CODE>:</P><PRECLASS="programlisting">$SECONDS_PER_DAY = 60 * 60 * 24;($atime, $mtime) = (stat($file))[8,9];$atime -= 7 * $SECONDS_PER_DAY;$mtime -= 7 * $SECONDS_PER_DAY;utime($atime, $mtime, $file) or die "couldn't backdate $file by a week w/ utime: $!";</PRE><PCLASS="para">You must call <CODECLASS="literal">utime</CODE> with both <CODECLASS="literal">atime</CODE> and <CODECLASS="literal">mtime</CODE> values. If you only want to change one, you must call <CODECLASS="literal">stat</CODE> first to get the other:</P><PRECLASS="programlisting">$mtime = (stat $file)[9];utime(time, $mtime, $file);</PRE><PCLASS="para">This is easier to understand if you use File::stat:</P><PRECLASS="programlisting">use File::stat;utime(time, stat($file)->mtime, $file);</PRE><PCLASS="para">Use <CODECLASS="literal">utime</CODE> to make it appear as though you never touched a file at all (beyond its <CODECLASS="literal">ctime</CODE> being updated). For example, to edit a file, use the program in <ACLASS="xref"HREF="ch09_02.htm#ch09-35447"TITLE="uvi">Example 9.1</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch09-35447">Example 9.1: uvi</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch09-idx-1000004486-0"></A>uvi - vi a file without changing its access times$file = shift or die "usage: uvi filename\n";($atime, $mtime) = (stat($file))[8,9];system($ENV{EDITOR} || "vi", $file);utime($atime, $mtime, $file) or die "couldn't restore $file to orig times: $!";</PRE></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-pgfId-306">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">stat</CODE> and <CODECLASS="literal">utime</CODE> functions in <ICLASS="filename">perlfunc </I>(1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the standard File::stat module (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; your system's <ICLASS="filename">utime </I>(3) manpage <ACLASS="indexterm"NAME="ch09-idx-1000004353-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004353-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004353-2"></A><ACLASS="indexterm"NAME="ch09-idx-1000004353-3"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_01.htm"TITLE="9.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 9.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_03.htm"TITLE="9.2. Deleting a File"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.2. Deleting a File"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">9.0. Introduction</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">9.2. Deleting a File</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + =
减小字号Ctrl + -
显示快捷键?