zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    高德地图 Android编程中 如何设置使 标记 marker 能够被拖拽
    Android编程 高德地图 中如何重写 定位按键 的触发事件 (com.amap.api.maps2d.LocationSource)点击定位后不仅定位在地图中心点上而且可以设置地图的缩放大小和提示
    Android 编程 高德地图 (实现显示地图以及定位功能)
    Android 编程 AMapLocationClientOption 类中的 setMockEnable (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)
    Android 编程 AMapLocationClientOption 类中的 setNeedAddress 方法用处 (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)
    数据挖掘、目标检测中的cnn和cn---卷积网络和卷积神经网络
    mfc学习之gdi---gdi空间
    图像处理之特效---光剑特效
    模式识别之线性回归---最小二乘和线性回归
    matlab 学习之常用函数2
  • 原文地址:https://www.cnblogs.com/wpsl5168/p/1783788.html
Copyright © 2011-2022 走看看