다음을 통해 공유


날짜 및 시간에 대한 산술 연산 수행

업데이트: 2007년 11월

DateTimeDateTimeOffset 구조체는 둘 다 해당 값에 대해 산술 연산을 수행하는 멤버를 제공하지만 산술 연산의 결과는 전혀 다릅니다. 이 항목에서는 이러한 차이점을 검토하고, 이러한 차이점을 날짜 및 시간 데이터의 표준 시간대 인식 수준에 연결하고, 날짜 및 시간 데이터를 사용하여 완전 표준 시간대 인식 작업을 수행하는 방법에 대해 설명합니다.

DateTime 값에 대한 비교 및 산술 연산

.NET Framework version 2.0부터는 DateTime 값에 제한된 표준 시간대 인식 수준이 적용됩니다. DateTime.Kind 속성을 사용하면 DateTimeKind 값을 날짜와 시간에 할당하여 날짜와 시간이 현지 시간, UTC(협정 세계시) 또는 지정되지 않은 표준 시간대의 시간 중 무엇을 나타내는지 지정할 수 있습니다. 그러나 DateTime 값을 사용하여 날짜와 시간에 대해 비교 또는 산술 연산을 수행할 때는 이 제한된 표준 시간대 정보가 무시됩니다. 다음 예제에서는 현재 현지 시간과 현재 UTC 시간을 비교하여 이를 보여 줍니다.

Public Enum TimeComparison As Integer
   EarlierThan = -1
   TheSameAs = 0
   LaterThan = 1
End Enum

Module DateManipulation
   Public Sub Main()
      Dim localTime As Date = Date.Now
      Dim utcTime As Date = Date.UtcNow

      Console.WriteLine("Difference between {0} and {1} time: {2}:{3} hours", _
                        localTime.Kind.ToString(), _
                        utcTime.Kind.ToString(), _
                        (localTime - utcTime).Hours, _
                        (localTime - utcTime).Minutes)
      Console.WriteLine("The {0} time is {1} the {2} time.", _
                        localTime.Kind.ToString(), _ 
                        [Enum].GetName(GetType(TimeComparison), localTime.CompareTo(utcTime)), _
                        utcTime.Kind.ToString())  
      ' If run in the U.S. Pacific Standard Time zone, the example displays 
      ' the following output to the console:
      '    Difference between Local and Utc time: -7:0 hours
      '    The Local time is EarlierThan the Utc time.                                                    
   End Sub
End Module
using System;

public enum TimeComparison
{
   EarlierThan = -1,
   TheSameAs = 0,
   LaterThan = 1
}

public class DateManipulation
{
   public static void Main()
   {
      DateTime localTime = DateTime.Now;
      DateTime utcTime = DateTime.UtcNow;

      Console.WriteLine("Difference between {0} and {1} time: {2}:{3} hours", 
                        localTime.Kind.ToString(), 
                        utcTime.Kind.ToString(), 
                        (localTime - utcTime).Hours, 
                        (localTime - utcTime).Minutes);
      Console.WriteLine("The {0} time is {1} the {2} time.", 
                        localTime.Kind.ToString(), 
                        Enum.GetName(typeof(TimeComparison), localTime.CompareTo(utcTime)), 
                        utcTime.Kind.ToString());  
   }
}
// If run in the U.S. Pacific Standard Time zone, the example displays 
// the following output to the console:
//    Difference between Local and Utc time: -7:0 hours
//    The Local time is EarlierThan the Utc time.                                                    

CompareTo(DateTime) 메서드는 현지 시간이 UTC 시간보다 빠르거나 느리다고 보고하고, 빼기 연산은 미국 태평양 표준 시간대에 있는 시스템의 UTC와 현지 시간 간 차이가 일곱 시간임을 나타냅니다. 그러나 이러한 두 값은 서로 다른 특정 시간 표현을 제공하므로 이 경우 이 시간 간격이 전적으로 UTC에서의 현지 표준 시간대 오프셋 때문이라는 것을 쉽게 알 수 있습니다.

