If date in file name, use that date

This commit is contained in:
2026-02-16 19:18:34 +09:00
parent 8764595cb0
commit 94e85979f1
2 changed files with 22 additions and 2 deletions
+20
View File
@@ -1,6 +1,9 @@
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace ReceiptPDFBuilders.Helpers;
@@ -15,4 +18,21 @@ class Utilities
};
return opts;
}
public static DateOnly? CheckValidDateInString(string str)
{
// https://stackoverflow.com/a/14918404/3938401
var rgx = new Regex(@"\d{4}-\d{2}-\d{2}");
var mat = rgx.Match(str);
if (mat.Success)
{
var dtStr = mat.ToString();
string[] formats = ["yyyy-MM-dd"];
DateTime parsedDateTime;
var didWork = DateTime.TryParseExact(dtStr, formats, CultureInfo.InvariantCulture,
DateTimeStyles.None, out parsedDateTime);
return didWork ? DateOnly.FromDateTime(parsedDateTime) : null;
}
return null;
}
}
+2 -2
View File
@@ -219,11 +219,11 @@ class MainViewModel : BaseViewModel, IFontResolver
{
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath) && !filePath.EndsWith(".DS_Store"))
{
// TODO: if date in file name, pull out that date instead
var date = Utilities.CheckValidDateInString(filePath);
ReportFiles.Add(new ReportFile()
{
Title = Path.GetFileName(filePath),
ReceiptDateTime = File.GetCreationTime(filePath),
ReceiptDateTime = date.HasValue ? date.Value.ToDateTime(TimeOnly.MinValue) : File.GetCreationTime(filePath),
Notes = "",
FilePath = filePath,
});