What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10557 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > ASP.NET > getting the TIMEZONE information ...
Bageshkumarbagi

getting the TIMEZONE information

 Code Snippet posted by: Bageshkumarbagi | Posted on: 10/13/2012 | Category: ASP.NET Codes | Views: 466 | Status: [Member] | Points: 40 | Alert Moderator   


1. take one dropdownlist
2.one button
3.one multiline textbox

========================================

using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Text;

namespace TimeZoneApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

public struct SYSTEMDATETIME
{
public short syear;
public short smonth;
public short sdayofweek;
public short day;
public short hour;
public short min;
public short sec;
public short millisec;
}
public struct TimezoneBias
{
public Int32 _bias;
public Int32 _standardbias;
public Int32 _daylightbias;
public SYSTEMDATETIME _standarddate;
public SYSTEMDATETIME _daylightdate;
}
public struct TimeZoneInformation
{
private string _keyname;

public string Keyname
{
get { return _keyname; }
set { _keyname = value; }
}

private string _standardname;

public string Standardname
{
get { return _standardname; }
set { _standardname = value; }
}
private string _daylightname;

public string Daylightname
{
get { return _daylightname; }
set { _daylightname = value; }
}

private TimezoneBias _timeZoneBias;

public TimezoneBias TimeZoneBias
{
get { return _timeZoneBias; }
set { _timeZoneBias = value; }
}

private string _displaystring;

public string Displaystring
{
get { return _displaystring; }
set { _displaystring = value; }
}
}
public class TimeZoneComparison : IComparer<TimeZoneInformation>
{
public int Compare(TimeZoneInformation TZI1, TimeZoneInformation TZI2)
{
if (TZI1.TimeZoneBias._bias > TZI2.TimeZoneBias._bias)
return -1;
else if (TZI1.TimeZoneBias._bias < TZI2.TimeZoneBias._bias)
return 1;
else
return 0;
}
}
protected void TimeZone_Click(object sender, EventArgs e)
{

RegistryKey Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones");
List<TimeZoneInformation> arr = new List<TimeZoneInformation>();

foreach (string SubKey in Reg.GetSubKeyNames())
{
RegistryKey KeyValue = Reg.OpenSubKey(SubKey);
TimeZoneInformation TZI = new TimeZoneInformation();
TZI.Keyname = SubKey.ToString();
TZI.Standardname = KeyValue.GetValue("Std").ToString();
TZI.Daylightname = KeyValue.GetValue("Dlt").ToString();
TZI.Displaystring = KeyValue.GetValue("Display").ToString();
TZI.TimeZoneBias = new TimezoneBias();

Byte[] b = (byte[])KeyValue.GetValue("TZI");
IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(TZI.TimeZoneBias));
Marshal.Copy(b, 0, buffer, 44);
TZI.TimeZoneBias = (TimezoneBias)Marshal.PtrToStructure(buffer, typeof(TimezoneBias));
arr.Add(TZI);
}
TimeZoneComparison TZC = new TimeZoneComparison();
arr.Sort(TZC);
DropDownList1.DataTextField = "Displaystring";
DropDownList1.DataValueField = "Keyname";
DropDownList1.DataSource = arr;
DropDownList1.DataBind();
}

public DateTime GetAdjustedDate(DateTime dt,int day)
{
DateTime monthenddate = dt.AddMonths(1).AddDays(-1);
if (dt.DayOfWeek != DayOfWeek.Sunday)
dt = dt.AddDays(7 - (Int32)dt.DayOfWeek);
if (monthenddate.DayOfWeek != DayOfWeek.Sunday)
monthenddate = monthenddate.AddDays((-1) * (Int32)monthenddate.DayOfWeek);

switch (day)
{
case 1:
return dt;
case 2:
return dt.AddDays(7);
case 3:
return dt.AddDays(14);
case 4:
return dt.AddDays(21);
case 5:
return monthenddate;
default:
return dt;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
RegistryKey Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones");
RegistryKey KeyValue = Reg.OpenSubKey(DropDownList1.SelectedItem.Value);
TimeZoneInformation TZI = new TimeZoneInformation();
String daylightdatestring,daylightsavingenddate;
TZI.Displaystring = KeyValue.GetValue("Display").ToString();
Byte[] b = (byte[])KeyValue.GetValue("TZI");

IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(TZI.TimeZoneBias));
Marshal.Copy(b,0,buffer,44);
TZI.TimeZoneBias = (TimezoneBias)Marshal.PtrToStructure(buffer, typeof(TimezoneBias));
if (TZI.TimeZoneBias._daylightdate.smonth > 0)
{
DateTime dt = new DateTime(DateTime.Now.Year, TZI.TimeZoneBias._daylightdate.smonth, 1, TZI.TimeZoneBias._daylightdate.hour, TZI.TimeZoneBias._daylightdate.min,TZI.TimeZoneBias._daylightdate.sec, TZI.TimeZoneBias._daylightdate.millisec);
dt = GetAdjustedDate(dt, TZI.TimeZoneBias._daylightdate.day);
daylightdatestring = dt.ToString();
}
else
{
daylightdatestring = "No Daylight Saving Experienced in this region ";
}

StringBuilder str = new StringBuilder();
str.Append("Time Zone Name Selected : " + TZI.Displaystring + "\n\r");
str.Append("Time Zone Bias From GMT : " + TZI.TimeZoneBias._bias + "\n\r");
str.Append("DayLight Saving starts from : " + daylightdatestring + "\n\r");
SummaryBox.Text = str.ToString();
}
}
}
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 4:24:19 PM