일반적으로 DateTime.Kind 속성은 동일한 두 시점의 비교가 나타내는 바와 같이 DateTime 비교 및 산술 메서드에서 반환하는 결과에 영향을 주지 않는 반면 이러한 결과의 해석에는 영향을 줄 수 있습니다. 예를 들어, 다음과 같습니다.

  • 해당 DateTime.Kind 속성이 둘 다 Utc인 두 날짜 및 시간 값에 대해 수행된 모든 산술 연산의 결과는 두 값 간의 실제 시간 간격을 반영합니다. 마찬가지로 이러한 두 시간 및 날짜 값의 비교는 시간 간의 관계를 정확하게 반영합니다.

  • 해당 DateTime.Kind 속성이 둘 다 Local인 두 날짜 및 시간 값이나 DateTime.Kind 속성 값이 서로 다른 두 날짜 및 시간 값에 대해 수행된 모든 산술 또는 비교 연산의 결과는 두 값 간의 차이를 클럭 시간으로 반영합니다.

  • 현지 날짜 및 시간 값에 대한 산술 또는 비교 연산은 특정 값이 모호하거나 올바르지 않다는 것도 고려하지 않고 현지 표준 시간대와 일광 절약 시간제 간의 전환에 따라 조정 규칙이 달라지는 것도 고려하지 않습니다.

  • UTC와 현지 시간 간의 차이를 비교하거나 계산하는 연산에는 결과에 있는 UTC에서의 현지 표준 시간대 오프셋과 동일한 시간 간격이 포함됩니다.

  • 지정되지 않은 시간과 UTC 또는 현지 시간 간의 차이를 비교하거나 계산하는 연산은 단순 클럭 시간을 반영합니다. 표준 시간대 차이는 고려되지 않으며, 표준 시간대 조정 규칙의 적용 여부도 결과에 반영되지 않습니다.

  • 지정되지 않은 두 값 간의 차이를 비교하거나 계산하는 연산에는 서로 다른 두 표준 시간대의 시간 차이를 반영하는 알 수 없는 간격이 포함될 수 있습니다.

표준 시간대 차이가 날짜 및 시간 계산에 영향을 주지 않거나(DateTime, DateTimeOffset 및 TimeZoneInfo 중 선택 참조) 날짜 및 시간 데이터의 컨텍스트에서 비교 또는 산술 연산의 의미를 정의하는 경우도 있을 수 있습니다.

DateTimeOffset 값에 대한 비교 및 산술 연산

DateTimeOffset 값에는 날짜와 시간뿐만 아니라 UTC를 기준으로 해당 날짜와 시간을 명확하게 정의하는 오프셋도 포함됩니다. 따라서 DateTime 값과는 약간 다른 방식으로 같음을 정의할 수 있습니다. DateTime 값은 날짜 및 시간 값이 같으면 같은 반면에 DateTimeOffset 값은 같은 시점을 참조하면 같습니다. 따라서 DateTimeOffset 값은 두 날짜 및 시간 간의 간격을 결정하며, 비교 연산 및 대부분의 산술 연산에 사용될 때 더 정확한 반면에 더 적은 해석을 필요로 합니다. 현지 및 UTC DateTime 값을 비교하는 이전 예제와 동일한 DateTimeOffset인 다음 예제에서는 이러한 동작의 차이를 보여 줍니다.

Public Enum TimeComparison As Integer
   EarlierThan = -1
   TheSameAs = 0
   LaterThan = 1
End Enum

Module DateTimeOffsetManipulation
   Public Sub Main()
      Dim localTime As DateTimeOffset = DateTimeOffset.Now
      Dim utcTime As DateTimeOffset = DateTimeOffset.UtcNow

      Console.WriteLine("Difference between local time and UTC: {0}:{0:D2} hours.", _
                        (localTime - utcTime).Hours, _
                        (localTime - utcTime).Minutes)
      Console.WriteLine("The local time is {0} UTC.", _
                        [Enum].GetName(GetType(TimeComparison), localTime.CompareTo(utcTime)))  
   End Sub
End Module
' Regardless of the local time zone, the example displays 
' the following output to the console:
'    Difference between local time and UTC: 0:00 hours.
'    The local time is TheSameAs UTC.
'          Console.WriteLine(e.GetType().Name)
using System;

public enum TimeComparison
{
   EarlierThan = -1,
   TheSameAs = 0,
   LaterThan = 1
}

