Time Introduction to Documents
ls (1)The commands follow the file's chronological order to see the following options:
- System defaults (with-l or-t) are sorted by the time the file was modified
- -u option Sort by access time
- -C option Sort by state change time
In fact, it is
StatA sort of 3 time values below the struct.
struct stat
{
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
struct timespec st_atim; /* time of last access */
struct timespec st_mtim; /* time of last modification */
struct timespec st_ctim; /* time of last file status change */
blksize_t st_blksize; /* best I/O block size */
blkcnt_t st_blocks; /* number of disk blocks allocated */
};
Attention
StatMost members of a struct are basic system data types.
TimespecThe structure type is as follows, which is defined inlinux/time.h:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
Prior to the 2008 version of the standard, the time field was defined as
St_atime、
St_mtimeAnd
St_ctime, all of them are
time_tType, expressed in seconds.and then
TimespecThe structure provides a more accurate timestamp. In order to maintain compatibility, the old name can be defined as
tv_secmembers. For example,
St_atimecan be defined as St_atim.tv_sev. (Note that an e-letter is missing)
| Field |
Description |
Example |
LS (1) option |
| St_atim |
Last-access Time of File data |
Read |
-U |
| St_mtim |
Last-modification Time of File data |
Write |
Default |
| St_ctim |
Last-change Time of I-node status |
chmod, Chown |
-C |
Table 1 The 3 time value modification times associated with each file (
St_mtim) is the time the file content was last modified. State Change Time (
St_ctim) is the last time the file's I node was modified. The access and modification times of a file for functions Futimens, Utimensat, and utimes can be changed with the following functions.
FutimensAnd
Utimensatfunction to specify the timestamp of the nanosecond-level precision.
#include <sys/stat.h>
int futimens (int fd, const struct TIMESPEC times [2]);
int Utimensat (int fd, const char *path, const struct TIMESPEC times [2], int flag);
Both return:0 if OK,? 1 On Error |
TimesThe first element of the array parameter contains the access time, and the second element contains the modification time. These two time values are calendar times. The value is the cumulative value of the number of seconds since the coordinated world (coordinated Universal time, UTC) January 1, 1970, 00:00:00 this particular period. The earlier manual referred to UTC as Greenwich Mean time.
FutimensAnd
UtimensatFunctions are contained in the POSIX.1, and the following
UtimesThe function is in the XSI extension option of single UNIX specification.
#include <sys/time.h>
int utimes (const char *pathname, const struct Timeval times [2]);
returns:0 if OK,? 1 On Error |
Utimesfunction to manipulate the path name.
The Times parameter points to an array of two timestamp (Access time and modification time) elements, with two timestamps expressed in seconds and subtlety.
struct timeval {
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
Note that we cannot change the state of time
St_ctim(The time that the I node was recently modified) specifies a value because the calling
Utimesfunction, this field is automatically updated. In some versions of UNIX,
Touch (1)command to use one of these functions. Instance using the band
O_truncOptions for
OpenThe function truncates the file length to 0, but does not change its access time. To do this, first use
Statfunction to get these times, then truncate the file, and finally use the
FutimensThe function resets these two times.
/**
* 文件内容:使用带O_TRUNC选项的open函数将文件长度截断为0,但并不更改其访问时间
* [email protected]
* 时间:2016年 11月 04日 星期五 22:00:14 CST
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd = 0;
struct stat statbuf;
struct timespec times[2];
int i = 0;
for (i = 1; i < argc; i++)
{
if (stat(argv[i], &statbuf) < 0)
{
printf("%s: stat error: %s\n", argv[1], strerror(errno));
continue;
}
if (fd = open(argv[i], O_RDWR | O_TRUNC) < 0)
{
printf("%s: open error: %s\n", argv[1], strerror(errno));
continue;
}
times[0] = statbuf.st_atim;
times[1] = statbuf.st_mtim;
if (futimens(fd, times) < 0)
{
printf("%s: futimens error: %s\n", argv[1], strerror(errno));
}
if (fd > 0)
{
close(fd);
}
}
exit(0);
}
The Code 1 Futimens function Instance program runs as follows:
$
ls-l Test test.c<------------view length and last modified time
-rwxrwxr-x 1 fireway fireway 9813 August 18:14 test
-rw-rw-r--1 fireway fireway 475 August 18:04 test.c
$
ls-lu Test test.c<-------------view last accessed time
-rwxrwxr-x 1 fireway fireway 9813 August 18:14 test
-rw-rw-r--1 Fireway fireway 475 September 9 22:44 test.c
$
Date<---------------date of the day of printing
Thursday, November 03, 2016 07:54:21 CST
$
a.out Test test.c<----------run the above program
$
ls-l Test test.c<--------------inspection results
-rwxrwxr-x 1 fireway fireway 0 November 3 07:55 test
-rw-rw-r--1 fireway fireway 0 November 3 07:55 test.c
$
ls-lu Test test.c<----------------check last access time
-rwxrwxr-x 1 fireway fireway 0 August 18:14 Test
-rw-rw-r--1 fireway fireway 0 September 9 22:44 test.c
$
LS-LC Test test.c<--------------check status change time
-rwxrwxr-x 1 fireway fireway 0 November 3 07:55 test
-rw-rw-r--1 fireway fireway 0 November 3 07:55 test.c
The last access time is not modified, and the time of modification and state change becomes more time for the program to run. Refer to Advanced Programming for UNIX Environments (third edition) 4.20 functions Futimens, Utimensat, and Utimes
The time of the file