• 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

  • 相关阅读:
    thinkphp3.2.3版本在windows本地apache环境运行正常,上传到centos服务器apache环境中出现:thinkphp 上传根目录不存在!请尝试手动创建:uploads/
    [POI2013]LUK-Triumphal arch
    【背包问题】
    2016 acm香港网络赛 A题. A+B Problem (FFT)
    tomcat部署项目的三种方式
    仿照ArrayList自己生成的MyList对象
    使用回调函数实现回文判断
    关于angularjs的model的一些问题
    关于使用Tomcat服务器出现413错误的解决办法(Request Entity Too Large)
    关于angularjs+typeahead的整合
  • 原文地址:https://www.cnblogs.com/wpsl5168/p/1783788.html
Copyright © 2020-2023  润新知