public class DateTimeOffsetManipulation
{
   public static void Main()
   {
      DateTimeOffset localTime = DateTimeOffset.Now;
      DateTimeOffset utcTime = DateTimeOffset.UtcNow;

      Console.WriteLine("Difference between local time and UTC: {0}:{1:D2} hours", 
                        (localTime - utcTime).Hours, 
                        (localTime - utcTime).Minutes);
      Console.WriteLine("The local time is {0} UTC.", 
                        Enum.GetName(typeof(TimeComparison), localTime.CompareTo(utcTime)));  
   }
}
// Regardless of the local time zone, the example displays 
// the following output to the console:
//    Difference between local time and UTC: 0:00 hours.
//    The local time is TheSameAs UTC.

이 예제에서 CompareTo 메서드는 현재 현지 시간과 현재 UTC 시간이 같음을 나타내고 DateTimeOffset 값의 빼기는 두 시간 간의 차이가 TimeSpan.Zero임을 나타냅니다.

날짜 및 시간 연산에 DateTimeOffset 값 사용과 관련된 주요 제한 사항은 DateTimeOffset 값의 표준 시간대 인식 수준이 완전하지 않고 제한적이라는 것입니다. DateTimeOffset 변수에 값이 처음 할당될 때는 DateTimeOffset 값의 오프셋이 UTC에서의 표준 시간대 오프셋을 반영하지만 이후부터는 표준 시간대와 분리됩니다. 식별 가능한 시간과 더 이상 직접 연결되지 않기 때문에 날짜 및 시간 간격의 더하기 및 빼기 연산에는 표준 시간대의 조정 규칙이 고려되지 않습니다.

예를 들어, 미국 중부 표준시에서는 2008년 3월 9일 오전 2시에 표준 시간대가 일광 절약 시간제로 전환되므로 미국 중부 표준시 2008년 3월 9일 오전 1시 30분에 2시간 30분을 더하면 2008년 3월 9일 오전 5시가 되어야 합니다. 그러나 다음 예제와 같이 이 더하기 연산의 결과는 2008년 3월 9일 오전 4시가 됩니다. 이것은 이 연산 결과는 올바른 시점을 나타내지만 해당 시간이 표준 시간대의 시간이 아니기 때문입니다. 즉, 예상된 표준 시간대 오프셋이 반영되지 않았기 때문입니다.

Module IntervalArithmetic
   Public Sub Main()
      Dim generalTime As Date = #03/09/2008 1:30AM#
      Const tzName As String = "Central Standard Time"
      Dim twoAndAHalfHours As New TimeSpan(2, 30, 0)

      ' Instantiate DateTimeOffset value to have correct CST offset
      Try
         Dim centralTime1 As New DateTimeOffset(generalTime, _
                    TimeZoneInfo.FindSystemTimeZoneById(tzName).GetUtcOffset(generalTime))

         ' Add two and a half hours      
         Dim centralTime2 As DateTimeOffset = centralTime1.Add(twoAndAHalfHours)
         ' Display result
         Console.WriteLine("{0} + {1} hours = {2}", centralTime1, _
                                                    twoAndAHalfHours.ToString(), _
                                                    centralTime2)   
      Catch e As TimeZoneNotFoundException
         Console.WriteLine("Unable to retrieve Central Standard Time zone information.")
      End Try
   End Sub
End Module
' The example displays the following output to the console:
'    3/9/2008 1:30:00 AM -06:00 + 02:30:00 hours = 3/9/2008 4:00:00 AM -06:00
using System;

public class IntervalArithmetic
{
   public static void Main()
   {
      DateTime generalTime = new DateTime(2008, 3, 9, 1, 30, 0);
      const string tzName = "Central Standard Time";
      TimeSpan twoAndAHalfHours = new TimeSpan(2, 30, 0);

      // Instantiate DateTimeOffset value to have correct CST offset
      try
      {
         DateTimeOffset centralTime1 = new DateTimeOffset(generalTime, 
                    TimeZoneInfo.FindSystemTimeZoneById(tzName).GetUtcOffset(generalTime));

         // Add two and a half hours      
         DateTimeOffset centralTime2 = centralTime1.Add(twoAndAHalfHours);
         // Display result
         Console.WriteLine("{0} + {1} hours = {2}", centralTime1, 
                                                    twoAndAHalfHours.ToString(), 
                                                    centralTime2);  
      }
      catch (TimeZoneNotFoundException)
      {
         Console.WriteLine("Unable to retrieve Central Standard Time zone information.");
      }
   }
}
// The example displays the following output to the console:
//    3/9/2008 1:30:00 AM -06:00 + 02:30:00 hours = 3/9/2008 4:00:00 AM -06:00

