本文整理汇总了C#中TimeSpan.Negate方法的典型用法代码示例。如果您正苦于以下问题:C# TimeSpan.Negate方法的具体用法?C# TimeSpan.Negate怎么用?C# TimeSpan.Negate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TimeSpan
的用法示例。
在下文中一共展示了TimeSpan.Negate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatCustomizedRoundripTimeZone
// output the 'K' format, which is for round-tripping the data
private static void FormatCustomizedRoundripTimeZone(DateTime dateTime, TimeSpan offset, StringBuilder result)
{
// The objective of this format is to round trip the data in the type
// For DateTime it should round-trip the Kind value and preserve the time zone.
// DateTimeOffset instance, it should do so by using the internal time zone.
if (offset == NullOffset)
{
// source is a date time, so behavior depends on the kind.
switch (dateTime.Kind)
{
case DateTimeKind.Local:
// This should output the local offset, e.g. "-07:30"
offset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
// fall through to shared time zone output code
break;
case DateTimeKind.Utc:
// The 'Z' constant is a marker for a UTC date
result.Append('Z');
return;
default:
// If the kind is unspecified, we output nothing here
return;
}
}
if (offset >= TimeSpan.Zero)
{
result.Append('+');
}
else
{
result.Append('-');
// get a positive offset, so that you don't need a separate code path for the negative numbers.
offset = offset.Negate();
}
result.AppendFormat(CultureInfo.InvariantCulture, "{0:00}:{1:00}", offset.Hours, offset.Minutes);
}
示例2: FormatCustomizedTimeZone
// output the 'z' famliy of formats, which output a the offset from UTC, e.g. "-07:30"
private static void FormatCustomizedTimeZone(DateTime dateTime, TimeSpan offset, String format, Int32 tokenLen, Boolean timeOnly, StringBuilder result)
{
// See if the instance already has an offset
Boolean dateTimeFormat = (offset == NullOffset);
if (dateTimeFormat)
{
// No offset. The instance is a DateTime and the output should be the local time zone
if (timeOnly && dateTime.Ticks < Calendar.TicksPerDay)
{
// For time only format and a time only input, the time offset on 0001/01/01 is less
// accurate than the system's current offset because of daylight saving time.
offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
}
else if (dateTime.Kind == DateTimeKind.Utc)
{
offset = TimeSpan.Zero;
}
else
{
offset = TimeZoneInfo.Local.GetUtcOffset(dateTime);
}
}
if (offset >= TimeSpan.Zero)
{
result.Append('+');
}
else
{
result.Append('-');
// get a positive offset, so that you don't need a separate code path for the negative numbers.
offset = offset.Negate();
}
if (tokenLen <= 1)
{
// 'z' format e.g "-7"
result.AppendFormat(CultureInfo.InvariantCulture, "{0:0}", offset.Hours);
}
else
{
// 'zz' or longer format e.g "-07"
result.AppendFormat(CultureInfo.InvariantCulture, "{0:00}", offset.Hours);
if (tokenLen >= 3)
{
// 'zzz*' or longer format e.g "-07:30"
result.AppendFormat(CultureInfo.InvariantCulture, ":{0:00}", offset.Minutes);
}
}
}
示例3: Subtract
public static DateTimeOffset Subtract(this DateTime dateTime, TimeSpan timeSpan, TimeZoneInfo timeZone, TimeZoneOffsetResolver resolver)
{
return dateTime.Add(timeSpan.Negate(), timeZone, resolver);
}
示例4: Subtract
public static DateTimeOffset Subtract(this DateTimeOffset dateTimeOffset, TimeSpan timeSpan, TimeZoneInfo timeZone)
{
return dateTimeOffset.Add(timeSpan.Negate(), timeZone);
}
示例5: ConvertDateTimeOffsetLiteralValue
private static DateTimeOffset ConvertDateTimeOffsetLiteralValue(ErrorContext errCtx, string datetimeLiteralValue)
{
var datetimeParts = datetimeLiteralValue.Split(_datetimeOffsetSeparators, StringSplitOptions.RemoveEmptyEntries);
Debug.Assert(datetimeParts.Length >= 7, "datetime literal value must have at least 7 parts");
int year;
int month;
int day;
GetDateParts(datetimeLiteralValue, datetimeParts, out year, out month, out day);
int hour;
int minute;
int second;
int ticks;
//Copy the time parts into a different array since the last two parts will be handled in this method.
var timeParts = new String[datetimeParts.Length - 2];
Array.Copy(datetimeParts, timeParts, datetimeParts.Length - 2);
GetTimeParts(datetimeLiteralValue, timeParts, 3, out hour, out minute, out second, out ticks);
Debug.Assert(year >= 1 && year <= 9999);
Debug.Assert(month >= 1 && month <= 12);
Debug.Assert(day >= 1 && day <= 31);
Debug.Assert(hour >= 0 && hour <= 24);
Debug.Assert(minute >= 0 && minute <= 59);
Debug.Assert(second >= 0 && second <= 59);
Debug.Assert(ticks >= 0 && ticks <= 9999999);
var offsetHours = Int32.Parse(datetimeParts[datetimeParts.Length - 2], NumberStyles.Integer, CultureInfo.InvariantCulture);
var offsetMinutes = Int32.Parse(datetimeParts[datetimeParts.Length - 1], NumberStyles.Integer, CultureInfo.InvariantCulture);
var offsetTimeSpan = new TimeSpan(offsetHours, offsetMinutes, 0);
//If DateTimeOffset had a negative offset, we should negate the timespan
if (datetimeLiteralValue.IndexOf('+')
== -1)
{
offsetTimeSpan = offsetTimeSpan.Negate();
}
var dateTime = new DateTime(year, month, day, hour, minute, second, 0);
dateTime = dateTime.AddTicks(ticks);
try
{
return new DateTimeOffset(dateTime, offsetTimeSpan);
}
catch (ArgumentOutOfRangeException e)
{
var message = Strings.InvalidDateTimeOffsetLiteral(datetimeLiteralValue);
throw EntitySqlException.Create(errCtx, message, e);
}
}