0

I need to parse a file that contains timestamps such as: "09.06.2019 00:00:00". The person that provides those files told me that those timestamps are in the "CET" time zone which is UTC+1 (no DST). How can I parse the timestamps into corresponding DateTime objects? The only possible solution that came to my mind so far is to parse them as if they were UTC and then manually add 1 hour to the DateTime object but is that really a good solition?

Thanks a lot in advance!

Chris
  • 1,417
  • 4
  • 21
  • 53
  • What do these files look like? What have you tried to solve this problem? – RoadRunner Oct 14 '19 at 07:43
  • Is it guaranteed that there will be no DST? – Salman A Oct 14 '19 at 08:02
  • https://stackoverflow.com/questions/5615538/parse-a-date-string-into-a-certain-timezone-supporting-daylight-saving-time – Dmitry Bychenko Oct 14 '19 at 08:05
  • @RoadRunner The files are csv files that contain timestamps in the first column and measurements (floating point values) in the following columns where each column represents a measuring point. So far I tried using DateTime.ParseExact() with the format "G" and the culture "de-DE" but this does not work because of DST. – Chris Oct 14 '19 at 08:48
  • @SalmanA I don't know. The information I got was that the time zone used is "CET" during the entire year and if that is true then there will be no DST. I am simply gonna assume that for now. – Chris Oct 14 '19 at 08:49

3 Answers3

2

Use DateTimeOffset along with TimeZoneInfo to convert the date string into a DateTimeOffset with the offset for Central European Time.

string dateString = "09.06.2019 00:00:00";
// get timezone for Central European Time
TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
// parse to local DateTimeOffset
var localDateTime = DateTimeOffset.ParseExact(dateString, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);
// get the UTC Offset value
var utcOffset = cet.GetUtcOffset(localDateTime.DateTime);
// convert to DateTimeOffset with CET timezone offset
var dateTimeWithTimeZone = new DateTimeOffset(localDateTime.DateTime, utcOffset);

// result dateTimeWithTimeZone.ToString()
// 12/09/2019 00:00:00 +01:00

This method will correctly handle Daylight Savings (DST).

haldo
  • 14,512
  • 5
  • 46
  • 52
1

Do your future self a favour, and use DateTimeOffset (instead of DateTime) when dealing with timezones.

Microsoft has a whole article explaining the different date/time related structures.

As far as parsing to TimeZoneOffset, there's a few different ways but the most straightforward one is to just use ParseExact() or TryParseExact().

var dateStr =  "09.06.2019 00:00:00";

var success = DateTimeOffset.TryParseExact(
        dateStr + " +01:00", // Append the desired timezone to the string
        "dd.MM.yyyy HH:mm:ss zzz", // The format to parse, including Timezone in the end
        null, 
        DateTimeStyles.None, // Strict style. You can also specify how tolerant it is to whitespace
        out DateTimeOffset result // Store it in new variable
    );

if (success)
{
    // Manipulate into DateTime of different zones.
    Debug.WriteLine(result.DateTime);       // 12am of 09 June 2019
    Debug.WriteLine(result.UtcDateTime);    // 11am the previous day, because result is in UTC+1 timezone
    Debug.WriteLine(result.LocalDateTime);  // Converted to your local timezone
    // You could also pretty much convert into any other zones
    // using the ToOffset() method.
}
NPras
  • 3,135
  • 15
  • 29
0

You could use:

DateTime dateTime = new DateTime(2010, 1, 1, 0, 1, 0,DateTimeKind.Unspecified);
dateTime=TimeZoneInfo.ConvertTimeToUtc(dateTime, 
    TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"));

this could be used to take the dates from the file and save them as DateTime Objects in Utc time.