If date in file name, use that date
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user