Unambiguous DateTime representation with NodaTime -- can this be done with less ceremony?

rianjs

I have a fairly straightforward notion that I want to represent using Noda Time:

04/24/2015 4:00pm America/New_York

To create an unambiguous representation, I've done the following:

var _americaNewYork = DateTimeZoneProviders.Tzdb["America/New_York"];
var pattern = LocalDateTimePattern.CreateWithInvariantCulture("M/d/yyyy h:mmtt");
var localTime = pattern.Parse(formattedDatetime).Value;
var zonedDateTime = localTime.InZoneLeniently(_easternTime);

Is there a more concise way to do what I did above? I feel like I should be able to do this with one or two lines instead:

var unambiguous = new ZonedDateTime(string textToParse, DateTimePattern pattern, string timezone);

And maybe a few overloads if I really want to specify gap semantics when changes occur.

Jon Skeet

I'd say that what you've got is actually the best approach at the moment - you're parsing to the right type, in that what you've got in your text ("04/24/2015 4:00pm") really is a local time. (If you've actually got the "America/New_York" bit in there, you should definitely just use a ZonedDateTimePattern.)

But you can use a ZonedDateTimePattern anyway, with an appropriate resolver and template value:

var tzdb = DateTimeZoneProviders.Tzdb;
var pattern = ZonedDateTimePattern.Create(
    "M/d/yyyy h:mmtt",
    CultureInfo.InvariantCulture,
    Resolvers.LenientResolver,
    tzdb,
    NodaConstants.UnixEpoch.InZone(tzdb["America/New_York"]));
string text = "04/24/2015 4:00pm";

var zoned = pattern.Parse(text).Value;
Console.WriteLine(zoned);

Obviously that's more code to start with, but you can reuse the pattern multiple times.

As I say, I'd personally stick with what you've got as it says exactly what you're trying to do: parse a LocalDateTime and then resolve that into a particular zone.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extending a scala class with less ceremony

From Dev

Can this code be done with less media queries?

From Dev

Given an unzoned DateTime and a timezone, how can I construct an instant in NodaTime?

From Dev

Given an unzoned DateTime and a timezone, how can I construct an instant in NodaTime?

From Dev

How can I make this grammar unambiguous?

From Dev

NodaTime: Replicating DateTime.Today

From Dev

Pandas Conversion of datetime representation

From Dev

Can't get my head around creating unambiguous grammar for if else

From Dev

How can I make this AWK array matching unambiguous?

From Dev

How can I colorize head, tail and less, same as I've done with cat?

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

Convert DateTime from specific time zone to UTC using Nodatime?

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

Finding all the indexes of some elements on a given list. Can it be done in less than O(n^2) without arrays in Haskell?

From Dev

How can I use type-traits to make this array-to-pointer conversion unambiguous?

From Dev

Convert pandas datetime month to string representation

From Dev

Converting Sitecore nvarchar date representation to SQL datetime

From Dev

Can this be done with Scala macros?

From Dev

Can this be done with regex?

From Dev

Can be done more pythonic?

From Dev

Can this be done with SQLAhclemy / Python?

From Dev

Can this be done faster with numpy?

From Dev

Can this be done with STM?

From Dev

Can this be done with regex?

From Dev

MySQL Datetime Less Than Functionality

From Dev

Can't generate NodaZoneData file using NodaTime.TzdbCompiler

From Dev

Floating point representation can support larger values in comparision to integer representation

From Dev

Can not load less files

From Dev

Is LESS transformation to CSS done client-side or server-side?

Related Related

  1. 1

    Extending a scala class with less ceremony

  2. 2

    Can this code be done with less media queries?

  3. 3

    Given an unzoned DateTime and a timezone, how can I construct an instant in NodaTime?

  4. 4

    Given an unzoned DateTime and a timezone, how can I construct an instant in NodaTime?

  5. 5

    How can I make this grammar unambiguous?

  6. 6

    NodaTime: Replicating DateTime.Today

  7. 7

    Pandas Conversion of datetime representation

  8. 8

    Can't get my head around creating unambiguous grammar for if else

  9. 9

    How can I make this AWK array matching unambiguous?

  10. 10

    How can I colorize head, tail and less, same as I've done with cat?

  11. 11

    Using NodaTime to parse an input and output different dateTime formats

  12. 12

    Convert DateTime from specific time zone to UTC using Nodatime?

  13. 13

    Using NodaTime to parse an input and output different dateTime formats

  14. 14

    Finding all the indexes of some elements on a given list. Can it be done in less than O(n^2) without arrays in Haskell?

  15. 15

    How can I use type-traits to make this array-to-pointer conversion unambiguous?

  16. 16

    Convert pandas datetime month to string representation

  17. 17

    Converting Sitecore nvarchar date representation to SQL datetime

  18. 18

    Can this be done with Scala macros?

  19. 19

    Can this be done with regex?

  20. 20

    Can be done more pythonic?

  21. 21

    Can this be done with SQLAhclemy / Python?

  22. 22

    Can this be done faster with numpy?

  23. 23

    Can this be done with STM?

  24. 24

    Can this be done with regex?

  25. 25

    MySQL Datetime Less Than Functionality

  26. 26

    Can't generate NodaZoneData file using NodaTime.TzdbCompiler

  27. 27

    Floating point representation can support larger values in comparision to integer representation

  28. 28

    Can not load less files

  29. 29

    Is LESS transformation to CSS done client-side or server-side?

HotTag

Archive