-3

I have a timestamp being extracted from an XML document in the format

2019-02-13T09:01:53.557+00:00

Does anyone know of a library to convert this to unix time or will I have to fiddle with the string?

Since I'm not generating the timestamp myself the answers I have been finding aren't helping me much. I think the format is iso8601 but I'm not certain as it seems to have a few extra digits which I'm guessing is ms resolution (since it relates to a machine tool), is this a problem?

GigaJoules
  • 53
  • 9
  • You're wanting to convert the format of the `timestamp`, correct? You can look into something like: https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format – Jaskier Feb 13 '19 at 14:34
  • I don't have the time as a dateTime object, just as a raw string with no metadata and a weird format with extra digits. – GigaJoules Feb 14 '19 at 13:10
  • I actually give up on trying to build rep on this SE. – GigaJoules Mar 21 '19 at 15:13

2 Answers2

1

Using DateTimeOffset.ToUnixTimeSeconds from .NET 4.6 and above, this should be fairly simple:

if (DateTime.TryParse("2019-02-13T09:01:53.557+00:00", out var dateTime))
{
    var unixTimestamp = ((DateTimeOffset)dateTime).ToUnixTimeSeconds();
}

If for whatever reason your string format changes, you can also do a exact parse using DateTime.TryParseExact:

if (DateTime.TryParseExact("2019-02-13T09:01:53.557+00:00", "yyyy-MM-dd'T'HH:mm:ss.fffzzz", 
    CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTime))
{
    var unixTimestamp = ((DateTimeOffset)dateTime).ToUnixTimeSeconds();
}
Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
0

Basically you should consider what a Unix timestamp is: (from Wikipedia)

It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds

So probably a solution would be parsing the string to a datetime, converting it to UTC and then calculate the seconds.

var date = DateTime.Parse("2019-02-13T09:01:53.557+00:00", null, System.Globalization.DateTimeStyles.RoundtripKind);
var unixTimestamp = (int)(date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
n0idea
  • 732
  • 1
  • 5
  • 14
  • The actual value in my case is not really that important, I just need it to be able to compare which file is the most recently added, so the subtraction of the unix epoch timestamp isn't strictly necessary in my case, I just need an integer that is larger if the date/time is later. But thanks :) – GigaJoules Feb 13 '19 at 14:40
  • Unix Timestamp is a standard point of reference system that can be used to do what you're asking and it is based on the total seconds that have elapsed since a specific datetime (set to 1/1/1970 00:00 by convention). You can use another datetime if you want, but basically the code needed to extract this value remains the same. – n0idea Feb 13 '19 at 14:46
  • I'm glad that I could help! Please mark the answer if it is the correct one! – n0idea Feb 13 '19 at 14:51
  • I'd upvote you but I don't have the rep. People seem to like downvoting fairly reasonable questions. I feel like I might as well just delete the whole post at this point. – GigaJoules Feb 14 '19 at 13:08