Date Time Calculator

最近头疼于时区间的时间转换,所以做个小工具来使用。

支持自适应Daylight Saving Time。有关Daylight Saving Time,请参考:WikiPedia

运行机器需安装.net framework 3.5 +

 

 

 

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections.ObjectModel;
using System.Security.Permissions;
using Microsoft.Win32;

namespace Stefanie.TimeZoneCalculator
{
    
public partial class Form1 : Form
    {
        
private static ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();

        
public Form1()
        {
            InitializeComponent();
            
if (!checkEnveronment())
            {
                
return;
            }
            panelReslut.Visible 
= false;
            initTimeZoneControl();
        }

        
private void btnGo_Click(object sender, EventArgs e)
        {
            
if (!validateField())
            {
                
return;
            }
            DateTime dtLocalDate 
= dtpLocalDate.Value;
            DateTime dtLocalTime 
= dtpLocalTime.Value;
            DateTime dtLocal 
= new DateTime(dtLocalDate.Year, dtLocalDate.Month, dtLocalDate.Day, dtLocalTime.Hour, dtLocalTime.Minute, dtLocalTime.Second);

            ComboBoxItem selectedLocalTZ 
= (ComboBoxItem)cbLocalTimeZone.SelectedItem;
            ComboBoxItem selectedDestTZ 
= (ComboBoxItem)cbDestTimeZone.SelectedItem;
            
string localTimeZoneId = selectedLocalTZ.Value;
            
string destTimeZoneId = selectedDestTZ.Value;

            DateTime targetDateTime 
= TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dtLocal, localTimeZoneId, destTimeZoneId);

            tbResult.Text 
= targetDateTime.ToString("F");
            panelReslut.Visible 
= true;
        }


        
private void initTimeZoneControl()
        {
            
string timeZoneId = string.Empty;
            
string timeZoneDisplayName = string.Empty;

            cbLocalTimeZone.Items.Clear();
            cbDestTimeZone.Items.Clear();

            ComboBoxItem item;
            
foreach (TimeZoneInfo tz in timeZones)
            {
                item 
= new ComboBoxItem();
                item.Text 
= tz.ToString();
                item.Value 
= tz.Id;

                cbLocalTimeZone.Items.Add(item);
                cbDestTimeZone.Items.Add(item);
                
if (tz.Id == TimeZoneInfo.Local.Id)
                {
                    cbLocalTimeZone.SelectedItem 
= item;
                    cbDestTimeZone.SelectedItem 
= item;
                }
            }
        }

        
private bool checkEnveronment()
        {
            
bool b = true;
            
if (!IsDotNetVersionInstalled(350))
            {
                labError.Text 
= ":( Just supported:.Net Framework 3.5 +";
                labError.Visible 
= true;
                b 
= false;
            }
            
return b;
        }

        
private bool validateField()
        {
            
bool b = true;
            
if (cbLocalTimeZone.SelectedItem == null || cbDestTimeZone.SelectedItem == null)
            {
                labError.Text 
= ":( Please check your setting. All fields are required.";
                labError.Visible 
= true;
                b 
= false;
            }
            
return b;
        }


        
private static bool IsDotNetVersionInstalled(int major, int minor, int build)
        {
            
bool result = false;
            
const string regValueName = "Install";

            
const string regPathFormat = "Software\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}";
            
string regPath = string.Format(regPathFormat, major, minor);
            result 
= CheckRegValue(regPath, regValueName);

            
return result;
        }


        
private static bool CheckRegValue(string regPath, string regValueName)
        {
            
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regPath, false))
            {
                
object value = null;
                
if (key != null)
                {
                    value 
= key.GetValue(regValueName);
                }
                
return (value != null && value is int && (int)value == 1);
            }
        }
    }

    
public class ComboBoxItem
    {
        
private string _text = null;
        
private string _value = null;
        
public string Text { get { return this._text; } set { this._text = value; } }
        
public string Value { get { return this._value; } set { this._value = value; } }
        
public override string ToString()
        {
            
return this._text;
        }
    }

}

 

 

Download the Code from here: http://cid-2601e97600c7b866.office.live.com/self.aspx/Public/SourceCode/TimeZoneCalculator.zip

 

posted @ 2010-07-23 15:54  stefanie  阅读(276)  评论(0编辑  收藏  举报