public static ulong DateTimeToNptTimestamp(ref System.DateTime value) { System.DateTime baseDate = value >= UtcEpoch2036 ? UtcEpoch2036 : UtcEpoch1900; System.TimeSpan elapsedTime = value > baseDate ? value.ToUniversalTime() - baseDate.ToUniversalTime() : baseDate.ToUniversalTime() - value.ToUniversalTime(); return ((ulong)(elapsedTime.Ticks / System.TimeSpan.TicksPerSecond) << 32) | (uint)(elapsedTime.Ticks / Media.Common.Extensions.TimeSpan.TimeSpanExtensions.MicrosecondsPerMillisecond); }
/// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="time">时间</param> /// <returns>double</returns> public static int ConvertDateTimeInt(System.DateTime time) { int intResult = 0; System.DateTime startTime = new System.DateTime(1970, 1, 1); intResult = (int)(time.ToUniversalTime() - startTime).TotalSeconds; return intResult; }
/// <summary> /// Converts a c# DateTime Object in a JavaScript timestamp. /// </summary> /// <param name="input">The DateTime to convert.</param> /// <returns>JavaScript TimeStamp</returns> public static long DateTimeToJavascriptTimestamp(System.DateTime input) { DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = input.ToUniversalTime(); TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); return (long)ts.TotalMilliseconds; }
public static string GetStringFromDate (System.DateTime unespecifiedDate) { if (unespecifiedDate.Kind.Equals (DateTimeKind.Local)) { return unespecifiedDate.ToLocalTime().ToString("d"); } System.DateTime localDateTime = System.TimeZoneInfo.ConvertTimeFromUtc(unespecifiedDate.ToUniversalTime(), TimeZoneInfo.Local); return localDateTime.ToLocalTime().ToString("d"); }
public DateHeader(string name, System.DateTime dateTime, System.TimeSpan timeZoneOffset) : base(name, Header.GetHeaderId(name, true)) { var type = Header.TypeFromHeaderId(this.HeaderId); if (this.HeaderId != HeaderId.Unknown && type != typeof (DateHeader)) throw new System.ArgumentException(Resources.Strings.NameNotValidForThisHeaderType(name, "DateHeader", type.Name)); this.SetRawValue(null, true); parsed = true; utcDateTime = dateTime.ToUniversalTime(); this.timeZoneOffset = timeZoneOffset; }
public int UnixTime(System.DateTime d) { return Convert.ToInt32((d.ToUniversalTime() - EPOCH).TotalSeconds); }
/// <summary> /// Format a date/time variable for output. /// </summary> /// <example> /// <code> /// using Mezzocode.Halide3; /// ... /// string result = h3Temporal.DateFormat( /// theDate, /// h3Temporal.DateFormats.friendly); /// </code> /// </example> /// <param name="date">DateTime variable to format.</param> /// <param name="format">Date format.</param> /// <returns>String with the date formatted as requested.</returns> public static String DateFormat(System.DateTime date, DateFormats format) { string thedate = string.Empty; try { switch (format) { case DateFormats.sortable: thedate = date.ToString("yyyy-MM-dd"); break; case DateFormats.slashes: thedate = date.ToString("M/d/yyyy"); break; case DateFormats.dots: thedate = date.ToString("M.d.yyyy"); break; case DateFormats.full: thedate = date.ToString("dddd; MMMM d, yyyy"); break; case DateFormats.daily: thedate = date.ToString("dddd; MMMM d"); break; case DateFormats.tidy: thedate = date.ToString("MMM d, yyyy"); break; case DateFormats.weekday: thedate = date.ToString("dddd"); break; case DateFormats.weekdayShort: thedate = date.ToString("ddd"); break; case DateFormats.month: thedate = date.ToString("MMMM"); break; case DateFormats.monthShort: thedate = date.ToString("MMM"); break; case DateFormats.corporate: thedate = date.ToString("M/yyyy"); break; case DateFormats.pressRelease: thedate = date.ToString("MMMM d, yyyy"); break; case DateFormats.RSS: thedate = date.ToUniversalTime().ToString("o"); // thedate = date.ToString("yyyy-MM-dd") + "T" + date.ToString("HH:mm:ss") + "Z"; break; case DateFormats.RSS2: thedate = date.ToUniversalTime().ToString("R"); break; case DateFormats.abbreviatedFull: thedate = date.ToString("ddd-MMM-dd-yyyy"); break; case DateFormats.friendly: double days = Convert.ToInt32(DateDiff(DateDiffComparisonType.days, date, System.DateTime.Now)); thedate = "today"; if (DateDiff(DateDiffComparisonType.hours, date, System.DateTime.Now) < 13 && DateDiff(DateDiffComparisonType.hours, date, System.DateTime.Now) >= 0) { if (DateDiff(DateDiffComparisonType.minutes, date, System.DateTime.Now) < 60 && DateDiff(DateDiffComparisonType.minutes, date, System.DateTime.Now) > 0) { thedate = Convert.ToInt32(DateDiff(DateDiffComparisonType.minutes, date, System.DateTime.Now)).ToString() + " minute"; if (Convert.ToInt32(DateDiff(DateDiffComparisonType.minutes, date, System.DateTime.Now)) != 1) { thedate += "s"; } thedate += " ago"; } else { thedate = Convert.ToInt32(DateDiff(DateDiffComparisonType.hours, date, System.DateTime.Now)).ToString() + " hour"; if (Convert.ToInt32(DateDiff(DateDiffComparisonType.hours, date, System.DateTime.Now)) != 1) { thedate += "s"; } thedate += " ago"; } } else { if (days < 7 && days > 0) { if (days == 1) { thedate = "yesterday"; } else { thedate = days.ToString() + " day"; if (days != 1) thedate += "s"; thedate += " ago, on " + date.ToString("M/d/yyyy"); } } else { if (days == 7) { thedate = "a week ago"; } else { thedate = "on " + date.ToString("M/d/yyyy"); } } } break; default: thedate = date.ToString("M-d-yyyy"); break; } } catch { } return (thedate); }
internal void SetValue(System.DateTime value, System.TimeSpan timeZoneOffset) { this.SetRawValue(null, true); parsed = true; utcDateTime = value.ToUniversalTime(); this.timeZoneOffset = timeZoneOffset; }
public static String From(System.DateTime value) { return value.ToUniversalTime().ToString("u").Replace(" ", "T"); }
public void WriteHeaderValue(System.DateTime value) { this.AssertOpen(); if (headerValueWritten) throw new System.InvalidOperationException(Resources.Strings.CannotWriteHeaderValueMoreThanOnce); if (lastHeader == null) throw new System.InvalidOperationException(Resources.Strings.CannotWriteHeaderValueHere); headerValueWritten = true; var timeZoneOffset = System.TimeSpan.Zero; var utcDateTime = value.ToUniversalTime(); if (value.Kind != System.DateTimeKind.Utc) timeZoneOffset = System.TimeZoneInfo.Local.GetUtcOffset(value); Header.WriteName(shimStream, lastHeader.Name, ref scratchBuffer); var currentLineLength = new MimeStringLength(0); DateHeader.WriteDateHeaderValue(shimStream, utcDateTime, timeZoneOffset, ref currentLineLength); lastHeader = null; }
public void AddValue(string name, System.DateTime value) { try { XmlNode node = m_document.SelectSingleNode( m_strSubkey ); AppendSimpleValNode( node, name, "datetime", value.ToUniversalTime().ToString() ); } catch( XPathException e ) { Trace.WriteLine( " Error : " + e.Message ); Trace.WriteLine( " XML : " + m_document.OuterXml); throw new Exception(" XPathException in Add Value " + e.Message); } }
private static string CreateICal(string SendFrom, string SendTo, string Subject, string Body, string Location, System.DateTime StartTime, System.DateTime EndTime, string MsgID, int Sequence, bool IsCancelled) { StringBuilder sb = new StringBuilder(); if (string.IsNullOrEmpty(MsgID)) { MsgID = Guid.NewGuid().ToString(); } //See iCalendar spec here: http://tools.ietf.org/html/rfc2445 //Abridged version here: http://www.kanzaki.com/docs/ical/ sb.AppendLine("BEGIN:VCALENDAR"); sb.AppendLine("PRODID:-//Microsoft LightSwitch"); sb.AppendLine("VERSION:2.0"); if (IsCancelled) sb.AppendLine("METHOD:CANCEL"); else sb.AppendLine("METHOD:REQUEST"); sb.AppendLine("BEGIN:VEVENT"); if (IsCancelled) { sb.AppendLine("STATUS:CANCELLED"); sb.AppendLine("PRIORITY:1"); } sb.AppendLine(string.Format("ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT:MAILTO:{0}", SendTo)); sb.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", SendFrom)); sb.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", StartTime.ToUniversalTime())); sb.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", EndTime.ToUniversalTime())); sb.AppendLine(string.Format("LOCATION:{0}", Location)); sb.AppendLine("TRANSP:OPAQUE"); //You need to increment the sequence anytime you update the meeting request. sb.AppendLine(string.Format("SEQUENCE:{0}", Sequence)); //This needs to be a unique ID. A GUID is created when the appointment entity is inserted sb.AppendLine(string.Format("UID:{0}", MsgID)); sb.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow)); sb.AppendLine(string.Format("DESCRIPTION:{0}", Body)); sb.AppendLine(string.Format("SUMMARY:{0}", Subject)); sb.AppendLine("CLASS:PUBLIC"); //Create a 15min reminder sb.AppendLine("BEGIN:VALARM"); sb.AppendLine("TRIGGER:-PT15M"); sb.AppendLine("ACTION:DISPLAY"); sb.AppendLine("DESCRIPTION:Reminder"); sb.AppendLine("END:VALARM"); sb.AppendLine("END:VEVENT"); sb.AppendLine("END:VCALENDAR"); return sb.ToString(); }
/// <summary> /// 创建时间戳 /// </summary> /// <returns></returns> public static long ToTimestamp(System.DateTime dt) { return (dt.ToUniversalTime().Ticks - 621355968000000000) / 10000000; }
// Converts a given System.DateTime object to DMTF datetime format. static string ToDmtfDateTime(System.DateTime date) { string utcString = string.Empty; System.TimeSpan tickOffset = System.TimeZone.CurrentTimeZone.GetUtcOffset(date); long OffsetMins = ((long)((tickOffset.Ticks / System.TimeSpan.TicksPerMinute))); if ((System.Math.Abs(OffsetMins) > 999)) { date = date.ToUniversalTime(); utcString = "+000"; } else { if ((tickOffset.Ticks >= 0)) { utcString = string.Concat("+", ((System.Int64 )((tickOffset.Ticks / System.TimeSpan.TicksPerMinute))).ToString().PadLeft(3, '0')); } else { string strTemp = ((System.Int64 )(OffsetMins)).ToString(); utcString = string.Concat("-", strTemp.Substring(1, (strTemp.Length - 1)).PadLeft(3, '0')); } } string dmtfDateTime = ((System.Int32 )(date.Year)).ToString().PadLeft(4, '0'); dmtfDateTime = string.Concat(dmtfDateTime, ((System.Int32 )(date.Month)).ToString().PadLeft(2, '0')); dmtfDateTime = string.Concat(dmtfDateTime, ((System.Int32 )(date.Day)).ToString().PadLeft(2, '0')); dmtfDateTime = string.Concat(dmtfDateTime, ((System.Int32 )(date.Hour)).ToString().PadLeft(2, '0')); dmtfDateTime = string.Concat(dmtfDateTime, ((System.Int32 )(date.Minute)).ToString().PadLeft(2, '0')); dmtfDateTime = string.Concat(dmtfDateTime, ((System.Int32 )(date.Second)).ToString().PadLeft(2, '0')); dmtfDateTime = string.Concat(dmtfDateTime, "."); System.DateTime dtTemp = new System.DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, 0); long microsec = ((long)((((date.Ticks - dtTemp.Ticks) * 1000) / System.TimeSpan.TicksPerMillisecond))); string strMicrosec = ((System.Int64 )(microsec)).ToString(); if ((strMicrosec.Length > 6)) { strMicrosec = strMicrosec.Substring(0, 6); } dmtfDateTime = string.Concat(dmtfDateTime, strMicrosec.PadLeft(6, '0')); dmtfDateTime = string.Concat(dmtfDateTime, utcString); return dmtfDateTime; }
public DateTimeZone (System.DateTime datetime) { UtcTime = datetime.ToUniversalTime (); offset = Convert.ToInt32 (datetime.ToString ("zz")); }
internal static void CheckSubtraction(System.DateTime date1, long ticks, Frame caller) { try { if (date1.ToUniversalTime().AddTicks(-ticks) < Time.Epoch) { throw new ArgumentError("time out of range").raise(caller); } } catch { throw new ArgumentError("time out of range").raise(caller); } }
/// <summary> /// Format a date/time variable for output. /// </summary> /// <example> /// <code> /// using Argentini.Halide; /// ... /// string result = H3Temporal.DateFormat( /// theDate, /// H3Temporal.DateFormats.friendly); /// </code> /// </example> /// <param name="date">DateTime variable to format.</param> /// <param name="format">Date format.</param> /// <returns>String with the date formatted as requested.</returns> public static String DateFormat(System.DateTime date, DateFormats format) { string thedate = string.Empty; try { switch (format) { case DateFormats.Sortable: thedate = date.ToString("yyyy-MM-dd"); break; case DateFormats.Slashes: thedate = date.ToString("M/d/yyyy"); break; case DateFormats.Dots: thedate = date.ToString("M.d.yyyy"); break; case DateFormats.Full: thedate = date.ToString("dddd; MMMM d, yyyy"); break; case DateFormats.Daily: thedate = date.ToString("dddd; MMMM d"); break; case DateFormats.Tidy: thedate = date.ToString("MMM d, yyyy"); break; case DateFormats.Weekday: thedate = date.ToString("dddd"); break; case DateFormats.WeekdayShort: thedate = date.ToString("ddd"); break; case DateFormats.Month: thedate = date.ToString("MMMM"); break; case DateFormats.MonthShort: thedate = date.ToString("MMM"); break; case DateFormats.Corporate: thedate = date.ToString("M/yyyy"); break; case DateFormats.PressRelease: thedate = date.ToString("MMMM d, yyyy"); break; case DateFormats.Rss: thedate = date.ToUniversalTime().ToString("o"); // thedate = date.ToString("yyyy-MM-dd") + "T" + date.ToString("HH:mm:ss") + "Z"; break; case DateFormats.Rss2: thedate = date.ToUniversalTime().ToString("R"); break; case DateFormats.AbbreviatedFull: thedate = date.ToString("ddd-MMM-dd-yyyy"); break; case DateFormats.Friendly: double days = Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Days, date, System.DateTime.Now))); bool future = false; if (DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now) <= 0) { future = true; } thedate = "today"; if (Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now)) < 13 && Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now)) >= 0) { if (Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now)) < 60 && Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now)) > 0) { thedate = Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now))).ToString() + " minute"; if (Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now))) != 1) { thedate += "s"; } thedate += (future ? " from now" : " ago"); } else { thedate = Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now))).ToString() + " hour"; if (Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now))) != 1) { thedate += "s"; } thedate += (future ? " from now" : " ago"); } } else { if (days < 7 && days > 0) { if (days == 1) { thedate = (future ? "tomorrow" : "yesterday"); } else { thedate = days.ToString() + " day"; if (days != 1) { thedate += "s"; } thedate += (future ? " from now" : " ago") + ", on " + date.ToString("M/d/yyyy"); } } else { if (days == 7) { thedate = (future ? "a week from now" : "a week ago"); } else { thedate = "on " + date.ToString("M/d/yyyy"); } } } break; case DateFormats.Abstract: days = Math.Abs(DateDiff(DateDiffComparisonType.Days, date, System.DateTime.Now)); future = false; if (DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now) <= 0) { future = true; } thedate = "today"; if (Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now)) < 24 && Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now)) >= 0) { if (Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now)) < 60 && Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now)) > 0) { thedate = Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now))).ToString() + " minute"; if (Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Minutes, date, System.DateTime.Now))) != 1) { thedate += "s"; } thedate += (future ? " from now" : " ago"); } else { thedate = Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now))).ToString() + " hour"; if (Convert.ToInt32(Math.Abs(DateDiff(DateDiffComparisonType.Hours, date, System.DateTime.Now))) != 1) { thedate += "s"; } thedate += (future ? " from now" : " ago"); } } else { if (days < 7 && days > 0) { if (days <= 1) { thedate = (future ? "tomorrow" : "yesterday"); } else { thedate = FormatAbstract(days, "day", 0.3, 0.6, 0.8, future); } } else { double weeks = days/7; if (weeks < 4) { thedate = FormatAbstract(weeks, "week", 0.3, 0.6, 0.8, future); } else { double months = Math.Abs(DateDiff(DateDiffComparisonType.Months, date, System.DateTime.Now)); if (months < 12) { thedate = FormatAbstract(months, "month", 0.3, 0.6, 0.8, future); } else { double years = Math.Abs(DateDiff(DateDiffComparisonType.Years, date, System.DateTime.Now)); thedate = FormatAbstract(years, "year", 0.3, 0.6, 0.8, future); } } } } break; default: thedate = date.ToString("M-d-yyyy"); break; } } catch {} return (thedate); }
private System.DateTime GetDate(string dateString, System.DateTime baseDate, bool useGMT) { if (useGMT) { baseDate = baseDate.ToUniversalTime(); } TclDateTime calendar = new TclDateTime(); calendar.dateTime = baseDate; calendar.hour = 0; calendar.minute = 0; calendar.second = 0; calendar.millisecond = 0; ClockToken[] dt = GetTokens(dateString, false); System.Int32 parsePos = 0; ClockRelTimespan diff = new ClockRelTimespan(); int hasTime = 0; int hasZone = 0; int hasDate = 0; int hasDay = 0; int hasOrdMonth = 0; int hasRel = 0; while (parsePos < dt.Length) { if (ParseTime(dt, ref parsePos, calendar)) { hasTime++; } else if (ParseZone(dt, ref parsePos, calendar)) { hasZone++; } else if (ParseIso(dt, ref parsePos, calendar)) { hasDate++; } else if (ParseDate(dt, ref parsePos, calendar)) { hasDate++; } else if (ParseDay(dt, ref parsePos, diff)) { hasDay++; } else if (ParseOrdMonth(dt, ref parsePos, diff)) { hasOrdMonth++; } else if (ParseRelSpec(dt, ref parsePos, diff)) { hasRel++; } else if (ParseNumber(dt, ref parsePos, calendar, hasDate > 0 && hasTime > 0 && hasRel == 0)) { if (hasDate == 0 || hasTime == 0 || hasRel > 0) { hasTime++; } } else if (ParseTrek(dt, ref parsePos, calendar)) { hasDate++; hasTime++; } else { goto failed; } } if (hasTime > 1 || hasZone > 1 || hasDate > 1 || hasDay > 1 || hasOrdMonth > 1) { goto failed; } // The following line handles years that are specified using // only two digits. The line of code below implements a policy // defined by the X/Open workgroup on the millinium rollover. // Note: some of those dates may not actually be valid on some // platforms. The POSIX standard startes that the dates 70-99 // shall refer to 1970-1999 and 00-38 shall refer to 2000-2038. // This later definition should work on all platforms. int thisYear = calendar.year; if (thisYear < 100) { if (thisYear >= 69) { calendar.year = thisYear + 1900; } else { calendar.year = thisYear + 2000; } } if (hasRel > 0) { if (hasTime == 0 && hasDate == 0 && hasDay == 0) { calendar.dateTime = baseDate; } // Certain JDK implementations are buggy WRT DST. // Work around this issue by adding a day instead // of a days worth of seconds. int seconds_in_day = (60 * 60 * 24); int seconds = diff.Seconds; bool negative_seconds = (seconds < 0); int days = 0; if (negative_seconds) seconds *= (-1); while (seconds >= seconds_in_day) { seconds -= seconds_in_day; days++; } if (negative_seconds) { seconds *= (-1); days *= (-1); } if (days != 0) { // calendar.add(SupportClass.CalendarManager.DATE, days); } if (seconds != 0) { // calendar.add(SupportClass.CalendarManager.SECOND, seconds); } // calendar.add(SupportClass.CalendarManager.MONTH, diff.Months); } if (hasDay > 0 && hasDate == 0) { SetWeekday(calendar, diff); } if (hasOrdMonth > 0) { SetOrdMonth(calendar, diff); } try { return calendar.dateTime; } catch (Exception) { throw new FormatException(); } failed: throw new FormatException(); }
private void setTime(System.DateTime dateTime) { GeminiHardware.Instance.Trace.Enter("setTime", dateTime.ToString()); labelDateTimeData.Text = dateTime.ToString(); if (checkBoxUpdateClock.Checked) { SystemTime updatedTime = new SystemTime(); updatedTime.Year = (ushort)dateTime.ToUniversalTime().Year; updatedTime.Month = (ushort)dateTime.ToUniversalTime().Month; updatedTime.Day = (ushort)dateTime.ToUniversalTime().Day; updatedTime.Hour = (ushort)dateTime.ToUniversalTime().Hour; updatedTime.Minute = (ushort)dateTime.ToUniversalTime().Minute; updatedTime.Second = (ushort)dateTime.ToUniversalTime().Second; Win32SetSystemTime(ref updatedTime); } GeminiHardware.Instance.Trace.Exit("setTime" ); }