-
Notifications
You must be signed in to change notification settings - Fork 804
Expand file tree
/
Copy pathTimestampHelper.cs
More file actions
57 lines (49 loc) · 2.56 KB
/
TimestampHelper.cs
File metadata and controls
57 lines (49 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Globalization;
using System.IO;
namespace NETworkManager.Utilities;
public static class TimestampHelper
{
public static string GetTimestamp()
{
return DateTime.Now.ToString("yyyyMMddHHmmss");
}
/// <summary>
/// Generates a filename by prefixing the specified filename with a timestamp string.
/// </summary>
/// <param name="fileName">The original filename to be prefixed with a timestamp. Cannot be null or empty.</param>
/// <returns>A string containing the timestamp followed by an underscore and the original filename.</returns>
public static string GetTimestampFilename(string fileName)
{
return $"{GetTimestamp()}_{fileName}";
}
/// <summary>
/// Determines whether the specified file name begins with a valid timestamp in the format "yyyyMMddHHmmss".
/// </summary>
/// <remarks>This method checks only the first 14 characters of the file name for a valid timestamp and
/// does not validate the remainder of the file name.</remarks>
/// <param name="fileName">The file name to evaluate. The file name is expected to start with a 14-digit timestamp followed by an
/// underscore and at least one additional character.</param>
/// <returns>true if the file name starts with a valid timestamp in the format "yyyyMMddHHmmss"; otherwise, false.</returns>
public static bool IsTimestampedFilename(string fileName)
{
// Ensure the filename is long enough to contain a timestamp, an underscore, and at least one character after it
if (fileName.Length < 16)
return false;
var timestampString = fileName.Substring(0, 14);
return DateTime.TryParseExact(timestampString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
}
/// <summary>
/// Extracts the timestamp from a filename that starts with a timestamp prefix.
/// </summary>
/// <remarks>Filenames are expected to start with yyyyMMddHHmmss_* format (14 characters).
/// This method extracts the timestamp portion and parses it as a DateTime.</remarks>
/// <param name="fileName">The full path to the file or just the filename.</param>
/// <returns>The timestamp extracted from the filename.</returns>
public static DateTime ExtractTimestampFromFilename(string fileName)
{
// Extract the timestamp prefix (yyyyMMddHHmmss format, 14 characters)
var timestampString = fileName.Substring(0, 14);
return DateTime.ParseExact(timestampString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}
}