System.TimeZone 类

方法描述

表示时区。

语法定义(C# System.TimeZone 类 的用法)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class TimeZone

构造函数

构造函数名称 构造函数描述
TimeZone 初始化 TimeZone 类的新实例。

成员/方法

方法名称 方法描述
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetDaylightChanges 返回特定年份的夏时制期间。
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetType 获取当前实例的 Type。 (继承自 Object。)
GetUtcOffset 返回指定本地时间的协调世界时 (UTC) 偏移量。
IsDaylightSavingTime(DateTime) 返回一个值,用以指示指定日期和时间是否处于夏时制期间。
IsDaylightSavingTime(DateTime, DaylightTime) 返回一个值,用以指示指定日期和时间是否处于指定的夏时制期间。
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ToLocalTime 返回对应于指定日期和时间值的本地时间。
ToString 返回表示当前对象的字符串。 (继承自 Object。)
ToUniversalTime 返回对应于指定时间的协调世界时 (UTC)。

提示和注释

时区是使用同一标准时间的地理区域。

重要事项

应尽可能使用 TimeZoneInfo 类替代 TimeZone 类。

可以使用 TimeZone 类来检索有关当前时区的信息,并将时间从本地时间转换为协调世界时 (UTC),反之亦然。 但不可使用 TimeZone 类来表示本地时区以外的时区,或者将日期和时间从一个时区转换到另一个时区。 要进行检索,请使用 TimeZoneInfo 类。 可以使用此类来检索在本地系统上定义的任何时区的信息、创建自定义时区以及将时间从一个时区转换到另一个时区。

TimeZone 类只支持本地时区的单个夏时制调整规则。 因此,TimeZone 类只有在最新调整规则有效期间才能准确报告夏时制信息,或者在 UTC 和本地时间之间进行转换。 相反,TimeZoneInfo 类支持多个调整规则,因而可以处理历史时区数据。

对继承者的说明

除了为其 abstract 成员(这些成员在 Visual Basic 中标记为 MustOverride)提供实现外,重要的是从 TimeZone 派生的类会重写 ToLocalTime 方法的默认行为。 这是因为在 .NET Framework 2.0 版中,ToLocalTime 的默认行为不依赖于对 GetUtcOffset 的调用,而在 .NET Framework 1.0 和 1.1 版中则依赖于该调用。 有关详细信息,请参见 ToLocalTime 方法。

System.TimeZone 类例子

下面的示例引用并显示选定的 TimeZone 类元素。

// Example of selected TimeZone class elements.
using System;
using System.Globalization;

class TimeZoneDemo
{
    static void Main( )
    {
        const string dataFmt = "{0,-30}{1}";
        const string timeFmt = "{0,-30}{1:yyyy-MM-dd HH:mm}";

        Console.WriteLine(
            "This example of selected TimeZone class " +
            "elements generates the following \n" +
            "output, which varies depending on the " +
            "time zone in which it is run.\n" );

        // Get the local time zone and the current local time and year.
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime currentDate = DateTime.Now;
        int      currentYear = currentDate.Year;

        // Display the names for standard time and daylight saving 
        // time for the local time zone.
        Console.WriteLine( dataFmt, "Standard time name:", 
            localZone.StandardName );
        Console.WriteLine( dataFmt, "Daylight saving time name:", 
            localZone.DaylightName );

        // Display the current date and time and show if they occur 
        // in daylight saving time.
        Console.WriteLine( "\n" + timeFmt, "Current date and time:",
            currentDate );
        Console.WriteLine( dataFmt, "Daylight saving time?", 
            localZone.IsDaylightSavingTime( currentDate ) );

        // Get the current Coordinated Universal Time (UTC) and UTC 
        // offset.
        DateTime currentUTC = 
            localZone.ToUniversalTime( currentDate );
        TimeSpan currentOffset = 
            localZone.GetUtcOffset( currentDate );

        Console.WriteLine( timeFmt, "Coordinated Universal Time:", 
            currentUTC );
        Console.WriteLine( dataFmt, "UTC offset:", currentOffset );

        // Get the DaylightTime object for the current year.
        DaylightTime daylight = 
            localZone.GetDaylightChanges( currentYear );

        // Display the daylight saving time range for the current year.
        Console.WriteLine( 
            "\nDaylight saving time for year {0}:", currentYear );
        Console.WriteLine( "{0:yyyy-MM-dd HH:mm} to " +
            "{1:yyyy-MM-dd HH:mm}, delta: {2}", 
            daylight.Start, daylight.End, daylight.Delta );
    } 
} 

/*
This example of selected TimeZone class elements generates the following
output, which varies depending on the time zone in which it is run.

Standard time name:           Pacific Standard Time
Daylight saving time name:    Pacific Daylight Time

Current date and time:        2006-01-06 16:47
Daylight saving time?         False
Coordinated Universal Time:   2006-01-07 00:47
UTC offset:                   -08:00:00

Daylight saving time for year 2006:
2006-04-02 02:00 to 2006-10-29 02:00, delta: 01:00:00
*/

继承层次结构

System.Object

System.TimeZone

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。