up
This commit is contained in:
		
							
								
								
									
										43
									
								
								src/StellaOps.Scanner.Core/Utility/ScannerTimestamps.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/StellaOps.Scanner.Core/Utility/ScannerTimestamps.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
using System.Globalization;
 | 
			
		||||
 | 
			
		||||
namespace StellaOps.Scanner.Core.Utility;
 | 
			
		||||
 | 
			
		||||
public static class ScannerTimestamps
 | 
			
		||||
{
 | 
			
		||||
    private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
 | 
			
		||||
 | 
			
		||||
    public static DateTimeOffset Normalize(DateTimeOffset value)
 | 
			
		||||
    {
 | 
			
		||||
        var utc = value.ToUniversalTime();
 | 
			
		||||
        var ticks = utc.Ticks - (utc.Ticks % TicksPerMicrosecond);
 | 
			
		||||
        return new DateTimeOffset(ticks, TimeSpan.Zero);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static DateTimeOffset UtcNow(TimeProvider? provider = null)
 | 
			
		||||
        => Normalize((provider ?? TimeProvider.System).GetUtcNow());
 | 
			
		||||
 | 
			
		||||
    public static string ToIso8601(DateTimeOffset value)
 | 
			
		||||
        => Normalize(value).ToString("yyyy-MM-dd'T'HH:mm:ss.ffffff'Z'", CultureInfo.InvariantCulture);
 | 
			
		||||
 | 
			
		||||
    public static bool TryParseIso8601(string? value, out DateTimeOffset timestamp)
 | 
			
		||||
    {
 | 
			
		||||
        if (string.IsNullOrWhiteSpace(value))
 | 
			
		||||
        {
 | 
			
		||||
            timestamp = default;
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (DateTimeOffset.TryParse(
 | 
			
		||||
                value,
 | 
			
		||||
                CultureInfo.InvariantCulture,
 | 
			
		||||
                DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
 | 
			
		||||
                out var parsed))
 | 
			
		||||
        {
 | 
			
		||||
            timestamp = Normalize(parsed);
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        timestamp = default;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user