public string ConvertToLocalizedString(DateTime?date, string format, DateLocalizationOptions options = null) { options = options ?? new DateLocalizationOptions(); if (!date.HasValue) { return(options.NullText); } var dateValue = date.Value; var offset = TimeSpan.Zero; if (options.EnableTimeZoneConversion) { dateValue = ConvertToSiteTimeZone(dateValue); offset = CurrentTimeZone.GetUtcOffset(date.Value); } var parts = DateTimeParts.FromDateTime(dateValue, offset); if (options.EnableCalendarConversion && !(CurrentCalendar is GregorianCalendar)) { parts = ConvertToSiteCalendar(dateValue, offset); } return(_dateFormatter.FormatDateTime(parts, format)); }
/// <summary> /// Returns the local time that corresponds to a specified coordinated universal time (UTC). /// </summary> /// <param name="time">A UTC time.</param> /// <returns> /// A <see cref="T:System.DateTime"></see> instance whose value is the local time that corresponds to time. /// </returns> public override DateTime ToLocalTime(DateTime time) { if (time.Kind != DateTimeKind.Unspecified) { time = new DateTime(time.Ticks, DateTimeKind.Unspecified); } return(time.Add(CurrentTimeZone.GetUtcOffset(time) - GetUtcOffset(time))); }
/// <summary> /// Converts DateTime to the local time. /// </summary> /// <param name="time">The time.</param> /// <returns>Local DateTime</returns> public DateTime FromLocalTime(DateTime time) { if (time.Kind != DateTimeKind.Unspecified) { time = new DateTime(time.Ticks, DateTimeKind.Unspecified); } return(time.Add(GetUtcOffset(time) - CurrentTimeZone.GetUtcOffset(time))); }
private DateTime ConvertLocalToUtc(int year, int month, int day, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0 ) { var localDate = new DateTime(year, month, day, hours, minutes, seconds, milliseconds, DateTimeKind.Unspecified); var offset = CurrentTimeZone.GetUtcOffset(localDate); var withOffset = new DateTimeOffset(localDate, offset); return(withOffset.UtcDateTime); }
public string ConvertToLocalizedTimeString(DateTime?date, DateLocalizationOptions options = null) { options = options ?? new DateLocalizationOptions(); if (!date.HasValue) { return(options.NullText); } var dateValue = date.Value; var offset = TimeSpan.Zero; if (options.EnableTimeZoneConversion) { if (options.IgnoreDate) { // The caller has asked us to ignore the date part. This usually because the source // is a time-only field. In such cases (with an undefined date) it does not make sense // to consider DST variations throughout the year, so we will use an arbitrary (but fixed) // non-DST date for the conversion to ensure DST is never applied during conversion. The // date part is usually DateTime.MinValue which we should not use because time zone // conversion cannot wrap DateTime.MinValue around to the previous day, resulting in // an undefined result. Instead we convert the date to a hard-coded date of 2000-01-01 // before the conversion, and back to the original date after. var tempDate = new DateTime(2000, 1, 1, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind); tempDate = ConvertToSiteTimeZone(tempDate); dateValue = new DateTime(dateValue.Year, dateValue.Month, dateValue.Day, tempDate.Hour, tempDate.Minute, tempDate.Second, tempDate.Millisecond, tempDate.Kind); } else { dateValue = ConvertToSiteTimeZone(dateValue); } offset = CurrentTimeZone.GetUtcOffset(date.Value); } var parts = DateTimeParts.FromDateTime(dateValue, offset); // INFO: No calendar conversion in this method - we expect the date component to be DateTime.MinValue and irrelevant anyway. return(_dateFormatter.FormatDateTime(parts, _dateTimeFormatProvider.LongTimeFormat)); }
public string ConvertToLocalizedTimeString(DateTime?date, DateLocalizationOptions options = null) { options = options ?? new DateLocalizationOptions(); if (!date.HasValue) { return(options.NullText); } var dateValue = date.Value; var offset = TimeSpan.Zero; if (options.EnableTimeZoneConversion) { if (options.IgnoreDate) { // The caller has asked us to ignore the date part and assume it is today. This usually because the source // is a time-only field, in which case the date part is usually DateTime.MinValue which we should not use // for the following reasons: // * DST can be active or not dependeng on the time of the year. We want the conversion to always act as if the time represents today, but we don't want that date stored. // * Time zone conversion cannot wrap DateTime.MinValue around to the previous day, resulting in undefined result. // Therefore we convert the date to today's date before the conversion, and back to the original date after. var today = _clock.UtcNow.Date; var tempDate = new DateTime(today.Year, today.Month, today.Day, dateValue.Hour, dateValue.Minute, dateValue.Second, dateValue.Millisecond, dateValue.Kind); tempDate = ConvertToSiteTimeZone(tempDate); dateValue = new DateTime(dateValue.Year, dateValue.Month, dateValue.Day, tempDate.Hour, tempDate.Minute, tempDate.Second, tempDate.Millisecond, tempDate.Kind); } else { dateValue = ConvertToSiteTimeZone(dateValue); } offset = CurrentTimeZone.GetUtcOffset(date.Value); } var parts = DateTimeParts.FromDateTime(dateValue, offset); // INFO: No calendar conversion in this method - we expect the date component to be DateTime.MinValue and irrelevant anyway. return(_dateFormatter.FormatDateTime(parts, _dateTimeFormatProvider.LongTimeFormat)); }
protected IQueryable <T> DatabaseCompareExpression( IQueryable <T> query, PropertyViewModel prop, string value, TimeZoneInfo timeZone) { if (prop.Type.IsDate) { // See if they just passed in a date or a date and a time DateTime parsedValue; if (DateTime.TryParse(value, out parsedValue)) { // Correct offset. if (prop.Type.IsDateTimeOffset) { DateTimeOffset dateTimeOffset = new DateTimeOffset(parsedValue, CurrentTimeZone.GetUtcOffset(parsedValue)); if (dateTimeOffset.TimeOfDay == TimeSpan.FromHours(0) && !value.Contains(':')) { // Only a date var nextDate = dateTimeOffset.AddDays(1); return(query.Where(string.Format( "{0} >= @0 && {0} < @1", prop.Name), dateTimeOffset, nextDate)); } else { // Date and Time return(query.Where(string.Format("{0} = @0", prop.Name), dateTimeOffset)); } } else { if (parsedValue.TimeOfDay == TimeSpan.FromHours(0) && !value.Contains(':')) { // Only a date var nextDate = parsedValue.AddDays(1); return(query.Where(string.Format( "{0} >= @0 && {0} < @1", prop.Name), parsedValue, nextDate)); } else { // Date and Time return(query.Where(string.Format("{0} = @0", prop.Name), parsedValue)); } } } else { // Could not parse date string. return(null); } } else if (prop.Type.IsString) { if (value.Contains("*")) { return(query.Where($"{prop.Name}.StartsWith(\"{value.Replace("*", "")}\")")); } else { return(query.Where($"{prop.Name} = \"{value}\"")); } } else if (prop.Type.IsEnum) { var expressions = new List <string>(); foreach (var valuePart in value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) { expressions.Add($"{prop.Name} = \"{prop.Type.EnumValues.SingleOrDefault(ev => ev.Key == Convert.ToInt32(valuePart)).Value}\""); } return(query.Where(string.Join(" || ", expressions))); } else { if (value.Contains(",")) { var whereList = new List <string>(); foreach (var num in value.Split(',')) { whereList.Add($"{prop.Name} = {num}"); } return(query.Where(string.Join(" or ", whereList))); } else { return(query.Where(string.Format("{0} = {1}", prop.Name, value))); } } }