-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(package_info_plus): add install time (#3434)
- Loading branch information
1 parent
ea72b63
commit 0ea0402
Showing
14 changed files
with
298 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/package_info_plus/package_info_plus/example/pubspec.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/package_info_plus/package_info_plus/lib/src/file_attribute.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'dart:ffi'; | ||
import 'dart:io'; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
import 'package:win32/win32.dart'; | ||
|
||
base class FILEATTRIBUTEDATA extends Struct { | ||
@DWORD() | ||
external int dwFileAttributes; | ||
|
||
external FILETIME ftCreationTime; | ||
|
||
external FILETIME ftLastAccessTime; | ||
|
||
external FILETIME ftLastWriteTime; | ||
|
||
@DWORD() | ||
external int nFileSizeHigh; | ||
|
||
@DWORD() | ||
external int nFileSizeLow; | ||
} | ||
|
||
class FileAttributes { | ||
final String filePath; | ||
|
||
late final DateTime? creationTime; | ||
late final DateTime? lastWriteTime; | ||
|
||
FileAttributes(this.filePath) { | ||
final (:creationTime, :lastWriteTime) = | ||
getFileCreationAndLastWriteTime(filePath); | ||
|
||
this.creationTime = creationTime; | ||
this.lastWriteTime = lastWriteTime; | ||
} | ||
|
||
static ({ | ||
DateTime? creationTime, | ||
DateTime? lastWriteTime, | ||
}) getFileCreationAndLastWriteTime(String filePath) { | ||
if (!File(filePath).existsSync()) { | ||
throw ArgumentError.value(filePath, 'filePath', 'File not present'); | ||
} | ||
|
||
final lptstrFilename = TEXT(filePath); | ||
final lpFileInformation = calloc<FILEATTRIBUTEDATA>(); | ||
|
||
try { | ||
if (GetFileAttributesEx(lptstrFilename, 0, lpFileInformation) == 0) { | ||
throw WindowsException(HRESULT_FROM_WIN32(GetLastError())); | ||
} | ||
|
||
final FILEATTRIBUTEDATA fileInformation = lpFileInformation.ref; | ||
|
||
return ( | ||
creationTime: fileTimeToDartDateTime( | ||
fileInformation.ftCreationTime, | ||
), | ||
lastWriteTime: fileTimeToDartDateTime( | ||
fileInformation.ftLastWriteTime, | ||
), | ||
); | ||
} finally { | ||
free(lptstrFilename); | ||
free(lpFileInformation); | ||
} | ||
} | ||
|
||
static DateTime? fileTimeToDartDateTime(FILETIME? fileTime) { | ||
if (fileTime == null) return null; | ||
|
||
final high = fileTime.dwHighDateTime; | ||
final low = fileTime.dwLowDateTime; | ||
|
||
final fileTime64 = (high << 32) + low; | ||
|
||
final windowsTimeMillis = fileTime64 ~/ 10000; | ||
final unixTimeMillis = windowsTimeMillis - 11644473600000; | ||
|
||
return DateTime.fromMillisecondsSinceEpoch(unixTimeMillis); | ||
} | ||
} |
Oops, something went wrong.