0

Metadata Consulting [dot] ca

 1 year ago
source link: https://metadataconsulting.blogspot.com/2023/04/CSharp-Parse-Linux-stat-command-timestamp-with-timezone.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# Parse Linux stat command timestamp with timezone




Here's a prototype on how to handle datetime formats not supported by the limits of the .NET framework's DateTime and DateTimeOffset format strings. 

This code will parse UNIX/LINUX datetime with timezone, specifically as generated by stat command.

stat .bashrc| grep Modify
Modify: 2014-03-30 23:14:47.658210121 -0500

We normally would expect to parse this format using custom DateTimeOffset format with a ...ParseExact statement.

"yyyy-MM-dd HH:mm:ss.FFFFFFFFF zzz"

On first blush, we expect this to work. A custom format should handle the input we are trying to parse. Note the 9 Fs in a row.  

But this is not supported in the .NET framework. 
This is because .NET datetime format strings will not support consuming more than 7Fs in a row. 7Fs is the limit in terms of accuracy. I was a little shocked at this. Custom formats should handle any combination and permutation thrown at it, but more importantly this is a canonical Linux timestamp! This should have been part of the test suite guys! 

So here's a way to parser stat command timestamp, but it will lose a tiny amount of accuracy.

Here's the static code 
using System;using System.Globalization; using System.Text.RegularExpressions;
					
public class Program
{
	public static void Main()
	{
		//Unix Stat CMD CANNOT BE FORMALIZED USING DATETIME and.or DATETIMEOFFSET FORMATS in .NET
		//                                         FFFFFFFFF  not accepted!
		
		string dateString = "2010-11-29 17:56:22.000000000 -0800"; 
		DateTimeOffset unixstatdt = new DateTimeOffset();  
                //                                                                123456789
		if (DateTimeOffset.TryParseExact(dateString, "yyyy-MM-dd HH:mm:ss.FFFFFFFFF zzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out unixstatdt))
		{
				Console.WriteLine("Worked parsed Unix stat datetime with time zone\n        {0}", unixstatdt.ToString ("o"));
		}
		else
		{
			Console.WriteLine("Failed to parsed Unix stat datetime");
		}
		
		//SOLUTION 
		//WE MUST TRUNCATE THE VALUE, CRUDE BUT IT WORKS down to 7 characters from 9 
		string LinuxStatCMDpattern = @"(\d{4}-\d{2}-\d{2})\s(\d{2}:\d{2}:\d{2}\.)(\d{7})\d{2}\s ([\+-]\d{4})";
                string substitution = @"$1 $2$3 $4";
                //ACCEPTED F's of length 7 max
		//                                   FFFFFFF
		string input = @"2021-03-17 08:53:39.540802643 +0100";
        RegexOptions options = RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace;
        
		Match m = Regex.Match(input, LinuxStatCMDpattern, options); 
		Regex regex = new Regex(LinuxStatCMDpattern, options);
		
		string result = string.Empty; 
		if (m.Success) 
			result = regex.Replace(input, substitution);					          
		
		Console.WriteLine("Trimmed "  + result);

		if (DateTimeOffset.TryParseExact(result, "yyyy-MM-dd HH:mm:ss.FFFFFFF zzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out unixstatdt))
		{
				Console.WriteLine("Pass 2: Success, parsed Unix stat datetime with time zone\n        {0}", unixstatdt.ToString("o"));
		}
		else
		{
			Console.WriteLine("Pass 2: Failed");
		}
		
		
	}
}

See it in action using my https://clipboardplaintextpowertool.blogspot.com/ and parsing all these datetime formats as well! 

Date Time FormatDFT Date Time Now
UTC2023-04-19T16:25:39Z
ISO-86012023-04-19T16:25:39+0000
RFC 2822Wed, 19 Apr 2023 16:25:39 +0000
RFC 850Wednesday, 19-Apr-23 16:25:39 UTC
RFC 1036Wed, 19 Apr 23 16:25:39 +0000
RFC 1123Wed, 19 Apr 2023 16:25:39 +0000
RFC 822Wed, 19 Apr 23 16:25:39 +0000
RFC 33392023-04-19T16:25:39+00:00
ATOM2023-04-19T16:25:39+00:00
COOKIEWednesday, 19-Apr-2023 16:25:39 UTC
RSSWed, 19 Apr 2023 16:25:39 +0000
W3C2023-04-19T16:25:39+00:00
YYYY-DD-MM HH:MM:SS2023-19-04 16:25:39
YYYY-DD-MM HH:MM:SS am/pm2023-19-04 04:25:39 PM
DD-MM-YYYY HH:MM:SS19-04-2023 16:25:39
MM-DD-YYYY HH:MM:SS04-19-2023 16:25:39

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK