Metadata Consulting [dot] ca
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.
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.
So here's a way to parser stat command timestamp, but it will lose a tiny amount of accuracy.
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 Format | DFT Date Time Now |
---|---|
UTC | 2023-04-19T16:25:39Z |
ISO-8601 | 2023-04-19T16:25:39+0000 |
RFC 2822 | Wed, 19 Apr 2023 16:25:39 +0000 |
RFC 850 | Wednesday, 19-Apr-23 16:25:39 UTC |
RFC 1036 | Wed, 19 Apr 23 16:25:39 +0000 |
RFC 1123 | Wed, 19 Apr 2023 16:25:39 +0000 |
RFC 822 | Wed, 19 Apr 23 16:25:39 +0000 |
RFC 3339 | 2023-04-19T16:25:39+00:00 |
ATOM | 2023-04-19T16:25:39+00:00 |
COOKIE | Wednesday, 19-Apr-2023 16:25:39 UTC |
RSS | Wed, 19 Apr 2023 16:25:39 +0000 |
W3C | 2023-04-19T16:25:39+00:00 |
YYYY-DD-MM HH:MM:SS | 2023-19-04 16:25:39 |
YYYY-DD-MM HH:MM:SS am/pm | 2023-19-04 04:25:39 PM |
DD-MM-YYYY HH:MM:SS | 19-04-2023 16:25:39 |
MM-DD-YYYY HH:MM:SS | 04-19-2023 16:25:39 |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK