Reputation: 1339
I'm trying to convert this date: Sunday 26 August 2018 18:30:00
to a GMT +2
, so the result should be: Today, 26 Aug 2018, 20:30
.
Now the following code works:
var unixTimeStampValue = Convert.ToInt64(1535308200);
var unixTimeStamp = unixTimeStampValue.Split("-")[0].Replace("t", "");
unixTimeStamp = Regex.Match(unixTimeStamp, @"\d+").Value;
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(Convert.ToInt64(unixTimeStamp));
TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
var date = TimeZoneInfo.ConvertTimeFromUtc(dtDateTime, timeInfo);
but the problem is that if I run this on linux I'll get:
System.IO.FileNotFoundException: The time zone ID 'Central Europe Standard Time' was not found on the local computer --> System.IO.FileNotFoundException: Could not find file '/usr/share/zoneinfo/Central Europe Standard Time'.
The problem appear only on linux, how can I fix that? There is another way to get this conversion?
Upvotes: 1
Views: 280
Reputation: 1500505
As requested, an example with Noda Time:
using System;
using NodaTime;
class Test
{
static void Main()
{
Instant instant = Instant.FromUnixTimeSeconds(1535308200);
DateTimeZone zone = DateTimeZoneProviders.Tzdb["Europe/Budapest"];
ZonedDateTime zoned = instant.InZone(zone);
Console.WriteLine(zoned);
}
}
Output on my machine:
2018-08-26T20:30:00 Europe/Budapest (+02)
You can convert the ZonedDateTime
back to a DateTime
if you need to, but it's best to use Noda Time for as much of your date/time handling as you can, to get the full benefit.
Upvotes: 1