4

Is there an available c# plugin that handles all the moment.js timezones.

I am having a hard time comparing the dates in the backend since the timezones from moment.js is not the same as the timezones from the .net DateTime.

I need to convert the times to Utc before comparing the different timezoned dates. I am using nodatime and it is able to handle some of the timezones but not all.

Thanks.

EDIT: NodaTime handled most of the timezones but I still need to cover the abbrevations. Is there something available for this?

Thanks.

Milo Cabs
  • 503
  • 1
  • 10
  • 24
  • Can you convert an moment.js datetime to utc? This could be parsed by nodatime- – Christian Sauer Sep 23 '14 at 06:48
  • thanks. I found nodatime but I still have problems dealing with Timezone Abbrevs. Is there other ways to get timezone via abbrev? e.g. ACST = Australian Central Standard Time – Milo Cabs Sep 23 '14 at 09:07
  • You could find *all* the time zones which ever use a particular abbreviation, but you'd be better off avoiding abbreviations entirely. Doesn't moment.js give you the full IANA ID, e.g. Europe/London? – Jon Skeet Sep 23 '14 at 10:54
  • 1
    @Milo - It's unclear what you're talking about. Please show a code example. You can't get an abbreviation out of moment without going through moment-timezone and specifying an IANA time zone id. Also, please recognize that many time zone abbreviations are ambiguous. For example, there are 5 different meanings of "CST". [See this list for more examples](http://en.wikipedia.org/wiki/List_of_time_zone_abbreviations). – Matt Johnson-Pint Sep 23 '14 at 20:26
  • @Jon - Moment doesn't *give* you the IANA time zone ID, you have to supply it, and only when using the moment-timezone plugin. Ex: `moment().tz('Europe/London').format('YYYY-MM-DD HH:mm:ss zz') === "2014-09-23 21:37:24 BST"` – Matt Johnson-Pint Sep 23 '14 at 20:38
  • Sorry for the confusion. I was also confused. I thought the selection I had, which had options from Olson to Abbrevs to GMT+0, were from moment.js but it seems they were static data from that was copied from iana. My problem now is handling abbrevations since it is being used in the application – Milo Cabs Sep 24 '14 at 01:58
  • @JonSkeet Oh, you're part of the dev team for NodaTime. I have a list of abbrevations that NodaTime missed. How can I give this to you? e.g. ACST = Australian Central Standard Time was not mapped to Australia... – Milo Cabs Sep 24 '14 at 03:40
  • It's not that Noda Time "missed" anything - it's a matter of what's available in TZDB (IANA). But again, I would *strongly* urge you to abandon abbreviations entirely. They're ambiguous and basically painful - even if it means a fair amount of work to pull them out (or at least only use them for display purposes) I'd strongly urge you to do so. – Jon Skeet Sep 24 '14 at 05:45
  • I can't necessarily back off of this since the downstream that will use the data is already using this since its java and it isn't even a problem for them there – Milo Cabs Sep 24 '14 at 06:00
  • 1
    Anything that uses abbreviations *does* have a problem - they may just not realize it yet. – Jon Skeet Sep 25 '14 at 10:23

1 Answers1

2

So NodaTime handled the Olson Timezones as well as GMT+ , UTC and others. For the abbrevations, I settled with this

public static DateTimeZone GetTimeZone(string id)
    {
        DateTimeZone timezone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(id);

        if (timezone == null) {
            var instant = Instant.FromDateTimeUtc(DateTime.UtcNow);
            var source = DateTimeZoneProviders.Tzdb;
            id = source.Ids.Where(i => source[i].GetZoneInterval(instant).Name == id).FirstOrDefault();
            timezone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(id);
        }

        return timezone;
    }
Milo Cabs
  • 503
  • 1
  • 10
  • 24