원하는 시간대의 날짜와 시간을 표시하기 위한 DateTimeOffset

클라우드에서 실행되는 웹 응용프로그램에서 날짜-시간 값을 처리할 때, DateTime 보다는 DateTimeOffset 형식으로 자료를 처리하면 개인화된 시간 표시를 보다 편리하게 지원할 수 있습니다.

클라우드에서 실행되는 웹 응용프로그램은 웹 서버의 시간대에 의지할 수 없는 경우가 많습니다.

예를 들면, 한국 지역의 웹서버를 선택해서 웹 응용프로그램을 게시했지만, 웹 서버의 시간대를 조정할 수 없는 경우 자료는 UTC 시각으로 저장되고, 표시를 원하는 시간대로 표시할 수 있습니다.

🔔 DateTimeOffset

DateTimeOffset 구조체는 UTC (Coordinated Universal Time)를 기준으로 한 날짜 및 시간으로 표시되는 특정 시점을 나타냅니다.

DateTime 속성

날짜와 시각을 표현하는 구조체입니다.

Offset 속성

UTC를 기준으로 한 시간(Hour) 차이를 나타냅니다.

-14 ~ 14 사이의 값입니다.

🔔 Timezone

TimeInfo 인스턴스를 얻기위해 사용되는 Id, StandardName의 목록입니다.

