# 方法: 日付と時刻の演算でタイム ゾーンを使用します。How to: Use time zones in date and time arithmetic

### 日付と時刻の演算に調整規則を適用するにはTo apply adjustment rules to date and time arithmetic

1. なんらかの方法を実装して、日付と時刻の値と、その値が属するタイム ゾーンを密接に結び付けます。Implement some method of closely coupling a date and time value with the time zone to which it belongs. たとえば、日付と時刻の値とそのタイム ゾーンの両方を含む構造体を宣言します。For example, declare a structure that includes both the date and time value and its time zone. 次の例にリンクするこの方法を使用して、DateTimeそのタイム ゾーンを持つ値です。The following example uses this approach to link a DateTime value with its time zone.

``````// Define a structure for DateTime values for internal use only
internal struct TimeWithTimeZone
{
TimeZoneInfo TimeZone;
DateTime Time;
}
``````
``````' Define a structure for DateTime values for internal use only
Friend Structure TimeWithTimeZone
Dim TimeZone As TimeZoneInfo
Dim Time As Date
End Structure
``````
2. 呼び出して、時刻を世界協定時刻 (UTC) を変換、ConvertTimeToUtcメソッドまたはConvertTimeメソッドです。Convert a time to Coordinated Universal Time (UTC) by calling either the ConvertTimeToUtc method or the ConvertTime method.

3. UTC 時刻で算術演算を実行します。Perform the arithmetic operation on the UTC time.

4. 時間を UTC から呼び出すことによって、元の時刻のタイム ゾーンに変換、TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo)メソッドです。Convert the time from UTC to the original time's associated time zone by calling the TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo) method.

## 例Example

``````using System;

public struct TimeZoneTime
{
public TimeZoneInfo TimeZone;
public DateTime Time;

public TimeZoneTime(TimeZoneInfo tz, DateTime time)
{
if (tz == null)
throw new ArgumentNullException("The time zone cannot be a null reference.");

this.TimeZone = tz;
this.Time = time;
}

{
// Convert time to UTC
DateTime utcTime = TimeZoneInfo.ConvertTimeToUtc(this.Time, this.TimeZone);
// Add time interval to time
// Convert time back to time in time zone
return new TimeZoneTime(this.TimeZone, TimeZoneInfo.ConvertTime(utcTime,
TimeZoneInfo.Utc, this.TimeZone));
}
}

public class TimeArithmetic
{
public const string tzName = "Central Standard Time";

public static void Main()
{
try
{
TimeZoneTime cstTime1, cstTime2;

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

cstTime1 = new TimeZoneTime(cst, time1);
Console.WriteLine("{0} + {1} hours = {2}", cstTime1.Time,
twoAndAHalfHours.ToString(),
cstTime2.Time);
}
catch
{
Console.WriteLine("Unable to find {0}.", tzName);
}
}
}
``````
``````Public Structure TimeZoneTime
Public TimeZone As TimeZoneInfo
Public Time As Date

Public Sub New(tz As TimeZoneInfo, time As Date)
If tz Is Nothing Then _
Throw New ArgumentNullException("The time zone cannot be a null reference.")

Me.TimeZone = tz
Me.Time = time
End Sub

Public Function AddTime(interval As TimeSpan) As TimeZoneTime
' Convert time to UTC
Dim utcTime As DateTime = TimeZoneInfo.ConvertTimeToUtc(Me.Time, _
Me.TimeZone)
' Add time interval to time
' Convert time back to time in time zone
Return New TimeZoneTime(Me.TimeZone, TimeZoneInfo.ConvertTime(utcTime, _
TimeZoneInfo.Utc, Me.TimeZone))
End Function
End Structure

Module TimeArithmetic
Public Const tzName As String = "Central Standard Time"

Public Sub Main()
Try
Dim cstTime1, cstTime2 As TimeZoneTime

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

cstTime1 = New TimeZoneTime(cst, time1)

Console.WriteLine("{0} + {1} hours = {2}", cstTime1.Time, _
twoAndAHalfHours.ToString(), _
cstTime2.Time)
Catch
Console.WriteLine("Unable to find {0}.", tzName)
End Try
End Sub
End Module
``````

