TimeZoneInfo.GetUtcOffset 메서드

정의

이 표준 시간대의 시간과 특정 날짜 및 시간에 대한 UTC(협정 세계시) 사이의 오프셋 또는 차이를 계산합니다.Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

오버로드

GetUtcOffset(DateTime)

이 표준 시간대의 시간과 특정 날짜 및 시간에 대한 UTC(협정 세계시) 사이의 오프셋 또는 차이를 계산합니다.Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

GetUtcOffset(DateTimeOffset)

이 표준 시간대의 시간과 특정 날짜 및 시간에 대한 UTC(협정 세계시) 사이의 오프셋 또는 차이를 계산합니다.Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

GetUtcOffset(DateTime)

이 표준 시간대의 시간과 특정 날짜 및 시간에 대한 UTC(협정 세계시) 사이의 오프셋 또는 차이를 계산합니다.Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

public:
 TimeSpan GetUtcOffset(DateTime dateTime);
public TimeSpan GetUtcOffset (DateTime dateTime);
member this.GetUtcOffset : DateTime -> TimeSpan
Public Function GetUtcOffset (dateTime As DateTime) As TimeSpan

매개 변수

dateTime
DateTime

오프셋을 확인할 대상 날짜 및 시간입니다.The date and time to determine the offset for.

반환

TimeSpan

두 표준 시간대 사이의 시간 차이를 나타내는 개체입니다.An object that indicates the time difference between the two time zones.

예제

다음 예제에서는 서로 다른 표준 시간대와 다른 속성 값을 가진 날짜 값의 메서드를 사용 하는 방법을 보여 줍니다 GetUtcOffset(DateTime) Kind .The following example illustrates the use of the GetUtcOffset(DateTime) method with different time zones and with date values that have different Kind property values.

using System;

[assembly:CLSCompliant(true)]
namespace TimeZoneInfoCode
{
   public class TimeOffsets
   {
      public static void Main()
      {
         TimeOffsets timeoff = new TimeOffsets();
         TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
   
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2007, 11, 4, 1, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2006, 12, 10, 15, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2007, 3, 11, 2, 30, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(DateTime.UtcNow, TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2007, 11, 4, 1, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2006, 12, 10, 3, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2007, 3, 11, 2, 30, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(DateTime.Now, TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 11, 4, 1, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2006, 12, 10, 15, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 3, 11, 2, 30, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 11, 14, 00, 00, 00, DateTimeKind.Local), cst);
      }
      
      private void ShowOffset(DateTime time, TimeZoneInfo timeZone)
      {
         DateTime convertedTime = time;  
         TimeSpan offset;
         
         if (time.Kind == DateTimeKind.Local && ! timeZone.Equals(TimeZoneInfo.Local)) 
            convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone);
         else if (time.Kind == DateTimeKind.Utc && ! timeZone.Equals(TimeZoneInfo.Utc)) 
            convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone);         
   
         offset = timeZone.GetUtcOffset(time);
         if (time == convertedTime)
         {
            Console.WriteLine("{0} {1} ", time, 
                              timeZone.IsDaylightSavingTime(time) ? timeZone.DaylightName : timeZone.StandardName);
            Console.WriteLine("   It differs from UTC by {0} hours, {1} minutes.", 
                               offset.Hours, 
                               offset.Minutes);
         }
         else
         {
            Console.WriteLine("{0} {1} ", time, 
                              time.Kind == DateTimeKind.Utc ? "UTC" :  TimeZoneInfo.Local.Id);       
            Console.WriteLine("   converts to {0} {1}.", 
                              convertedTime, 
                              timeZone.Id);
            Console.WriteLine("   It differs from UTC by {0} hours, {1} minutes.", 
                              offset.Hours, offset.Minutes);  
         }
         Console.WriteLine();                                             
      }
   }
}
// The example produces the following output:
//
//       6/12/2006 11:00:00 AM Pacific Daylight Time 
//          It differs from UTC by -7 hours, 0 minutes.
//       
//       11/4/2007 1:00:00 AM Pacific Standard Time 
//          It differs from UTC by -8 hours, 0 minutes.
//       
//       12/10/2006 3:00:00 PM Pacific Standard Time 
//          It differs from UTC by -8 hours, 0 minutes.
//       
//       3/11/2007 2:30:00 AM Pacific Standard Time 
//          It differs from UTC by -8 hours, 0 minutes.
//       
//       2/2/2007 8:35:46 PM UTC 
//          converts to 2/2/2007 12:35:46 PM Pacific Standard Time.
//          It differs from UTC by -8 hours, 0 minutes.
//       
//       6/12/2006 11:00:00 AM UTC 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       11/4/2007 1:00:00 AM UTC 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       12/10/2006 3:00:00 AM UTC 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       3/11/2007 2:30:00 AM UTC 
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       2/2/2007 12:35:46 PM Pacific Standard Time 
//          converts to 2/2/2007 8:35:46 PM UTC.
//          It differs from UTC by 0 hours, 0 minutes.
//       
//       6/12/2006 11:00:00 AM Central Daylight Time 
//          It differs from UTC by -5 hours, 0 minutes.
//       
//       11/4/2007 1:00:00 AM Central Standard Time 
//          It differs from UTC by -6 hours, 0 minutes.
//       
//       12/10/2006 3:00:00 PM Central Standard Time 
//          It differs from UTC by -6 hours, 0 minutes.
//       
//       3/11/2007 2:30:00 AM Central Standard Time 
//          It differs from UTC by -6 hours, 0 minutes.
//       
//       11/14/2007 12:00:00 AM Pacific Standard Time 
//          converts to 11/14/2007 2:00:00 AM Central Standard Time.
//          It differs from UTC by -6 hours, 0 minutes.
Option Strict On

