次の方法で共有


TimeZone.GetUtcOffset メソッド

指定した現地時間の世界協定時刻 (UTC: Coordinated Universal Time) オフセットを返します。

Public MustOverride Function GetUtcOffset( _
   ByVal time As DateTime _) As TimeSpan
[C#]
public abstract TimeSpan GetUtcOffset(DateTimetime);
[C++]
public: virtual TimeSpan GetUtcOffset(DateTimetime) = 0;
[JScript]
public abstract function GetUtcOffset(
   time : DateTime) : TimeSpan;

パラメータ

  • time
    現地の日付と時刻。

戻り値

タイマ刻み単位で計測した、 time からの UTC オフセット。

解説

世界協定時刻 (UTC: Coordinated Universal Time) は、以前はグリニッジ標準時 (GMT: Greenwich Mean Time) と呼ばれていました。現地時間は、使用しているコンピュータの日付と時刻になります。オフセットとは、現地時間と UTC 間の時差です。つまり、次の式で求められます。

現地時間 = UTC + オフセット

time はグレゴリオ暦で、このインスタンスが表すタイム ゾーンにする必要があります。 time が夏時間の場合、このメソッドは夏時間のタイム ゾーンに対する UTC オフセットを返します。このメソッドは、システムから夏時間の規則を取得します。

たとえば、オフセットが -8 時間の米国太平洋標準タイム ゾーンでは、 GetUtcOffset(new DateTime(1999, 1, 1)) は -288000000000 を返します。

使用例

[Visual Basic, C#, C++] GetUtcOffset メソッドを使用して、複数の現地時間の UTC オフセットを返すコード例を次に示します。

 
' Example of the TimeZone.ToLocalTime( DateTime ) and 
' TimeZone.GetUtcOffset( DateTime ) methods.
Imports System
Imports Microsoft.VisualBasic

Module UTCTimeDemo

    Sub Main( )

        Const headFmt As String = "{0,-20}{1,-20}{2,-12}{3}"

        ' Get the local time zone and a base Coordinated Universal 
        ' Time (UTC).
        Dim localZone As TimeZone = TimeZone.CurrentTimeZone
        Dim baseUTC As DateTime = new DateTime( 2000, 1, 1 )

        Console.WriteLine( "This example of " & vbCrLf & _
            "   TimeZone.ToLocalTime( DateTime ) and" & vbCrLf & _
            "   TimeZone.GetUtcOffset( DateTime ) " & vbCrLf & _
            "generates the following output, which varies " & _
            "depending on the time zone " & vbCrLf & "in which " & _
            "it is run. The example creates several Coordinated " & _
            "Universal " & vbCrLf & "Times (UTC), displays the " & _
            "corresponding local times and UTC offsets, " & _
            vbCrLf & "and shows if the times occur in daylight " & _
            "saving time (DST)." & vbCrLf )
        Console.WriteLine( "Local time: {0}" & vbCrLf, _
            localZone.StandardName )

        Console.WriteLine( headFmt, "UTC", "Local Time", _
            " Offset", "DST?" )
        Console.WriteLine( headFmt, "---", "----------", _
            " ------", "----" )

        ' Generate several UTC times.
        Dim loopX As Integer
        For loopX = 0 to 10

            ' Calculate the local time and UTC offset.
            Dim localTime As DateTime = _
                localZone.ToLocalTime( baseUTC )
            Dim localOffset As TimeSpan = _
                localZone.GetUtcOffset( localTime )

            Console.WriteLine( "{0,-20:yyyy-MM-dd HH:mm}" & _
                "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", _
                baseUTC, localTime, localOffset, _
                localZone.IsDaylightSavingTime( localTime ) )
            
            ' Advance to another UTC.
            baseUTC = baseUTC.AddDays( 155.55 )
        Next loopX
    End Sub 
End Module 

' This example of
'    TimeZone.ToLocalTime( DateTime ) and
'    TimeZone.GetUtcOffset( DateTime )
' generates the following output, which varies depending on the time zone
' in which it is run. The example creates several Coordinated Universal
' Times (UTC), displays the corresponding local times and UTC offsets,
' and shows if the times occur in daylight saving time (DST).
' 
' Local time: Pacific Standard Time
' 
' UTC                 Local Time           Offset     DST?
' ---                 ----------           ------     ----
' 2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
' 2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
' 2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
' 2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
' 2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
' 2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
' 2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
' 2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
' 2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
' 2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
' 2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True

[C#] 
// Example of the TimeZone.ToLocalTime( DateTime ) and 
// TimeZone.GetUtcOffset( DateTime ) methods.
using System;

class UTCTimeDemo
{
    static void Main( )
    {
        const string headFmt = "{0,-20}{1,-20}{2,-12}{3}";

        // Get the local time zone and a base Coordinated Universal 
        // Time (UTC).
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime baseUTC = new DateTime( 2000, 1, 1 );

        Console.WriteLine( "This example of \n" +
            "   TimeZone.ToLocalTime( DateTime ) and\n" +
            "   TimeZone.GetUtcOffset( DateTime ) \ngenerates the " +
            "following output, which varies depending on the time " +
            "zone \nin which it is run. The example creates several " +
            "Coordinated Universal \nTimes (UTC), displays the " +
            "corresponding local times and UTC offsets, \nand shows " +
            "if the times occur in daylight saving time (DST)." );
        Console.WriteLine( "\nLocal time: {0}\n", 
            localZone.StandardName );

        Console.WriteLine( headFmt, "UTC", "Local Time", 
            " Offset", "DST?" );
        Console.WriteLine( headFmt, "---", "----------", 
            " ------", "----" );

        // Generate several UTC times.
        for( int loopX = 0; loopX <= 10; loopX++ )
        {
            // Calculate the local time and UTC offset.
            DateTime localTime = localZone.ToLocalTime( baseUTC );
            TimeSpan localOffset = 
                localZone.GetUtcOffset( localTime );

            Console.WriteLine( "{0,-20:yyyy-MM-dd HH:mm}" +
                "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", 
                baseUTC, localTime, localOffset, 
                localZone.IsDaylightSavingTime( localTime ) );
            
            // Advance to another UTC.
            baseUTC = baseUTC.AddDays( 155.55 );
        }
    } 
} 

/*
This example of
   TimeZone.ToLocalTime( DateTime ) and
   TimeZone.GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/ 

[C++] 
// Example of the TimeZone::ToLocalTime( DateTime ) and 
// TimeZone::GetUtcOffset( DateTime ) methods.
#using <mscorlib.dll>
using namespace System;

void main( )
{
    String* headFmt = S"{0,-20}{1,-20}{2,-12}{3}";

    // Get the local time zone and a base Coordinated Universal 
    // Time (UTC).
    TimeZone* localZone = TimeZone::CurrentTimeZone;
    DateTime  baseUTC = DateTime( 2000, 1, 1 );

    Console::WriteLine( S"This example of \n" 
        S"   TimeZone::ToLocalTime( DateTime ) and\n" 
        S"   TimeZone::GetUtcOffset( DateTime ) \ngenerates the " 
        S"following output, which varies depending on the time " 
        S"zone \nin which it is run. The example creates several " 
        S"Coordinated Universal \nTimes (UTC), displays the " 
        S"corresponding local times and UTC offsets, \nand shows " 
        S"if the times occur in daylight saving time (DST)." );
    Console::WriteLine( S"\nLocal time: {0}\n", 
        localZone->StandardName );

    Console::WriteLine( headFmt, S"UTC", S"Local Time", 
        S" Offset", S"DST?" );
    Console::WriteLine( headFmt, S"---", S"----------", 
        S" ------", S"----" );

    // Generate several UTC times.
    for( int loopX = 0; loopX <= 10; loopX++ )
    {
        // Calculate the local time and UTC offset.
        DateTime localTime = localZone->ToLocalTime( baseUTC );
        TimeSpan localOffset = 
            localZone->GetUtcOffset( localTime );

        Console::WriteLine( S"{0,-20:yyyy-MM-dd HH:mm}" 
            S"{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", 
            __box( baseUTC ), __box( localTime ), 
            __box( localOffset ), 
            __box( localZone->IsDaylightSavingTime( localTime ) ) );
        
        // Advance to another UTC.
        baseUTC = baseUTC.AddDays( 155.55 );
    }
} 

/*
This example of
   TimeZone::ToLocalTime( DateTime ) and
   TimeZone::GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/ 

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

TimeZone クラス | TimeZone メンバ | System 名前空間