Merge pull request #680 from ente-io/fix_date_time

Fix incorrect date parsing from filename
This commit is contained in:
Neeraj Gupta 2022-12-06 14:06:13 +05:30 committed by GitHub
commit ca1eab1f89
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 26 deletions

View file

@ -45,7 +45,7 @@ Map<int, String> _days = {
7: "Sun",
};
final currentYear = int.parse(DateTime.now().year.toString());
final currentYear = DateTime.now().year;
const searchStartYear = 1970;
//Jun 2022
@ -267,30 +267,18 @@ bool isValidDate({
return true;
}
@Deprecated("Use parseDateTimeV2 ")
DateTime? parseDateFromFileName(String fileName) {
if (fileName.startsWith('IMG-') || fileName.startsWith('VID-')) {
// Whatsapp media files
return DateTime.tryParse(fileName.split('-')[1]);
} else if (fileName.startsWith("Screenshot_")) {
// Screenshots on droid
return DateTime.tryParse(
(fileName).replaceAll('Screenshot_', '').replaceAll('-', 'T'),
);
} else {
return DateTime.tryParse(
(fileName)
.replaceAll("IMG_", "")
.replaceAll("VID_", "")
.replaceAll("DCIM_", "")
.replaceAll("_", " "),
);
}
}
final RegExp exp = RegExp('[\\.A-Za-z]*');
DateTime? parseDateTimeFromFileNameV2(String fileName) {
DateTime? parseDateTimeFromFileNameV2(
String fileName, {
/* to avoid parsing incorrect date time from the filename, the max and min
year limits the chances of parsing incorrect date times
*/
int minYear = 1990,
int? maxYear,
}) {
// add next year to avoid corner cases for 31st Dec
maxYear ??= currentYear + 1;
String val = fileName.replaceAll(exp, '');
if (val.isNotEmpty && !isNumeric(val[0])) {
val = val.substring(1, val.length);
@ -319,7 +307,10 @@ DateTime? parseDateTimeFromFileNameV2(String fileName) {
if (kDebugMode && result == null) {
debugPrint("Failed to parse $fileName dateTime from $valForParser");
}
return result;
if (result != null && result.year >= minYear && result.year <= maxYear) {
return result;
}
return null;
}
bool isNumeric(String? s) {

View file

@ -9,14 +9,14 @@ void main() {
"IMG-20221109-WA0000",
'''Screenshot_20220807-195908_Firefox''',
'''Screenshot_20220507-195908''',
"2019-02-18 16.00.12-DCMX.png",
"2022-02-18 16.00.12-DCMX.png",
"20221107_231730",
"2020-11-01 02.31.02",
"IMG_20210921_144423",
"2019-10-31 155703",
"IMG_20210921_144423_783",
"Screenshot_2022-06-21-16-51-29-164_newFormat.heic",
"Screenshot 20221106 211633.com.google.android.apps.nbu.paisa.user.jpg"
"Screenshot 20221106 211633.com.google.android.apps.nbu.paisa.user.jpg",
];
for (String val in validParsing) {
final parsedValue = parseDateTimeFromFileNameV2(val);
@ -31,6 +31,21 @@ void main() {
}
});
test("test invalid datetime parsing", () {
final List<String> badParsing = ["Snapchat-431959199.mp4."];
for (String val in badParsing) {
final parsedValue = parseDateTimeFromFileNameV2(val);
expect(
parsedValue == null,
true,
reason: "parsing should have failed $val",
);
if (kDebugMode) {
debugPrint("Parsed $val as ${parsedValue?.toIso8601String()}");
}
}
});
test("verify constants", () {
final date = DateTime.fromMicrosecondsSinceEpoch(jan011981Time).toUtc();
expect(