zoukankan      html  css  js  c++  java
  • C# TimeZone Examples

    You want to get information about the TimeZone that the current computeris in. Time zones on the planet Earth change on lines on longitude, and you cannotassume the current computer is in any particular time zone reliably. Fortunately,as we demonstrate in this example set, the TimeZone type provides information abouttime zones for C# programmers.

    Time zone names

    What is the name of a specific time zone? The .NET Framework contains informationabout the names for time zones; you can access the properties StandardName and DaylightNameto get these strings. Also, this example as well as the following ones shows howyou can get the current time zone with the TimeZone.CurrentTimeZone property accessor.

    Program that gets time zone names [C#]
    
    using System;
    
    class Program
    {
        static void Main()
        {
    	// Get current time zone.
    	TimeZone zone = TimeZone.CurrentTimeZone;
    	string standard = zone.StandardName;
    	string daylight = zone.DaylightName;
    	Console.WriteLine(standard);
    	Console.WriteLine(daylight);
        }
    }
    
    Output
        (This will depend on your computer's location and system settings.)
    
    Mountain Standard Time
    Mountain Daylight Time

    Get universal time (local time)

    There are two concepts of time you can use: the local time, which depends on wherethe computer is located on the Earth; and the universal time, which is independentof the computer's location. If you use universal times, you can compare times fromdifferent locations with no errors.

    Program that checks universal and local times [C#]
    
    using System;
    
    class Program
    {
        static void Main()
        {
    	TimeZone zone =  TimeZone.CurrentTimeZone;
    	// Demonstrate ToLocalTime and ToUniversalTime.
    	DateTime local = zone.ToLocalTime(DateTime.Now);
    	DateTime universal = zone.ToUniversalTime(DateTime.Now);
    	Console.WriteLine(local);
    	Console.WriteLine(universal);
        }
    }
    
    Output
        (This also depends on your computer's location.)
    
    8/26/2010 1:02:28 PM
    8/26/2010 7:02:28 PM

    Get offsets

    A UTC offset indicates how many hours a time zone differs from the Coordinated UniversalTime (also referred to as Zulu Time or Greenwich Mean Time). When you call GetUtcOffseton a TimeZone instance, you get a TimeSpan instance that indicates how many hoursthe time is different from UTC.

    TimeSpan Examples
    Program that checks UTC offset [C#]
    
    using System;
    
    class Program
    {
        static void Main()
        {
    	TimeZone zone = TimeZone.CurrentTimeZone;
    	// Get offset.
    	TimeSpan offset = zone.GetUtcOffset(DateTime.Now);
    	Console.WriteLine(offset);
        }
    }
    
    Output
        (Results vary on location.)
    
    -06:00:00

    Daylight changes

    Daylight saving time was invented by George Vernon Hudson in New Zealand. Its intentionis to save money by reducing the costs of keeping lights on at night. Today, itsmain purpose is to make software programmers' lives hard. This short program showshow you can get information about the current year's daylight saving changes: whenthe clock is set forward, and when this change is reverted.

    Program that shows daylight changes [C#]
    
    using System;
    using System.Globalization;
    
    class Program
    {
        static void Main()
        {
    	// Get daylight saving information for current time zone.
    	TimeZone zone = TimeZone.CurrentTimeZone;
    	DaylightTime time = zone.GetDaylightChanges(DateTime.Today.Year);
    	Console.WriteLine("Start: {0}", time.Start);
    	Console.WriteLine("End: {0}", time.End);
    	Console.WriteLine("Delta: {0}", time.Delta);
        }
    }
    
    Output
        (Your mileage may vary.)
    
    Start: 3/14/2010 2:00:00 AM
    End: 11/7/2010 2:00:00 AM
    Delta: 01:00:00

    Daylight saving time

    This program shows how you can scan individual dates for their daylight saving status.It is probably better to use the GetDaylightChanges method instead of the IsDaylightSavingTimemethod repeatedly; however, IsDaylightSavingTime can be useful in some cases.

    Program that checks daylight saving time [C#]
    
    using System;
    
    class Program
    {
        static void Main()
        {
    	DateTime d = new DateTime(2010, 1, 1);
    	TimeZone cur = TimeZone.CurrentTimeZone;
    	bool flag = !cur.IsDaylightSavingTime(d);
    
    	for (int i = 0; i < 365; i++)
    	{
    	    bool f = cur.IsDaylightSavingTime(d);
    	    if (f != flag)
    	    {
    		Console.WriteLine("{0}: begin {1}", d, f);
    		flag = f;
    	    }
    	    d = d.AddDays(1);
    	}
        }
    }
    
    Output
        (Depends on your time zone.)
    
    1/1/2010 12:00:00 AM: begin False
    3/15/2010 12:00:00 AM: begin True
    11/8/2010 12:00:00 AM: begin False

    Summary

    The TimeZone type in the C# language and .NET Framework provides a handy way toget information about time zones. You can use the CurrentTimeZone property to getthe information for the computer, and then call methods that reveal informationabout daylight saving time universal times and UTC offsets.

    Time Representations

    作者:Angelo Lee
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    Python基础语法精讲
    使用xlwt 库将数据导入为Excel 表格
    统计指定目录下所有mp4文件的时长(包含子目录下的文件)
    获取一个目录下所有文件(完整路径,要借助os.path.join()方法)
    中国参与MOOC 建设的大学有多少所?
    蒙特卡洛方法应用
    手欠,起了一卦,看那房子的风水,悲剧了
    好无聊,大半夜地找了份传世服务端源码,编着玩
    昨晚房屋的风水
    上午,写了个小东西
  • 原文地址:https://www.cnblogs.com/yefengmeander/p/2887990.html
Copyright © 2011-2022 走看看