Id Standard name Offset
Dateline Standard Time Dateline Standard Time -12:00:00
UTC-11 UTC-11 -11:00:00
Aleutian Standard Time Aleutian Standard Time -10:00:00
Hawaiian Standard Time Hawaiian Standard Time -10:00:00
Marquesas Standard Time Marquesas Standard Time -09:30:00
Alaskan Standard Time Alaskan Standard Time -09:00:00
UTC-09 UTC-09 -09:00:00
Pacific Standard Time (Mexico) Pacific Standard Time (Mexico) -08:00:00
UTC-08 UTC-08 -08:00:00
Pacific Standard Time Pacific Standard Time -08:00:00
US Mountain Standard Time US Mountain Standard Time -07:00:00
Mountain Standard Time (Mexico) Mountain Standard Time (Mexico) -07:00:00
Mountain Standard Time Mountain Standard Time -07:00:00
Central America Standard Time Central America Standard Time -06:00:00
Central Standard Time Central Standard Time -06:00:00
Easter Island Standard Time Easter Island Standard Time -06:00:00
Central Standard Time (Mexico) Central Standard Time (Mexico) -06:00:00
Canada Central Standard Time Canada Central Standard Time -06:00:00
SA Pacific Standard Time SA Pacific Standard Time -05:00:00
Eastern Standard Time (Mexico) Eastern Standard Time (Mexico) -05:00:00
Eastern Standard Time Eastern Standard Time -05:00:00
Haiti Standard Time Haiti Standard Time -05:00:00
Cuba Standard Time Cuba Standard Time -05:00:00
US Eastern Standard Time US Eastern Standard Time -05:00:00
Turks And Caicos Standard Time Turks and Caicos Standard Time -05:00:00
Paraguay Standard Time Paraguay Standard Time -04:00:00
Atlantic Standard Time Atlantic Standard Time -04:00:00
Venezuela Standard Time Venezuela Standard Time -04:00:00
Central Brazilian Standard Time Central Brazilian Standard Time -04:00:00
SA Western Standard Time SA Western Standard Time -04:00:00
Pacific SA Standard Time Pacific SA Standard Time -04:00:00
Newfoundland Standard Time Newfoundland Standard Time -03:30:00
Tocantins Standard Time Tocantins Standard Time -03:00:00
E. South America Standard Time E. South America Standard Time -03:00:00
SA Eastern Standard Time SA Eastern Standard Time -03:00:00
Argentina Standard Time Argentina Standard Time -03:00:00
Greenland Standard Time Greenland Standard Time -03:00:00
Montevideo Standard Time Montevideo Standard Time -03:00:00
Magallanes Standard Time Magallanes Standard Time -03:00:00
Saint Pierre Standard Time Saint Pierre Standard Time -03:00:00
Bahia Standard Time Bahia Standard Time -03:00:00
UTC-02 UTC-02 -02:00:00
Mid-Atlantic Standard Time Mid-Atlantic Standard Time -02:00:00
Azores Standard Time Azores Standard Time -01:00:00
Cape Verde Standard Time Cabo Verde Standard Time -01:00:00
UTC Coordinated Universal Time 00:00:00
Morocco Standard Time Morocco Standard Time 00:00:00
GMT Standard Time GMT Standard Time 00:00:00
Greenwich Standard Time Greenwich Standard Time 00:00:00
W. Europe Standard Time W. Europe Standard Time 01:00:00
Central Europe Standard Time Central Europe Standard Time 01:00:00
Romance Standard Time Romance Standard Time 01:00:00
Sao Tome Standard Time Sao Tome Standard Time 01:00:00
Central European Standard Time Central European Standard Time 01:00:00
W. Central Africa Standard Time W. Central Africa Standard Time 01:00:00
Jordan Standard Time Jordan Standard Time 02:00:00
GTB Standard Time GTB Standard Time 02:00:00
Middle East Standard Time Middle East Standard Time 02:00:00
Egypt Standard Time Egypt Standard Time 02:00:00
E. Europe Standard Time E. Europe Standard Time 02:00:00
Syria Standard Time Syria Standard Time 02:00:00
West Bank Standard Time West Bank Gaza Standard Time 02:00:00
South Africa Standard Time South Africa Standard Time 02:00:00
FLE Standard Time FLE Standard Time 02:00:00
Israel Standard Time Jerusalem Standard Time 02:00:00
Kaliningrad Standard Time Russia TZ 1 Standard Time 02:00:00
Sudan Standard Time Sudan Standard Time 02:00:00
Libya Standard Time Libya Standard Time 02:00:00
Namibia Standard Time Namibia Standard Time 02:00:00
Arabic Standard Time Arabic Standard Time 03:00:00
Turkey Standard Time Turkey Standard Time 03:00:00
Arab Standard Time Arab Standard Time 03:00:00
Belarus Standard Time Belarus Standard Time 03:00:00
Russian Standard Time Russia TZ 2 Standard Time 03:00:00
E. Africa Standard Time E. Africa Standard Time 03:00:00
Iran Standard Time Iran Standard Time 03:30:00
Arabian Standard Time Arabian Standard Time 04:00:00
Astrakhan Standard Time Astrakhan Standard Time 04:00:00
Azerbaijan Standard Time Azerbaijan Standard Time 04:00:00
Russia Time Zone 3 Russia TZ 3 Standard Time 04:00:00
Mauritius Standard Time Mauritius Standard Time 04:00:00
Saratov Standard Time Saratov Standard Time 04:00:00
Georgian Standard Time Georgian Standard Time 04:00:00
Caucasus Standard Time Caucasus Standard Time 04:00:00
Afghanistan Standard Time Afghanistan Standard Time 04:30:00
West Asia Standard Time West Asia Standard Time 05:00:00
Ekaterinburg Standard Time Russia TZ 4 Standard Time 05:00:00
Pakistan Standard Time Pakistan Standard Time 05:00:00
India Standard Time India Standard Time 05:30:00
Sri Lanka Standard Time Sri Lanka Standard Time 05:30:00
Nepal Standard Time Nepal Standard Time 05:45:00
Central Asia Standard Time Central Asia Standard Time 06:00:00
Bangladesh Standard Time Bangladesh Standard Time 06:00:00
Omsk Standard Time Omsk Standard Time 06:00:00
Myanmar Standard Time Myanmar Standard Time 06:30:00
SE Asia Standard Time SE Asia Standard Time 07:00:00
Altai Standard Time Altai Standard Time 07:00:00
W. Mongolia Standard Time W. Mongolia Standard Time 07:00:00
North Asia Standard Time Russia TZ 6 Standard Time 07:00:00
N. Central Asia Standard Time Novosibirsk Standard Time 07:00:00
Tomsk Standard Time Tomsk Standard Time 07:00:00
China Standard Time China Standard Time 08:00:00
North Asia East Standard Time Russia TZ 7 Standard Time 08:00:00
Singapore Standard Time Malay Peninsula Standard Time 08:00:00
W. Australia Standard Time W. Australia Standard Time 08:00:00
Taipei Standard Time Taipei Standard Time 08:00:00
Ulaanbaatar Standard Time Ulaanbaatar Standard Time 08:00:00
North Korea Standard Time North Korea Standard Time 08:30:00
Aus Central W. Standard Time Aus Central W. Standard Time 08:45:00
Transbaikal Standard Time Transbaikal Standard Time 09:00:00
Tokyo Standard Time Tokyo Standard Time 09:00:00
Korea Standard Time Korea Standard Time 09:00:00
Yakutsk Standard Time Russia TZ 8 Standard Time 09:00:00
Cen. Australia Standard Time Cen. Australia Standard Time 09:30:00
AUS Central Standard Time AUS Central Standard Time 09:30:00
E. Australia Standard Time E. Australia Standard Time 10:00:00
AUS Eastern Standard Time AUS Eastern Standard Time 10:00:00
West Pacific Standard Time West Pacific Standard Time 10:00:00
Tasmania Standard Time Tasmania Standard Time 10:00:00
Vladivostok Standard Time Russia TZ 9 Standard Time 10:00:00
Lord Howe Standard Time Lord Howe Standard Time 10:30:00
Bougainville Standard Time Bougainville Standard Time 11:00:00
Russia Time Zone 10 Russia TZ 10 Standard Time 11:00:00
Magadan Standard Time Magadan Standard Time 11:00:00
Norfolk Standard Time Norfolk Standard Time 11:00:00
Sakhalin Standard Time Sakhalin Standard Time 11:00:00
Central Pacific Standard Time Central Pacific Standard Time 11:00:00
Russia Time Zone 11 Russia TZ 11 Standard Time 12:00:00
New Zealand Standard Time New Zealand Standard Time 12:00:00
UTC+12 UTC+12 12:00:00
Fiji Standard Time Fiji Standard Time 12:00:00
Kamchatka Standard Time Kamchatka Standard Time 12:00:00
Chatham Islands Standard Time Chatham Islands Standard Time 12:45:00
UTC+13 UTC+13 13:00:00
Tonga Standard Time Tonga Standard Time 13:00:00
Samoa Standard Time Samoa Standard Time 13:00:00
Line Islands Standard Time Line Islands Standard Time 14:00:00
Console.WriteLine($"| Id | Standard name | Offset |");
Console.WriteLine($"| :- | :- | :- |");
foreach (var timezone in TimeZoneInfo.GetSystemTimeZones().OrderBy(z=>z.BaseUtcOffset))
{
    Console.WriteLine($"| {timezone.Id} | {timezone.StandardName} | {timezone.BaseUtcOffset} |");
}

🔔 시간대 변경

TimeZoneInfo 클래스를 사용해서 간편하게 원하는 시간대의 시각으로 변경할 수 있습니다.

public static class DateTimeOffsetExtensions
{
    /// <summary>
    /// 지정된 시간대에 해당하는 시각을 계산합니다.
    /// </summary>
    /// <param name="dateTimeOffset"></param>
    /// <param name="timeZoneId">시간대 식별자</param>
    /// <returns></returns>
    public static DateTimeOffset ToLocalDateTime(this DateTimeOffset dateTimeOffset, string timeZoneId)
    {
        var timeZoneInfo = TimeZoneInfo.GetSystemTimeZones()
            .Where(t => t.Id.Equals(timeZoneId, StringComparison.OrdinalIgnoreCase))
            .FirstOrDefault();

        var localTime = TimeZoneInfo.ConvertTime(dateTimeOffset, timeZoneInfo);

        return localTime;
    }
}

👁‍🗨 코드

Github 에서 간략한 예제를 확인할 수 있습니다.

댓글 남기기