표준 시간대의 시간에 대한 산술 연산

TimeZoneInfo 클래스에는 한 표준 시간대에서 다른 표준 시간대로 시간을 변환할 때 자동으로 조정을 적용하는 여러 변환 메서드가 포함되어 있습니다. 여기에는 다음과 같은 컨트롤이 포함됩니다.

자세한 내용은 표준 시간대 간에 시간 변환을 참조하십시오.

TimeZoneInfo 클래스는 날짜 및 시간 연산을 수행할 때 자동으로 조정 규칙을 적용하는 메서드를 제공하지 않습니다. 그러나 표준 시간대의 시간을 UTC로 변환하고 산술 연산을 수행한 다음 UTC를 다시 표준 시간대의 시간으로 변환하면 자동으로 조정 규칙을 적용할 수 있습니다. 자세한 내용은 방법: 날짜 및 시간 산술 연산의 표준 시간대 사용을 참조하십시오.

예를 들어, 다음 코드는 2008년 3월 9일 오전 2시에 2시간 30분을 더하는 이전 코드와 비슷합니다. 그러나 이 코드에서는 먼저 중부 표준시를 UTC로 변환하고 날짜 및 시간 연산을 수행한 다음 이 연산 결과에서 UTC를 중부 표준시로 다시 변환합니다. 그러면 결과 시간에 중부 표준시가 일광 절약 시간제로 전환된 것이 반영됩니다.

Module TimeZoneAwareArithmetic
   Public Sub Main()
      Const tzName As String = "Central Standard Time"

      Dim generalTime As Date = #03/09/2008 1:30AM#
      Dim cst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(tzName) 
      Dim twoAndAHalfHours As New TimeSpan(2, 30, 0)

      ' Instantiate DateTimeOffset value to have correct CST offset
      Try
         Dim centralTime1 As New DateTimeOffset(generalTime, _
                    cst.GetUtcOffset(generalTime))

         ' Add two and a half hours 
         Dim utcTime As DateTimeOffset = centralTime1.ToUniversalTime()
         utcTime += twoAndAHalfHours

         Dim centralTime2 As DateTimeOffset = TimeZoneInfo.ConvertTime(utcTime, cst)
         ' Display result
         Console.WriteLine("{0} + {1} hours = {2}", centralTime1, _
                                                    twoAndAHalfHours.ToString(), _
                                                    centralTime2)   
      Catch e As TimeZoneNotFoundException
         Console.WriteLine("Unable to retrieve Central Standard Time zone information.")
      End Try
   End Sub
End Module
' The example displays the following output to the console:
'    3/9/2008 1:30:00 AM -06:00 + 02:30:00 hours = 3/9/2008 5:00:00 AM -05:00
using System;

public class TimeZoneAwareArithmetic
{
   public static void Main()
   {
      const string tzName = "Central Standard Time";

      DateTime generalTime = new DateTime(2008, 3, 9, 1, 30, 0);
      TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById(tzName);
      TimeSpan twoAndAHalfHours = new TimeSpan(2, 30, 0);

      // Instantiate DateTimeOffset value to have correct CST offset
      try
      {
         DateTimeOffset centralTime1 = new DateTimeOffset(generalTime, 
                                       cst.GetUtcOffset(generalTime));

         // Add two and a half hours
         DateTimeOffset utcTime = centralTime1.ToUniversalTime();
         utcTime += twoAndAHalfHours;

         DateTimeOffset centralTime2 = TimeZoneInfo.ConvertTime(utcTime, cst);
         // Display result
         Console.WriteLine("{0} + {1} hours = {2}", centralTime1, 
                                                    twoAndAHalfHours.ToString(), 
                                                    centralTime2);  
      }
      catch (TimeZoneNotFoundException)
      {
         Console.WriteLine("Unable to retrieve Central Standard Time zone information.");
      }
   }
}
// The example displays the following output to the console:
//    3/9/2008 1:30:00 AM -06:00 + 02:30:00 hours = 3/9/2008 5:00:00 AM -05:00

참고 항목

작업

방법: 날짜 및 시간 산술 연산의 표준 시간대 사용

기타 리소스

시간 및 표준 시간대