private void GMT(TimeZone t1) { // Probably wont work on MS.NET, but is better than nothing. Where do // we change our implementation to match theirs? Assert.AreEqual("GMT", t1.StandardName, "D01"); Assert.IsTrue("BST" == t1.DaylightName || "IST" == t1.DaylightName, "D02"); DaylightTime d1 = t1.GetDaylightChanges(2002); Assert.AreEqual("03/31/2002 01:00:00", d1.Start.ToString("G"), "D03"); Assert.AreEqual("10/27/2002 02:00:00", d1.End.ToString("G"), "D04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "D05"); DaylightTime d2 = t1.GetDaylightChanges(1996); Assert.AreEqual("03/31/1996 01:00:00", d2.Start.ToString("G"), "D06"); Assert.AreEqual("10/27/1996 02:00:00", d2.End.ToString("G"), "D07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "D08"); DateTime d3 = new DateTime(2002, 2, 25); Assert.AreEqual(false, t1.IsDaylightSavingTime(d3), "D09"); DateTime d4 = new DateTime(2002, 4, 2); Assert.AreEqual(true, t1.IsDaylightSavingTime(d4), "D10"); DateTime d5 = new DateTime(2002, 11, 4); Assert.AreEqual(false, t1.IsDaylightSavingTime(d5), "D11"); Assert.AreEqual(0L, t1.GetUtcOffset(d3).Ticks, "D12"); Assert.AreEqual(36000000000L, t1.GetUtcOffset(d4).Ticks, "D13"); Assert.AreEqual(0L, t1.GetUtcOffset(d5).Ticks, "D14"); }
private void EST(TimeZone t1) { // It could be EST though... //Assert.AreEqual("Eastern Standard Time", t1.StandardName, "B01"); //Assert.AreEqual("Eastern Daylight Time", t1.DaylightName, "B02"); DaylightTime d1 = t1.GetDaylightChanges(2002); Assert.AreEqual("04/07/2002 02:00:00", d1.Start.ToString("G"), "B03"); Assert.AreEqual("10/27/2002 02:00:00", d1.End.ToString("G"), "B04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "B05"); DaylightTime d2 = t1.GetDaylightChanges(1996); Assert.AreEqual("04/07/1996 02:00:00", d2.Start.ToString("G"), "B06"); Assert.AreEqual("10/27/1996 02:00:00", d2.End.ToString("G"), "B07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "B08"); DateTime d3 = new DateTime(2002, 2, 25); Assert.AreEqual(false, t1.IsDaylightSavingTime(d3), "B09"); DateTime d4 = new DateTime(2002, 4, 8); Assert.AreEqual(true, t1.IsDaylightSavingTime(d4), "B10"); DateTime d5 = new DateTime(2002, 11, 4); Assert.AreEqual(false, t1.IsDaylightSavingTime(d5), "B11"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset(d3).Ticks, "B12"); Assert.AreEqual(-144000000000L, t1.GetUtcOffset(d4).Ticks, "B13"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset(d5).Ticks, "B14"); }
private void EST(TimeZone t1) { Assert.IsTrue("EST" == t1.StandardName || "Eastern Standard Time" == t1.StandardName, "B01"); Assert.IsTrue("EDT" == t1.DaylightName || "Eastern Daylight Time" == t1.DaylightName, "B02"); DaylightTime d1 = t1.GetDaylightChanges(2002); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "B05"); DaylightTime d2 = t1.GetDaylightChanges(1996); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "B08"); DateTime d3 = new DateTime(2002, 2, 25); Assert.AreEqual(false, t1.IsDaylightSavingTime(d3), "B09"); DateTime d4 = new DateTime(2002, 4, 8); Assert.AreEqual(true, t1.IsDaylightSavingTime(d4), "B10"); DateTime d5 = new DateTime(2002, 11, 4); Assert.AreEqual(false, t1.IsDaylightSavingTime(d5), "B11"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset(d3).Ticks, "B12"); Assert.AreEqual(-144000000000L, t1.GetUtcOffset(d4).Ticks, "B13"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset(d5).Ticks, "B14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d4.ToUniversalTime(); Assert.AreEqual(false, t1.IsDaylightSavingTime(d6), "B15"); Assert.AreEqual(0, t1.GetUtcOffset(d6).Ticks, "B16"); }
private void CET(TimeZone t1) { Assert.AreEqual("CET", t1.StandardName, "A01"); Assert.AreEqual("CEST", t1.DaylightName, "A02"); DaylightTime d1 = t1.GetDaylightChanges(2002); Assert.AreEqual("03/31/2002 02:00:00", d1.Start.ToString("G"), "A03"); Assert.AreEqual("10/27/2002 03:00:00", d1.End.ToString("G"), "A04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "A05"); DaylightTime d2 = t1.GetDaylightChanges(1996); Assert.AreEqual("03/31/1996 02:00:00", d2.Start.ToString("G"), "A06"); Assert.AreEqual("10/27/1996 03:00:00", d2.End.ToString("G"), "A07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "A08"); DateTime d3 = new DateTime(2002, 2, 25); Assert.AreEqual(false, t1.IsDaylightSavingTime(d3), "A09"); DateTime d4 = new DateTime(2002, 4, 2); Assert.AreEqual(true, t1.IsDaylightSavingTime(d4), "A10"); DateTime d5 = new DateTime(2002, 11, 4); Assert.AreEqual(false, t1.IsDaylightSavingTime(d5), "A11"); Assert.AreEqual(36000000000L, t1.GetUtcOffset(d3).Ticks, "A12"); Assert.AreEqual(72000000000L, t1.GetUtcOffset(d4).Ticks, "A13"); Assert.AreEqual(36000000000L, t1.GetUtcOffset(d5).Ticks, "A14"); }
private void NZST(TimeZone t1) { Assert.AreEqual("NZST", t1.StandardName, "E01"); Assert.AreEqual("NZDT", t1.DaylightName, "E02"); DaylightTime d1 = t1.GetDaylightChanges(2013); Assert.AreEqual("09/29/2013 02:00:00", d1.Start.ToString("G"), "E03"); Assert.AreEqual("04/07/2013 03:00:00", d1.End.ToString("G"), "E04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "E05"); DaylightTime d2 = t1.GetDaylightChanges(2001); Assert.AreEqual("10/07/2001 02:00:00", d2.Start.ToString("G"), "E06"); Assert.AreEqual("03/18/2001 03:00:00", d2.End.ToString("G"), "E07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "E08"); DateTime d3 = new DateTime(2013, 02, 15); Assert.AreEqual(true, t1.IsDaylightSavingTime(d3), "E09"); DateTime d4 = new DateTime(2013, 04, 30); Assert.AreEqual(false, t1.IsDaylightSavingTime(d4), "E10"); DateTime d5 = new DateTime(2013, 11, 03); Assert.AreEqual(true, t1.IsDaylightSavingTime(d5), "E11"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset(d3).Ticks, "E12"); Assert.AreEqual(36000000000L /*hour*/ * 12L, t1.GetUtcOffset(d4).Ticks, "E13"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset(d5).Ticks, "E14"); }
/// <summary> /// Calculate the Chronological Modified Julian Date from the Julian Date /// </summary> /// <param name="julianDate">Julian Date</param> /// <returns>Chronological Modified Julian Date</returns> public static double CalculateChronologicalModifiedJulianDate(double julianDate) { TimeZone zone = TimeZone.CurrentTimeZone; DaylightTime time = zone.GetDaylightChanges(year: DateTime.Today.Year); return(julianDate - doubleModifiedJulianDateCoefficient + 0.5 + (time.Delta.Hours / 24.0)); }
/// <summary> /// Calculate the Chronological Julian Date from the date /// </summary> /// <param name="date">date</param> /// <returns>Chronological Julian Date</returns> public static double CalculateChronologicalJulianDate(DateTime date) { TimeZone zone = TimeZone.CurrentTimeZone; DaylightTime time = zone.GetDaylightChanges(year: DateTime.Today.Year); return(CalculateJulianDate(date: date) + 0.5 + (time.Delta.Hours / 24.0)); }
public static void MakeNow(this IfcLocalTime lt) { DateTimeOffset localTime = DateTimeOffset.Now; lt.HourComponent = localTime.Hour; lt.SecondComponent = localTime.Second; lt.MinuteComponent = localTime.Minute; IfcCoordinatedUniversalTimeOffset coordinatedUniversalTimeOffset = lt.Model.Instances.New <IfcCoordinatedUniversalTimeOffset>(); coordinatedUniversalTimeOffset.HourOffset = new IfcHourInDay(localTime.Offset.Hours); coordinatedUniversalTimeOffset.MinuteOffset = new IfcMinuteInHour(localTime.Offset.Minutes); if (localTime.Offset.Hours < 0 || (localTime.Offset.Hours == 0 && localTime.Offset.Minutes < 0)) { coordinatedUniversalTimeOffset.Sense = IfcAheadOrBehind.BEHIND; } else { coordinatedUniversalTimeOffset.Sense = IfcAheadOrBehind.AHEAD; } lt.Zone = coordinatedUniversalTimeOffset; TimeZone tz = TimeZone.CurrentTimeZone; DaylightTime dt = tz.GetDaylightChanges(localTime.Year); lt.DaylightSavingOffset = dt.Delta.Hours; }
/// <summary> /// Calculate the Chronological Modified Julian Date /// </summary> /// <returns>Chronological Modified Julian Date</returns> public static double CalculateChronologicalModifiedJulianDate() { TimeZone zone = TimeZone.CurrentTimeZone; DaylightTime time = zone.GetDaylightChanges(year: DateTime.Today.Year); //System.Windows.Forms.MessageBox.Show((time.Delta.Hours * (24 / 100)).ToString()); //double a = time.Delta.Hours / 24.0; return(CalculateJulianDate() - doubleModifiedJulianDateCoefficient + 0.5 + (time.Delta.Hours / 24.0)); }
static void Main(string[] args) { const string dataFmt = "{0,-30}{1}"; const string timeFmt = "{0,-30}{1:yyyy-MM-dd HH:mm}"; Console.WriteLine( "This example of selected TimeZone class " + "elements generates the following \n" + "output, which varies depending on the " + "time zone in which it is run. \n"); //Get the local time zone and the current local time and year TimeZone localZone = TimeZone.CurrentTimeZone; DateTime currentDate = DateTime.Now; int currentYear = currentDate.Year; //Display the names for standard time and daylight saving //time for the local time zone Console.WriteLine(dataFmt, "Standard time name:", localZone.StandardName); Console.WriteLine(dataFmt, "Daylight saving time name:", localZone.DaylightName); // Display the current date and time and show if they occur // in daylight saving time. Console.WriteLine("\n" + timeFmt, "Current date and time:", currentDate); Console.WriteLine(dataFmt, "Daylight saving time?", localZone.IsDaylightSavingTime(currentDate)); // Get the current Coordinated Universal Time (UTC) and UTC // offset. DateTime currentUTC = localZone.ToUniversalTime(currentDate); TimeSpan currentOffset = localZone.GetUtcOffset(currentDate); Console.WriteLine(timeFmt, "Coordinated Universal Time:", currentUTC); Console.WriteLine(dataFmt, "UTC offset:", currentOffset); // Get the DaylightTime object for the current year. DaylightTime daylight = localZone.GetDaylightChanges(currentYear); // Display the daylight saving time range for the current year. Console.WriteLine( "\nDaylight saving time for year {0}:", currentYear); Console.WriteLine("{0:yyyy-MM-dd HH:mm} to " + "{1:yyyy-MM-dd HH:mm}, delta: {2}", daylight.Start, daylight.End, daylight.Delta); }
private int ConvertToTimestamp(DateTime value) { TimeZoneInfo NYTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time"); DateTime NyTime = TimeZoneInfo.ConvertTime(value, NYTimeZone); TimeZone localZone = TimeZone.CurrentTimeZone; System.Globalization.DaylightTime dst = localZone.GetDaylightChanges(NyTime.Year); NyTime = NyTime.AddHours(-1); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime(); TimeSpan span = (NyTime - epoch); return((int)Convert.ToDouble(span.TotalSeconds)); }
public static long ConvertToTimestamp(DateTime value) { TimeZoneInfo NYTimeZone = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time"); // (GMT+07:00) Bangkok, Hanoi, Jakarta DateTime NyTime = TimeZoneInfo.ConvertTime(value, NYTimeZone); TimeZone localZone = TimeZone.CurrentTimeZone; System.Globalization.DaylightTime dst = localZone.GetDaylightChanges(NyTime.Year); NyTime = NyTime.AddHours(-1); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime(); TimeSpan span = (NyTime - epoch); return((long)Convert.ToDouble(span.TotalSeconds)); }
private void EST(TimeZone t1) { Assert.IsTrue("EST" == t1.StandardName || "Eastern Standard Time" == t1.StandardName, "B01"); Assert.IsTrue("EDT" == t1.DaylightName || "Eastern Daylight Time" == t1.DaylightName, "B02"); DaylightTime d1 = t1.GetDaylightChanges(2002); Assert.AreEqual("04/07/2002 02:00:00", d1.Start.ToString("G", CultureInfo.InvariantCulture), "B03"); Assert.AreEqual("10/27/2002 02:00:00", d1.End.ToString("G", CultureInfo.InvariantCulture), "B04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "B05"); DaylightTime d2 = t1.GetDaylightChanges(1996); Assert.AreEqual("03/31/1996 02:00:00", d2.Start.ToString("G", CultureInfo.InvariantCulture), "B06"); Assert.AreEqual("10/27/1996 03:00:00", d2.End.ToString("G", CultureInfo.InvariantCulture), "B07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "B08"); DateTime d3 = new DateTime(2002, 2, 25); Assert.AreEqual(false, t1.IsDaylightSavingTime(d3), "B09"); DateTime d4 = new DateTime(2002, 4, 8); Assert.AreEqual(true, t1.IsDaylightSavingTime(d4), "B10"); DateTime d5 = new DateTime(2002, 11, 4); Assert.AreEqual(false, t1.IsDaylightSavingTime(d5), "B11"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset(d3).Ticks, "B12"); Assert.AreEqual(-144000000000L, t1.GetUtcOffset(d4).Ticks, "B13"); Assert.AreEqual(-180000000000L, t1.GetUtcOffset(d5).Ticks, "B14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d4.ToUniversalTime(); Assert.AreEqual(false, t1.IsDaylightSavingTime(d6), "B15"); Assert.AreEqual(0, t1.GetUtcOffset(d6).Ticks, "B16"); }
private void CET(TimeZone t1) { Assert.IsTrue("CET" == t1.StandardName || "W. Europe Standard Time" == t1.StandardName, "A01"); Assert.IsTrue("CEST" == t1.DaylightName || "W. Europe Daylight Time" == t1.DaylightName, "A02"); DaylightTime d1 = t1.GetDaylightChanges(2002); Assert.AreEqual("03/31/2002 02:00:00", d1.Start.ToString("G", CultureInfo.InvariantCulture), "A03"); Assert.AreEqual("10/27/2002 03:00:00", d1.End.ToString("G", CultureInfo.InvariantCulture), "A04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "A05"); DaylightTime d2 = t1.GetDaylightChanges(1996); Assert.AreEqual("03/31/1996 02:00:00", d2.Start.ToString("G", CultureInfo.InvariantCulture), "A06"); Assert.AreEqual("10/27/1996 03:00:00", d2.End.ToString("G", CultureInfo.InvariantCulture), "A07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "A08"); DateTime d3 = new DateTime(2002, 2, 25); Assert.AreEqual(false, t1.IsDaylightSavingTime(d3), "A09"); DateTime d4 = new DateTime(2002, 4, 2); Assert.AreEqual(true, t1.IsDaylightSavingTime(d4), "A10"); DateTime d5 = new DateTime(2002, 11, 4); Assert.AreEqual(false, t1.IsDaylightSavingTime(d5), "A11"); Assert.AreEqual(36000000000L, t1.GetUtcOffset(d3).Ticks, "A12"); Assert.AreEqual(72000000000L, t1.GetUtcOffset(d4).Ticks, "A13"); Assert.AreEqual(36000000000L, t1.GetUtcOffset(d5).Ticks, "A14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d4.ToUniversalTime(); Assert.AreEqual(false, t1.IsDaylightSavingTime(d6), "A15"); Assert.AreEqual(0, t1.GetUtcOffset(d6).Ticks, "A16"); }
private void NZST(TimeZone t1) { Assert.AreEqual("NZST", t1.StandardName, "E01"); Assert.AreEqual("NZDT", t1.DaylightName, "E02"); DaylightTime d1 = t1.GetDaylightChanges(2013); Assert.AreEqual("09/29/2013 02:00:00", d1.Start.ToString("G", CultureInfo.InvariantCulture), "E03"); Assert.AreEqual("04/07/2013 03:00:00", d1.End.ToString("G", CultureInfo.InvariantCulture), "E04"); Assert.AreEqual(36000000000L, d1.Delta.Ticks, "E05"); DaylightTime d2 = t1.GetDaylightChanges(2001); Assert.AreEqual("10/07/2001 02:00:00", d2.Start.ToString("G", CultureInfo.InvariantCulture), "E06"); Assert.AreEqual("03/18/2001 03:00:00", d2.End.ToString("G", CultureInfo.InvariantCulture), "E07"); Assert.AreEqual(36000000000L, d2.Delta.Ticks, "E08"); DateTime d3 = new DateTime(2013, 02, 15); Assert.AreEqual(true, t1.IsDaylightSavingTime(d3), "E09"); DateTime d4 = new DateTime(2013, 04, 30); Assert.AreEqual(false, t1.IsDaylightSavingTime(d4), "E10"); DateTime d5 = new DateTime(2013, 11, 03); Assert.AreEqual(true, t1.IsDaylightSavingTime(d5), "E11"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset(d3).Ticks, "E12"); Assert.AreEqual(36000000000L /*hour*/ * 12L, t1.GetUtcOffset(d4).Ticks, "E13"); Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset(d5).Ticks, "E14"); // Test TimeZone methods with UTC DateTime in DST. DateTime d6 = d5.ToUniversalTime(); Assert.AreEqual(false, t1.IsDaylightSavingTime(d6), "E15"); Assert.AreEqual(0, t1.GetUtcOffset(d6).Ticks, "E16"); }
public void ToLocalTimeAtDSTBoundaries() { TimeZone tz = TimeZone.CurrentTimeZone; DateTime dst_start_utc = tz.GetDaylightChanges(2007).Start.ToUniversalTime(); if (dst_start_utc == DateTime.MinValue) { Assert.Ignore("Couldn't get beginning of daylight saving time in 2007."); } Assert.IsTrue(tz.ToLocalTime(dst_start_utc.Subtract(new TimeSpan(0, 1, 0))) < tz.ToLocalTime(dst_start_utc), "0:1:59 < 0:3:00"); Assert.IsTrue(tz.ToLocalTime(dst_start_utc) < tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(0, 1, 0))), "0:3:00 < 0:3:01"); Assert.IsTrue(tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(0, 1, 0))) < tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(0, 59, 0))), "0:3:01 < 0:3:59"); Assert.IsTrue(tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(0, 59, 0))) < tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(1, 0, 0))), "0:3:59 < 0:4:00"); Assert.IsTrue(tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(1, 0, 0))) < tz.ToLocalTime(dst_start_utc.Add(new TimeSpan(1, 1, 0))), "0:4:00 < 0:4:01"); }
public static DateTime GetDateTime(DateTime dt, string timeZone = "") { if (!String.IsNullOrEmpty(timeZone)) { TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone); dt = TimeZoneInfo.ConvertTimeFromUtc(dt, timeInfo); } else { TimeZone localZone = TimeZone.CurrentTimeZone; TimeSpan currentOffset = localZone.GetUtcOffset(dt); DaylightTime daylight = localZone.GetDaylightChanges(dt.Year); dt = dt.Add(currentOffset); } return(dt); }
private void WorkOutTime() { Invoke(new Action(() => { // Get DaylightTime object System.Globalization.DaylightTime dl = curTimeZone.GetDaylightChanges(DateTime.Now.Year); // What is GMT (also called Coordinated Universal Time (UTC) DateTime curUTC = curTimeZone.ToUniversalTime(DateTime.Now); // What is GMT/UTC offset ? TimeSpan currentOffset = curTimeZone.GetUtcOffset(DateTime.Now); //---------------------------------------------------------------- lbl_local_tz.Text = TimeZoneInfo.Local.DisplayName; // Difference between standard time and the daylight-saving time. TimeZoneInfo localZone = TimeZoneInfo.Local; string answer = (localZone.BaseUtcOffset >= TimeSpan.Zero) ? "later" : "earlier"; lbl_time_info.Text = "Local time now is " + dl.Delta + " (hh:mm:ss) " + answer + " than UTC."; lbl_UTC.Text = curUTC.ToString(); lbl_local_time.Text = DateTime.Now.ToString(); lbl_utc_offset.Text = currentOffset.ToString(); lbl_DST_setting.Text = curTimeZone.IsDaylightSavingTime(DateTime.Now) ? "On" : "Off"; lbl_dst_start.Text = dl.Start.ToString(); lbl_dst_ends.Text = dl.End.ToString(); lbl_timezone_name.Text = curTimeZone.StandardName; lbl_daylight_name.Text = curTimeZone.DaylightName; //--------------------------------------------------------------------------- DateTime timeUtc = curTimeZone.ToUniversalTime(DateTime.Now); var dt = DateTime.SpecifyKind(timeUtc, DateTimeKind.Utc); //TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); TimeZoneInfo tZone = TimeZoneInfo.FindSystemTimeZoneById(cmbobx_tzones.SelectedItem.ToString()); DateTime TheTime = TimeZoneInfo.ConvertTimeFromUtc(dt, tZone); lbl_time_in_tz.Text = TheTime.ToString(); })); }
private static void Main() { Action <string> display = Console.WriteLine; display("Time Zone"); TimeZoneInfo tzi = TimeZoneInfo.Local; display($"time zone Info standard name is {tzi.StandardName}"); display($"time zone Info ID is {tzi.Id}"); display($"{TimeZoneInfo.GetSystemTimeZones()}"); display($"time zone Info standard name is {tzi.StandardName}"); display($"time zone Info ID is {tzi.Id}"); TimeZone localZone = TimeZone.CurrentTimeZone; DateTime currentDate = DateTime.Now; string currentTime = DateTime.Now.ToLongTimeString(); display($"standard name is {localZone.StandardName}"); display($"day light name is {localZone.DaylightName}"); display($"get daylight changes is {localZone.GetDaylightChanges(DateTime.Now.Year)}"); display($"Universal time is {localZone.ToUniversalTime(DateTime.Now)}"); display($"current time is {currentTime}"); display($"current date is {currentDate}"); //foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones()) // Console.WriteLine(z.Id); TimeZoneInfo timeZone1; display($"US Mountain Standard Time"); DateTime dateTime; //Set the time zone information to US Mountain Standard Time timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time"); //Get date and time in US Mountain Standard Time dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZone1); //Print out the date and time Console.WriteLine(dateTime.ToString("yyyy-MM-dd HH-mm-ss")); display(string.Empty); display("Press any key to exit:"); Console.ReadKey(); }
public void GetUtcOffsetAtDSTBoundary() { /* * Getting a definitive list of timezones which do or don't observe Daylight * Savings is difficult (can't say America's or USA definitively) and lengthy see * * http://en.wikipedia.org/wiki/Daylight_saving_time_by_country * * as a good starting point for a list. * * The following are SOME of the timezones/regions which do support daylight savings. * * Pacific/Auckland * Pacific/Sydney * USA (EST, CST, MST, PST, AKST) note this does not cover all states or regions * Europe/London (GMT) * CET (member states of the European Union) * * This test should work in all the above timezones */ TimeZone tz = TimeZone.CurrentTimeZone; int year = DateTime.Now.Year; DaylightTime daylightChanges = tz.GetDaylightChanges(year); DateTime dst_end = daylightChanges.End; if (dst_end == DateTime.MinValue) { Assert.Ignore(tz.StandardName + " did not observe daylight saving time during " + year + "."); } var standardOffset = tz.GetUtcOffset(daylightChanges.Start.AddMinutes(-1)); var dstOffset = tz.GetUtcOffset(daylightChanges.Start.AddMinutes(61)); Assert.AreEqual(standardOffset, tz.GetUtcOffset(dst_end)); Assert.AreEqual(dstOffset, tz.GetUtcOffset(dst_end.Add(daylightChanges.Delta.Negate().Add(TimeSpan.FromSeconds(1))))); Assert.AreEqual(dstOffset, tz.GetUtcOffset(dst_end.Add(daylightChanges.Delta.Negate()))); Assert.AreEqual(dstOffset, tz.GetUtcOffset(dst_end.Add(daylightChanges.Delta.Negate().Add(TimeSpan.FromSeconds(-1))))); }
public static void Main() { TimeZone zone = TimeZone.CurrentTimeZone; System.Console.WriteLine(zone.StandardName); System.Console.WriteLine(zone.DaylightName); DateTime dtNow = DateTime.Now; DateTime dt2 = new DateTime(2020, 12, 12); System.Console.WriteLine(zone.IsDaylightSavingTime(dtNow)); System.Console.WriteLine(zone.IsDaylightSavingTime(dt2)); System.Console.WriteLine(zone.GetUtcOffset(dtNow)); System.Console.WriteLine(zone.GetUtcOffset(dt2)); System.Console.WriteLine(); DaylightTime day = zone.GetDaylightChanges(2020); System.Console.WriteLine("2020 DST Begin: {0}", day.Start.ToString()); System.Console.WriteLine("2020 DST Ends: {0}", day.End.ToString()); System.Console.WriteLine("Time delta: {0}", day.Delta); }
public static string GetDateWithFormat(string dt, string timeZone = "") { DateTime convertedDateTime; var isValidFormat = DateTime.TryParse(dt, out convertedDateTime); if (isValidFormat) { if (!String.IsNullOrEmpty(timeZone)) { TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone); dt = TimeZoneInfo.ConvertTimeFromUtc(convertedDateTime, timeInfo).ToString("MM/dd/yyyy"); } else { TimeZone localZone = TimeZone.CurrentTimeZone; TimeSpan currentOffset = localZone.GetUtcOffset(convertedDateTime); DaylightTime daylight = localZone.GetDaylightChanges(convertedDateTime.Year); convertedDateTime = convertedDateTime.Add(currentOffset); //convertedDateTime = convertedDateTime.Add(daylight.Delta); // If required then later we can add return(convertedDateTime.ToString("MM/dd/yyyy")); } } return(dt); }
private void FormInfo_Load(object sender, EventArgs e) { TimeZone localZone = TimeZone.CurrentTimeZone; DaylightTime daylight = localZone.GetDaylightChanges(DateTime.Now.Year); this.textBox.Text = "Timezone: " + localZone.StandardName + " \r\nDaylight saving time name: " + localZone.DaylightName + " \r\nDaylight saving time: " + localZone.IsDaylightSavingTime(DateTime.Now) + " \r\nDaylight start: " + daylight.Start.ToString() + " \r\nDaylight end: " + daylight.End.ToString() + " \r\nDelta: " + daylight.Delta + " \r\nUTC time: " + localZone.ToUniversalTime(DateTime.Now) + " \r\nUTC offset: " + localZone.GetUtcOffset(DateTime.Now) ; this.textBox.SelectionStart = 0; this.textBox.SelectionLength = 0; }
public static void Show() { // DateTime and Time Zones { // When you compare two DateTime instances, only their ticks values are compared; their DateTimeKinds are ignored: DateTime dt1 = new DateTime(2000, 1, 1, 10, 20, 30, DateTimeKind.Local); DateTime dt2 = new DateTime(2000, 1, 1, 10, 20, 30, DateTimeKind.Utc); Console.WriteLine(dt1 == dt2); // True DateTime local = DateTime.Now; DateTime utc = local.ToUniversalTime(); Console.WriteLine(local == utc); // False // You can construct a DateTime that differs from another only in Kind with the static DateTime.SpecifyKind method: DateTime d = new DateTime(2000, 12, 12); // Unspecified DateTime utc2 = DateTime.SpecifyKind(d, DateTimeKind.Utc); Console.WriteLine(utc2); // 12/12/2000 12:00:00 AM } // DateTimeOffset and Time Zones { // Comparisons look only at the (UTC) DateTime; the Offset is used primarily for formatting. DateTimeOffset local = DateTimeOffset.Now; DateTimeOffset utc = local.ToUniversalTime(); // 只会影响 UTC 偏移量 Console.WriteLine(local.Offset); // -06:00:00 (in Central America) Console.WriteLine(utc.Offset); // 00:00:00 Console.WriteLine(local == utc); // True //To include the Offset in the comparison, you must use the EqualsExact method: Console.WriteLine(local.EqualsExact(utc)); // False } // TimeZone { // The static TimeZone.CurrentTimeZone method returns a TimeZone object based on the current local settings. TimeZone zone = TimeZone.CurrentTimeZone; zone.StandardName.Dump("StandardName"); zone.DaylightName.Dump("DaylightName"); // The IsDaylightSavingTime and GetUtcOffset methods work as follows: DateTime dt1 = new DateTime(2008, 1, 1); DateTime dt2 = new DateTime(2008, 6, 1); zone.IsDaylightSavingTime(dt1).Dump("IsDaylightSavingTime (January)"); zone.IsDaylightSavingTime(dt2).Dump("IsDaylightSavingTime (June)"); zone.GetUtcOffset(dt1).Dump("UTC Offset (January)"); zone.GetUtcOffset(dt2).Dump("UTC Offset (June)"); // The GetDaylightChanges method returns specific daylight saving information for a given year: DaylightTime day = zone.GetDaylightChanges(2010); // 返回指定年份的夏令时信息 if (day == null) { return; } day.Start.Dump("day.Start"); day.End.Dump("day.End"); day.Delta.Dump("day.Delta"); } //TODO: 夏令时 的概念 暂时不了解 // Daylight Saving and DateTime { // The IsDaylightSavingTime tells you whether a given local DateTime is subject to daylight saving. Console.WriteLine(DateTime.Now.IsDaylightSavingTime()); // True or False // UTC times always return false: Console.WriteLine(DateTime.UtcNow.IsDaylightSavingTime()); // Always False // The end of daylight saving presents a particular complication for algorithms that use local time. // The comments on the right show the results of running this in a daylight-saving-enabled zone: DaylightTime changes = TimeZone.CurrentTimeZone.GetDaylightChanges(2010); TimeSpan halfDelta = new TimeSpan(changes.Delta.Ticks / 2); DateTime utc1 = changes.End.ToUniversalTime() - halfDelta; DateTime utc2 = utc1 - changes.Delta; // Converting these variables to local times demonstrates why you should use UTC and not local time // if your code relies on time moving forward: DateTime loc1 = utc1.ToLocalTime(); // (Pacific Standard Time) DateTime loc2 = utc2.ToLocalTime(); Console.WriteLine(loc1); // 2/11/2010 1:30:00 AM Console.WriteLine(loc2); // 2/11/2010 1:30:00 AM Console.WriteLine(loc1 == loc2); // True // Despite loc1 and loc2 reporting as equal, they are different inside: Console.WriteLine(loc1.ToString("o")); // 2010-11-02T02:30:00.0000000-08:00 Console.WriteLine(loc2.ToString("o")); // 2010-11-02T02:30:00.0000000-07:00 // The extra bit ensures correct round-tripping between local and UTC times: Console.WriteLine(loc1.ToUniversalTime() == utc1); // True Console.WriteLine(loc2.ToUniversalTime() == utc2); // True } }
public override System.Globalization.DaylightTime GetDaylightChanges(int year) { return(currentTimeZone.GetDaylightChanges(year)); }
/** * Calculates the 1900 based numerical value based upon the utc value held * in the date object * * @param adjust TRUE if we want to incorporate timezone information * into the raw UTC date eg. when copying from a spreadsheet */ private void calculateValue(bool adjust) { // Offsets for current time zone long zoneOffset = 0; long dstOffset = 0; // Get the timezone and dst offsets if we want to take these into // account if (adjust) { // Get the current calender, replete with timezone information //Calendar cal = Calendar.CurrentEra(); //cal.setTime(date); //zoneOffset = cal[Calendar.ZONE_OFFSET]; //dstOffset = cal[Calendar.DST_OFFSET]; // TODO: fix this data handling.... DateTime now = DateTime.Now; TimeZone zone = TimeZone.CurrentTimeZone; zoneOffset = (long)zone.GetUtcOffset(now).TotalMilliseconds; DaylightTime daylight = zone.GetDaylightChanges(now.Year); if (daylight != null) { dstOffset = (long)daylight.Delta.TotalMilliseconds; } else { dstOffset = 0; } if (now.IsDaylightSavingTime()) { zoneOffset += dstOffset; } //zoneOffset = cal[Calendar.ZONE_OFFSET]; //dstOffset = cal[Calendar.DST_OFFSET]; } long utcValue = date.GetTime() + zoneOffset + dstOffset; // Convert this to the number of days, plus fractions of a day since // 01 Jan 1970 double utcDays = (double)utcValue / (double)msInADay; // Add in the offset to get the number of days since 01 Jan 1900 value = utcDays + utcOffsetDays; // Work round a bug in excel. Excel seems to think there is a date // called the 29th Feb, 1900 - but this was not a leap year. // Therefore for values less than 61, we must subtract 1. Only do // this for full dates, not times if (!time && value < nonLeapDay) { value -= 1; } // If this refers to a time, then get rid of the integer part if (time) { value = value - (int)value; } }
public override DaylightTime GetDaylightChanges(int year) { return(internalTimeZone.GetDaylightChanges(year)); }
static void CompareTimeZoneData(TimeZone tz, TimeZoneInfo tzi, Java.Util.TimeZone jtz) { Console.WriteLine("## Comparing TimeZone Data:"); Console.WriteLine("# TimeZone: StandardName={0}; DaylightName={1}", tz.StandardName, tz.DaylightName); Console.WriteLine("# TimeZoneInfo: StandardName={0}; DaylightName={1}; DisplayName={2}; Id={3}", tzi.StandardName, tzi.DaylightName, tzi.DisplayName, tzi.Id); Console.WriteLine("# Java TimeZone: DisplayName={0}; ID={1}", jtz.DisplayName, jtz.ID); bool found_errors = false; for (int year = 2012; year < 2015; ++year) { if (tz != null) { var dst = tz.GetDaylightChanges(year); Console.WriteLine("Year: {0}; DST: {1} -> {2}", year, dst.Start.ToString("g"), dst.End.ToString("g")); } for (int month = 1; month <= 12; month++) { int lastDayInMonth = DateTime.DaysInMonth(year, month); for (int day = 1; day <= lastDayInMonth; day++) { var localtime = new DateTime(year, month, day, 10, 00, 00, DateTimeKind.Local); var utctime = localtime.ToUniversalTime(); var tz_offset = tz.GetUtcOffset(localtime); var tzi_offset = tzi.GetUtcOffset(localtime); var tz_dst = tz.IsDaylightSavingTime(localtime); var tzi_dst = tzi.IsDaylightSavingTime(localtime); using (var jd = ToDate(localtime)) { var jtz_dst = jtz.InDaylightTime(jd); var ms = (int)(localtime - new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Local)).TotalMilliseconds; var jtz_offseti = jtz.GetOffset(1, year, month - 1, day, ((int)localtime.DayOfWeek) + 1, ms); var jtz_offset = TimeSpan.FromMilliseconds(jtz_offseti); if (tz_offset != tzi_offset || tz_offset != jtz_offset || tzi_offset != jtz_offset || tz_dst != tzi_dst || tz_dst != jtz_dst || tzi_dst != jtz_dst) { found_errors = true; Console.WriteLine("MISMATCH! @ {0} [{1}]", localtime, jd.ToLocaleString()); Console.WriteLine("\t TimeZone Offset: {0}", tz_offset); Console.WriteLine("\t TimeZoneInfo Offset: {0}", tzi_offset); Console.WriteLine("\tJava TimeZone Offset: {0}", jtz_offset); Console.WriteLine("\t TimeZone DST: {0}", tz_dst); Console.WriteLine("\t TimeZoneInfo DST: {0}", tzi_dst); Console.WriteLine("\t Java TimeZone DST: {0}", jtz_dst); } } } } } if (found_errors) { var rules = tzi.GetAdjustmentRules(); for (int i = 0; i < rules.Length; ++i) { var rule = rules [i]; Console.WriteLine("# AdjustmentRules[{0}]: DaylightDelta={1}; DateStart={2}; DateEnd={3}; DaylightTransitionStart={4}/{5} @ {6}; DaylightTransitionEnd={7}/{8} @ {9}", i, rule.DaylightDelta, rule.DateStart.ToString("yyyy-MM-dd"), rule.DateEnd.ToString("yyyy-MM-dd"), rule.DaylightTransitionStart.Month, rule.DaylightTransitionStart.Day, rule.DaylightTransitionStart.TimeOfDay.ToString("hh:mm:ss"), rule.DaylightTransitionEnd.Month, rule.DaylightTransitionEnd.Day, rule.DaylightTransitionEnd.TimeOfDay.ToString("hh:mm:ss")); } } Assert.IsFalse(found_errors, "TimeZoneData MISMATCH! See logcat output for details."); }
static void Main(string[] args) { try { const string dataFmt = "{0,-30}{1}"; const string timeFmt = "{0,-30}{1:MM-dd-yyyy HH:mm}"; TimeZone curTimeZone = TimeZone.CurrentTimeZone; // What is TimeZone name? System.Console.WriteLine(dataFmt, "TimeZone Name:", curTimeZone.StandardName); // Is TimeZone DayLight Saving?| System.Console.WriteLine(dataFmt, "Daylight saving time?", curTimeZone.IsDaylightSavingTime(DateTime.Now)); // What is GMT (also called Coordinated Universal Time (UTC) DateTime curUTC = curTimeZone.ToUniversalTime(DateTime.Now); System.Console.WriteLine(timeFmt, "Coordinated Universal Time:", curUTC); // What is GMT/UTC offset ? TimeSpan currentOffset = curTimeZone.GetUtcOffset(DateTime.Now); System.Console.WriteLine(dataFmt, "UTC offset:", currentOffset); // Get DaylightTime object System.Globalization.DaylightTime dl = curTimeZone.GetDaylightChanges(DateTime.Now.Year); // DateTime when the daylight-saving period begins. System.Console.WriteLine("Start: {0:MM-dd-yyyy HH:mm} ", dl.Start); // DateTime when the daylight-saving period ends. System.Console.WriteLine("End: {0:MM-dd-yyyy HH:mm} ", dl.End); // Difference between standard time and the daylight-saving time. System.Console.WriteLine("delta: {0}", dl.Delta); System.Console.Read(); //string input = ""; //bool state = true; //var value = Environment.GetEnvironmentVariable("DOTNETBACKEND_PORT"); //// If necessary, create it. //if (value == null) //{ // Environment.SetEnvironmentVariable("DOTNETBACKEND_PORT", "5567"); // // Now retrieve it. // value = Environment.GetEnvironmentVariable("DOTNETBACKEND_PORT"); //} //Environment.SetEnvironmentVariable("DOTNET_ASSEMBLY_SEARCH_PATHS", @"C:\WorkingFolder-Custom\TZ.BigDataScript\TZ.DataAnalyzer.Console\bin\Debug"); //while (state) //{ // System.System.Console.WriteLine("Enter Query."); // input = System.Console.ReadLine(); // AScript script = new AScript(); // script.Compile(input,""); // //AntlrInputStream inputStream = new AntlrInputStream(input); // //AScriptLexer exprL = new AScriptLexer(inputStream); // //CommonTokenStream commonTokenStream = new CommonTokenStream(exprL); // //AScriptParser expPar = new AScriptParser(commonTokenStream); // //// exprParser.GroupdataContext getContext = expPar.groupdata(); // //AScriptParser.StatementContext getContext = expPar.statement(); // //AScriptVisitor visitor = new AScriptVisitor(); // //visitor.Visit(getContext); // //foreach (var line in visitor.Lines) // //{ // // System.System.Console.WriteLine("{0 } has said {1} ", line.Person, line.Text); // //} // //// var item= System.Console.ReadLine(); // //if (input == "exist") // //{ // // state = false; // //} // //else // //{ // // state = true; // //} //} } catch (Exception ex) { } finally { System.Console.ReadLine(); } }