• アプリケーションで使用されるすべての時刻が、特定のタイム ゾーンに属するものと仮定します。Assume that all times used in an application belong to a particular time zone. この方法は、適切な場合もありますが、柔軟性が限られ、移植性が制限される可能性もあります。Although appropriate in some cases, this approach offers limited flexibility and possibly limited portability.

• 日時と関連付けられているタイム ゾーンを型のフィールドとして組み込むことで、両者を密に結合する型を定義します。Define a type that tightly couples a date and time with its associated time zone by including both as fields of the type. コード例ではこの方法を使用して、日時とタイム ゾーンを 2 つのメンバー フィールドに格納する構造体を定義しています。This approach is used in the code example, which defines a structure to store the date and time and the time zone in two member fields.

``````using System;

public struct TimeZoneTime
{
public TimeZoneInfo TimeZone;
public DateTimeOffset Time;

public TimeZoneTime(TimeZoneInfo tz, DateTimeOffset time)
{
if (tz == null)
throw new ArgumentNullException("The time zone cannot be a null reference.");

this.TimeZone = tz;
this.Time = time;
}

{
// Convert time to UTC
DateTimeOffset utcTime = TimeZoneInfo.ConvertTime(this.Time, TimeZoneInfo.Utc);
// Add time interval to time
// Convert time back to time in time zone
return new TimeZoneTime(this.TimeZone, TimeZoneInfo.ConvertTime(utcTime, this.TimeZone));
}
}

public class TimeArithmetic
{
public const string tzName = "Central Standard Time";

public static void Main()
{
try
{
TimeZoneTime cstTime1, cstTime2;

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

cstTime1 = new TimeZoneTime(cst,
new DateTimeOffset(time1, cst.GetUtcOffset(time1)));
Console.WriteLine("{0} + {1} hours = {2}", cstTime1.Time,
twoAndAHalfHours.ToString(),
cstTime2.Time);
}
catch
{
Console.WriteLine("Unable to find {0}.", tzName);
}
}
}
``````
``````Public Structure TimeZoneTime
Public TimeZone As TimeZoneInfo
Public Time As DateTimeOffset

Public Sub New(tz As TimeZoneInfo, time As DateTimeOffset)
If tz Is Nothing Then _
Throw New ArgumentNullException("The time zone cannot be a null reference.")

Me.TimeZone = tz
Me.Time = time
End Sub

Public Function AddTime(interval As TimeSpan) As TimeZoneTime
' Convert time to UTC
Dim utcTime As DateTimeOffset = TimeZoneInfo.ConvertTime(Me.Time, TimeZoneInfo.Utc)
' Add time interval to time
' Convert time back to time in time zone
Return New TimeZoneTime(Me.TimeZone, TimeZoneInfo.ConvertTime(utcTime, Me.TimeZone))
End Function
End Structure

Module TimeArithmetic
Public Const tzName As String = "Central Standard Time"

Public Sub Main()
Try
Dim cstTime1, cstTime2 As TimeZoneTime

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

cstTime1 = New TimeZoneTime(cst, _
New DateTimeOffset(time1, cst.GetUtcOffset(time1)))

Console.WriteLine("{0} + {1} hours = {2}", cstTime1.Time, _
twoAndAHalfHours.ToString(), _
cstTime2.Time)
Catch
Console.WriteLine("Unable to find {0}.", tzName)
End Try
End Sub
End Module
``````

この追加を実行するだけの場合、DateTimeOffset最初 UTC に変換する、結果、適切な時点の反映しますが、そのオフセットは反映されませんを指定されたタイム ゾーンの時点のなしの値します。Note that if this addition is simply performed on the DateTimeOffset value without first converting it to UTC, the result reflects the correct point in time but its offset does not reflect that of the designated time zone for that time.

## コードのコンパイルCompiling the code

この例で必要な要素は次のとおりです。This example requires:

• される System.Core.dll への参照をプロジェクトに追加します。That a reference to System.Core.dll be added to the project.

• Systemと共に名前空間をインポートする、`using`ステートメント (c# コードで必要です)。That the System namespace be imported with the `using` statement (required in C# code).