<Assembly: CLSCompliant(True)>
Module TimeOffsets
   Public Sub Main()
      Dim cst As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")

      ShowOffset(#6/12/2006 11:00AM#, TimeZoneInfo.Local)
      ShowOffset(#11/4/2007 1:00AM#, TimeZoneInfo.Local)
      ShowOffset(#12/10/2006 3:00PM#, TimeZoneInfo.Local)
      ShowOffset(#3/11/2007 2:30:00AM#, TimeZoneInfo.Local)
      ShowOffset(Date.UtcNow, TimeZoneInfo.Local)
      ShowOffset(#6/12/2006 11:00AM#, TimeZoneInfo.Utc)
      ShowOffset(#11/4/2007 1:00AM#, TimeZoneInfo.Utc)
      ShowOffset(#12/10/2006 3:00PM#, TimeZoneInfo.Utc)
      ShowOffset(#3/11/2007 2:30:00AM#, TimeZoneInfo.Utc)
      ShowOffset(Date.Now, TimeZoneInfo.Utc)
      ShowOffset(#6/12/2006 11:00AM#, cst)
      ShowOffset(#11/4/2007 1:00AM#, cst)
      ShowOffset(#12/10/2006 3:00PM#, cst)
      ShowOffset(#3/11/2007 2:30:00AM#, cst)
      ShowOffset(New Date(2007, 11, 14, 00, 00, 00, DateTimeKind.Local), cst)
   End Sub
   
   Private Sub ShowOffset(time As Date, timeZone As TimeZoneInfo)
      Dim convertedTime As Date = time  
      Dim offset As TimeSpan
      
      If time.Kind = DateTimeKind.Local And Not timeZone.Equals(TimeZoneInfo.Local) Then
         convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Local, timeZone)
      ElseIf time.Kind = DateTimeKind.Utc And Not timeZone.Equals(TimeZoneInfo.Utc) Then
         convertedTime = TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZone)         
      End If
      offset = timeZone.GetUtcOffset(time)
      If time = convertedTime Then
         Console.WriteLine("{0} {1} ", time, _
                           IIf(timeZone.IsDaylightSavingTime(time), timeZone.DaylightName, timeZone.StandardName))
         Console.WriteLine("   It differs from UTC by {0} hours, {1} minutes.", _
                            offset.Hours, _
                            offset.Minutes)
      Else
         Console.WriteLine("{0} {1} ", time, _
                           IIf(time.Kind = DateTimeKind.Utc, "UTC", TimeZoneInfo.Local.Id))       
         Console.WriteLine("   converts to {0} {1}.", _
                           convertedTime, _
                           timeZone.Id)
         Console.WriteLine("   It differs from UTC by {0} hours, {1} minutes.", _
                           offset.Hours, offset.Minutes)  
       End If
       Console.WriteLine()                                             
   End Sub
End Module
'
' The example produces the following output:
' 
'       6/12/2006 11:00:00 AM Pacific Daylight Time 
'          It differs from UTC by -7 hours, 0 minutes.
'       
'       11/4/2007 1:00:00 AM Pacific Standard Time 
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       12/10/2006 3:00:00 PM Pacific Standard Time 
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       3/11/2007 2:30:00 AM Pacific Standard Time 
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       2/2/2007 8:35:46 PM UTC 
'          converts to 2/2/2007 12:35:46 PM Pacific Standard Time.
'          It differs from UTC by -8 hours, 0 minutes.
'       
'       6/12/2006 11:00:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       11/4/2007 1:00:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       12/10/2006 3:00:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       3/11/2007 2:30:00 AM UTC 
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       2/2/2007 12:35:46 PM Pacific Standard Time 
'          converts to 2/2/2007 8:35:46 PM UTC.
'          It differs from UTC by 0 hours, 0 minutes.
'       
'       6/12/2006 11:00:00 AM Central Daylight Time 
'          It differs from UTC by -5 hours, 0 minutes.
'       
'       11/4/2007 1:00:00 AM Central Standard Time 
'          It differs from UTC by -6 hours, 0 minutes.
'       
'       12/10/2006 3:00:00 PM Central Standard Time 
'          It differs from UTC by -6 hours, 0 minutes.
'       
'       3/11/2007 2:30:00 AM Central Standard Time 
'          It differs from UTC by -6 hours, 0 minutes.
'       
'       11/14/2007 12:00:00 AM Pacific Standard Time 
'          converts to 11/14/2007 2:00:00 AM Central Standard Time.
'          It differs from UTC by -6 hours, 0 minutes.

설명

반환 된 시간 범위는 현재 표준 시간대 조정 규칙 애플리케이션으로 인 한 모든 차이 포함합니다.The returned time span includes any differences due to the application of adjustment rules to the current time zone. 이는 BaseUtcOffset utc (협정 세계시)와 표준 시간대의 표준 시간 사이의 차이를 반환 하는 속성과 다르며, 따라서 조정 규칙을 고려 하지 않습니다.It differs from the BaseUtcOffset property, which returns the difference between Coordinated Universal Time (UTC) and the time zone's standard time and, therefore, does not take adjustment rules into account.

dateTime매개 변수의 Kind 속성이 표준 시간대 개체와 일치 하지 않는 경우이 메서드는 결과를 반환 하기 전에 필요한 변환을 수행 합니다.If the dateTime parameter's Kind property does not correspond to the time zone object, this method performs the necessary conversion before returning a result. 예를 들어 Kind 속성이 DateTimeKind.Local 이지만 표준 시간대 개체가 현지 표준 시간대가 아닌 경우이 오류가 발생할 수 있습니다.For example, this can occur if the Kind property is DateTimeKind.Local but the time zone object is not the local time zone. dateTime 모호 하거나 변환 된 시간이 모호한 경우이 메서드는 모호한 시간을 표준 시간으로 해석 합니다.If dateTime is ambiguous, or if the converted time is ambiguous, this method interprets the ambiguous time as a standard time. dateTime이 잘못 된 경우이 메서드는 TimeSpan UTC와 표준 시간대의 표준 시간 사이의 차이를 반영 하는 개체를 반환 합니다.If dateTime is invalid, this method returns a TimeSpan object that reflects the difference between UTC and the time zone's standard time.

TimeZoneInfo.GetUtcOffset(DateTime)메서드는 GetUtcOffset 클래스의 메서드에 대 한 작업과 비슷합니다 TimeZone .The TimeZoneInfo.GetUtcOffset(DateTime) method is similar in operation to the GetUtcOffset method of the TimeZone class.

추가 정보

적용 대상

GetUtcOffset(DateTimeOffset)

이 표준 시간대의 시간과 특정 날짜 및 시간에 대한 UTC(협정 세계시) 사이의 오프셋 또는 차이를 계산합니다.Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

public:
 TimeSpan GetUtcOffset(DateTimeOffset dateTimeOffset);
public TimeSpan GetUtcOffset (DateTimeOffset dateTimeOffset);
member this.GetUtcOffset : DateTimeOffset -> TimeSpan
Public Function GetUtcOffset (dateTimeOffset As DateTimeOffset) As TimeSpan

매개 변수

dateTimeOffset
DateTimeOffset

오프셋을 확인할 대상 날짜 및 시간입니다.The date and time to determine the offset for.

반환

TimeSpan

UTC(협정 세계시)와 현재 표준 시간대 사이의 시간 차이를 나타내는 개체입니다.An object that indicates the time difference between Coordinated Universal Time (UTC) and the current time zone.

설명

반환 된 시간 범위는 현재 표준 시간대 조정 규칙 애플리케이션으로 인 한 모든 차이 포함합니다.The returned time span includes any differences due to the application of adjustment rules to the current time zone. 이는 BaseUtcOffset utc (협정 세계시)와 표준 시간대의 표준 시간 사이의 차이를 반환 하는 속성과 다르며, 따라서 조정 규칙을 고려 하지 않습니다.It differs from the BaseUtcOffset property, which returns the difference between Coordinated Universal Time (UTC) and the time zone's standard time and, therefore, does not take adjustment rules into account.

적용 대상