2

I have a string which I know is Eastern Standard Time:

"8/14/2013 5:51am"

I want to convert this to a UTC DateTime in my portable class library, taking daylight savings time into consideration. I have discovered that Portable Class Library does not have the following method:

TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")

Is this possible to do in a Portable Class Library? If not, how do I do it in a normal class library given that there is no time zone information in the string?

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • There is a convert time to utc method, did you try that – V4Vendetta Aug 14 '13 at 12:18
  • @V4Vendetta, the OP's problem is that the local time is GMT, the time of interest is EST, and the desired time is UTC. The PCL seems only to make available methods that assume times are in local time and the OP is looking for a way to say "8/14/2013 5:51am" is not local time but some other time zone. – Brad Rem Aug 14 '13 at 12:25

2 Answers2

4

TimeZoneInfo is not fully portable. It exists, but has no data to back it up, so it can only provide access to UTC and the local time zone. It can't resolve a zone by its id.

Fortunately, Noda Time has a portable version, but you will need to use the IANA zone name, not the Windows zone name. See the timezone tag wiki for more details. Also make sure you read about limitations of NodaTime as a PCL.

using NodaTime;
using NodaTime.Text;

LocalDateTimePattern pattern = LocalDateTimePattern
                                 .CreateWithInvariantCulture("M/dd/yyyy h:mmtt");
LocalDateTime ldt = pattern.Parse("8/14/2013 5:51am").Value;

DateTimeZone tz = DateTimeZoneProviders.Tzdb["America/New_York"];
ZonedDateTime zdt = tz.AtLeniently(ldt);
Instant instant = zdt.ToInstant();

Debug.WriteLine(instant.ToString());  // 2013-08-14T09:51:00Z   (UTC)

// if you need it as a DateTime
DateTime utc = instant.ToDateTimeUtc();
Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 1
    Glad it works for you. Also, be sure to read [in the docs](http://nodatime.org/1.1.x/api/html/M_NodaTime_DateTimeZone_AtLeniently.htm) about the meaning of `.AtLeniently`. You could instead use `.AtStrictly`, or you could provide your own strategy. – Matt Johnson-Pint Aug 14 '13 at 21:22
2

The PCL exposes the TimeZoneInfo structure, but fails to include the function you need which means that you'll probably have to write the time zone conversion code yourself. This shouldn't be too bad if you'll always be converting just one time zone to another, but if your source time zones are spread across the world than that will be challenging.

At first I was going to recommend Jon Skeet's, et. al., Noda Time, but they list a significant limitation and the same limitation you've discovered:

The .NET API provided for portable class libraries is more limited than the full desktop version. Currently this provides relatively few challenges for Noda Time, with one significant exception: TimeZoneInfo. While we are able to detect the local time zone's TZDB equivalent through TimeZoneInfo.StandardName instead of its ID (as we would do normally), we can't fetch arbitrary time zones by ID, nor can we ask for the adjustment rules for a particular time zone.

If Noda Time can't do, then I think that you are left with calculating it manually.

Brad Rem
  • 6,036
  • 2
  • 25
  • 50