The time of the file

Source: Internet
Author: User

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.
  
 
  1. struct stat
  2. {
  3. mode_t st_mode; /* file type & mode (permissions) */
  4. ino_t st_ino; /* i-node number (serial number) */
  5. dev_t st_dev; /* device number (file system) */
  6. dev_t st_rdev; /* device number for special files */
  7. nlink_t st_nlink; /* number of links */
  8. uid_t st_uid; /* user ID of owner */
  9. gid_t st_gid; /* group ID of owner */
  10. off_t st_size; /* size in bytes, for regular files */
  11. struct timespec st_atim; /* time of last access */
  12. struct timespec st_mtim; /* time of last modification */
  13. struct timespec st_ctim; /* time of last file status change */
  14. blksize_t st_blksize; /* best I/O block size */
  15. blkcnt_t st_blocks; /* number of disk blocks allocated */
  16. };
Attention StatMost members of a struct are basic system data types. TimespecThe structure type is as follows, which is defined inlinux/time.h:
 
   
  
  1. struct timespec {
  2. time_t tv_sec; /* seconds */
  3. long tv_nsec; /* nanoseconds */
  4. };
Prior to the 2008 version of the standard, the time field was defined as St_atimeSt_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.
 
   
  
  1. struct timeval {
  2. time_t tv_sec; /* seconds */
  3. long tv_usec; /* microseconds */
  4. };
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.
  
 
  1. /**
  2. * 文件内容:使用带O_TRUNC选项的open函数将文件长度截断为0,但并不更改其访问时间
  3. * [email protected]
  4. * 时间:2016年 11月 04日 星期五 22:00:14 CST
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. int main(int argc, char *argv[])
  15. {
  16. int fd = 0;
  17. struct stat statbuf;
  18. struct timespec times[2];
  19. int i = 0;
  20. for (i = 1; i < argc; i++)
  21. {
  22. if (stat(argv[i], &statbuf) < 0)
  23. {
  24. printf("%s: stat error: %s\n", argv[1], strerror(errno));
  25. continue;
  26. }
  27. if (fd = open(argv[i], O_RDWR | O_TRUNC) < 0)
  28. {
  29. printf("%s: open error: %s\n", argv[1], strerror(errno));
  30. continue;
  31. }
  32. times[0] = statbuf.st_atim;
  33. times[1] = statbuf.st_mtim;
  34. if (futimens(fd, times) < 0)
  35. {
  36. printf("%s: futimens error: %s\n", argv[1], strerror(errno));
  37. }
  38. if (fd > 0)
  39. {
  40. close(fd);
  41. }
  42. }
  43. exit(0);
  44. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.