Well, you cannot do this only with the country code - you need a time zone. Some countries have several time zones.
Once you have a time zone like DateTimeZone
(either through BCL TimeZoneInfo
or the TZDB time zone provider), you should:
- a
LocalDateTime
, , . LocalDateTimePattern
LocalDateTime.InZoneLeniently
( - ), ZonedDateTime
WithZone(DateTimeZone.Utc)
UTC-based ZonedDateTime
InZoneLeniently
, InZoneStrictly
InZone(DateTimeZone, ZoneLocalMappingResolver)
, , DST. . .
:
using System;
using NodaTime;
using NodaTime.Text;
class Test
{
static void Main()
{
var text = "Feb 5, 2016 7:45 PM";
var zone = DateTimeZoneProviders.Tzdb["Europe/Istanbul"];
var pattern = LocalDateTimePattern.CreateWithInvariantCulture("MMM d, YYYY h:mm tt");
var local = pattern.Parse(text).Value;
var zoned = local.InZoneStrictly(zone);
var utc = zoned.WithZone(DateTimeZone.Utc);
Console.WriteLine(utc);
}
}
, , TZDB (IANA) , Noda Time. , 2- ISO-3166, :
using NodaTime;
using NodaTime.TimeZones;
using System.Linq;
...
var code = "TR";
var zoneId = TzdbDateTimeZoneSource.Default.ZoneLocations
.Single(loc => loc.CountryCode == code)
.ZoneId;
var zone = DateTimeZoneProviders.Tzdb[zoneId];
Single
, ( none).
, :
var zonesByCountryCode = TzdbDateTimeZoneSource.Default
.ZoneLocations
.GroupBy(loc => loc.CountryCode)
.Where(g => g.Count() == 1)
.ToDictionary(g => g.Key, g => g.First());