Dart - How can I get the creationTime of a File?

2,722

Not all platforms does have a concept of file creation time. E.g. Linux does not for all file systems and the general stat() call does not provide that information.

That does not mean you cannot access what seems to be creation time. But you should not necessarily trust its value which are also documented in the Java API:

Returns the creation time. The creation time is the time that the file was created.

If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).

https://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html#creationTime()

Dart does have a similar API if you use the FileStat class which have this property:

DateTime changed

The time of the last change to the data or metadata of the file system object.

On Windows platforms, this is instead the file creation time.

https://api.dart.dev/stable/2.7.2/dart-io/FileStat/changed.html

But the data for FileStat is documented to come from the POSIX stat() system call which does not have a concept of creation timestamp of files but has the following:

    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */

Which maps to the three timestamps you can get from FileStat:

import "dart:io";

main() {
  final stat = FileStat.statSync("test.dart");
  print('Accessed: ${stat.accessed}');
  print('Modified: ${stat.modified}');
  print('Changed:  ${stat.changed}');
}

But as you can see on Linux with XFS it will return the same value for changed and modified:

[julemand101@beta ~]$ dart test.dart
Accessed: 2020-04-07 18:19:20.404
Modified: 2020-04-07 18:19:19.020
Changed:  2020-04-07 18:19:19.020

You can get a different changed time if you e.g. update inode information:

[julemand101@beta ~]$ chmod +x test.dart
[julemand101@beta ~]$ dart test.dart
Accessed: 2020-04-07 18:19:42.341
Modified: 2020-04-07 18:19:19.020
Changed:  2020-04-07 18:19:39.397

Which makes sense since the st_ctime is documented as:

The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).

https://linux.die.net/man/2/stat

So in short, you should try and see what happens for iOS and Android when using FileStat. But in short, it is difficult to write a platform independent API which gives access to differences at each platform. Especially for a platform like Linux where it is up to each file system if a feature exists or not.

Share:
2,722
Leonardo Rignanese
Author by

Leonardo Rignanese

Updated on December 19, 2022

Comments

  • Leonardo Rignanese
    Leonardo Rignanese over 1 year

    I need to get the creationTime of a File in my Flutter project but all I have from a File object is lastModified() and lastAccessed(), no trace of a method to get the DateTime of creation.

    I see that in Java it is possible: https://stackoverflow.com/a/2724009/3997782 and also in Swift: https://stackoverflow.com/a/6428757/3997782

    I could use the Flutter MethodChannel function to get that but I would like to know if there a native Dart way to get it.

    How to get the local file information, such as file creation time