本文整理汇总了C#中DateTimeOffset.Subtract方法的典型用法代码示例。如果您正苦于以下问题:C# DateTimeOffset.Subtract方法的具体用法?C# DateTimeOffset.Subtract怎么用?C# DateTimeOffset.Subtract使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeOffset
的用法示例。
在下文中一共展示了DateTimeOffset.Subtract方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Before
/// <summary>
/// Asserts that a <see cref="DateTimeOffset"/> occurs a specified amount of time before another <see cref="DateTimeOffset"/>.
/// </summary>
/// <param name="target">
/// The <see cref="DateTimeOffset"/> to compare the subject with.
/// </param>
/// <param name="because">
/// A formatted phrase explaining why the assertion should be satisfied. If the phrase does not
/// start with the word <i>because</i>, it is prepended to the message.
/// </param>
/// <param name="reasonArgs">
/// Zero or more values to use for filling in any <see cref="string.Format(string,object[])"/> compatible placeholders.
/// </param>
public AndConstraint<DateTimeOffsetAssertions> Before(DateTimeOffset target, string because = "",
params object[] reasonArgs)
{
bool success = Execute.Assertion
.ForCondition(subject.HasValue)
.BecauseOf(because, reasonArgs)
.FailWith("Expected date and/or time {0} to be " + predicate.DisplayText +
" {1} before {2}{reason}, but found a <null> DateTime.",
subject, timeSpan, target);
if (success)
{
var actual = target.Subtract(subject.Value);
if (!predicate.IsMatchedBy(actual, timeSpan))
{
Execute.Assertion
.BecauseOf(because, reasonArgs)
.FailWith(
"Expected date and/or time {0} to be " + predicate.DisplayText +
" {1} before {2}{reason}, but it differs {3}.",
subject, timeSpan, target, actual);
}
}
return new AndConstraint<DateTimeOffsetAssertions>(parentAssertions);
}
示例2: AdvanceTo
public void AdvanceTo(DateTimeOffset when)
{
if (when < _now)
throw new InvalidOperationException("You can't reverse time...");
Advance(when.Subtract(_now));
}
示例3: TestTimeSpan
public static void TestTimeSpan()
{
DateTimeOffset dt = new DateTimeOffset(new DateTime(2012, 6, 18, 10, 5, 1, 0));
TimeSpan ts = dt.TimeOfDay;
DateTimeOffset newDate = dt.Subtract(ts);
Assert.Equal(new DateTimeOffset(new DateTime(2012, 6, 18, 0, 0, 0, 0)).Ticks, newDate.Ticks);
Assert.Equal(dt.Ticks, newDate.Add(ts).Ticks);
}
示例4: ScheduleNextTime
public DateTimeOffset ScheduleNextTime(DateTimeOffset currentTime, JobExecutionRecord lastExecution)
{
var next = currentTime.Subtract(new TimeSpan(0, 0, 0, currentTime.Second, currentTime.Millisecond));
while (next < currentTime)
{
next = next.Add(_seconds.Seconds());
}
return next;
}
示例5: Main
static void Main(string[] args)
{
//var offset = DateTimeOffset.Now;
//Debug.WriteLine(offset);
//Debug.WriteLine(DateTimeOffset.UtcNow);
//String s = "10/1/2014 6:17 PM";
//DateTime dt = DateTime.ParseExact(s, "MM/dd/yyyy h:mm tt", CultureInfo.InvariantCulture);
//DateTimeOffset dto = new DateTimeOffset(dt);
DateTimeOffset dto1 = new DateTimeOffset();
dto1 = DateTimeOffset.Now;
dto1 = dto1.AddHours(2);
dto1 = dto1.AddMinutes(30);
Debug.WriteLine(DateTimeOffset.Now);
Debug.WriteLine(dto1);
TimeSpan diff = dto1.Subtract(DateTimeOffset.Now);
Debug.WriteLine(diff);
Debug.WriteLine(diff.TotalHours);
Debug.WriteLine(diff.TotalSeconds);
var diffsec = (DateTimeOffset.Now - dto1).TotalSeconds;
Debug.WriteLine(diffsec);
//dtoff.Value.ToLocalTime().ToString("dddd, MMM dd yyyy HH:mm", new CultureInfo("en-US"));
//Debug.WriteLine(dto1);
//Debug.WriteLine(dto2);
//Debug.WriteLine(DateTimeOffset.Now);
//Debug.WriteLine(dto1 - DateTimeOffset.Now);
//Debug.WriteLine(DateTimeOffset.Now - dto1);
//Debug.WriteLine(dto1.CompareTo(DateTimeOffset.Now));
//Debug.WriteLine(DateTimeOffset.Now.CompareTo(dto1));
//Debug.WriteLine(" ");
//Debug.WriteLine(dto1.Hour);
double temp = -26;
double res = 0;
for (int i = 0; i < 40; i++)
{
temp++;
res = 0.5 - (temp * 0.1);
Debug.WriteLine("Temp: " + temp + " Result: " + res);
}
//Debug.WriteLine(s);
//Debug.WriteLine(dt.ToString());
//Debug.WriteLine(dt.ToUniversalTime().ToString());
//Debug.WriteLine(dto.ToString());
}
示例6: DeliverAfter
public Task DeliverAfter(NimbusMessage message, DateTimeOffset deliveryTime)
{
// Deliberately not awaiting this task. We want it to run in the background.
Task.Run(async () =>
{
var delay = deliveryTime.Subtract(_clock.UtcNow);
if (delay < TimeSpan.Zero) delay = TimeSpan.Zero;
await Task.Delay(delay);
AsyncBlockingCollection<NimbusMessage> queue;
if (!_messageStore.TryGetExistingMessageQueue(message.DeliverTo, out queue)) return;
await queue.Add(message);
}).ConfigureAwaitFalse();
return Task.Delay(0);
}
示例7: DeliverAfter
public Task DeliverAfter(NimbusMessage message, DateTimeOffset deliveryTime)
{
_logger.Debug("Enqueuing {MessageId} for re-delivery at {DeliverAt}", message.MessageId, deliveryTime);
// Deliberately not awaiting this task. We want it to run in the background.
Task.Run(async () =>
{
var delay = deliveryTime.Subtract(_clock.UtcNow);
if (delay < TimeSpan.Zero) delay = TimeSpan.Zero;
await Task.Delay(delay);
_logger.Debug("Re-delivering {MessageId} (attempt {Attempt})", message.MessageId, message.DeliveryAttempts.Length);
var sender = _transport.GetQueueSender(message.DeliverTo);
await sender.Send(message);
}).ConfigureAwaitFalse();
return Task.Delay(0);
}
示例8: ConvertPeriodtoIb
//public void RequestHistoricalData(Security security, DateTime endDateTime, TimeSpan duration,
// BarSize barSizeSetting, HistoricalDataType whatToShow, int useRth)
//{
// DateTime beginDateTime = endDateTime.Subtract(duration);
// string dur = ConvertPeriodtoIb(beginDateTime, endDateTime);
// RequestHistoricalData(tickerId, contract, endDateTime, dur, barSizeSetting, whatToShow, useRth);
//}
/// <summary>
/// used for reqHistoricalData
/// </summary>
private static string ConvertPeriodtoIb(DateTimeOffset startTime, DateTimeOffset endTime)
{
var period = endTime.Subtract(startTime);
var secs = period.TotalSeconds;
long unit;
if (secs < 1)
throw new ArgumentOutOfRangeException("endTime", "Period cannot be less than 1 second.");
if (secs < 86400)
{
unit = (long)Math.Ceiling(secs);
return unit + " S";
}
var days = secs / 86400;
unit = (long)Math.Ceiling(days);
if (unit <= 34)
return unit + " D";
var weeks = days / 7;
unit = (long)Math.Ceiling(weeks);
if (unit > 52)
throw new ArgumentOutOfRangeException("endTime", "Period cannot be bigger than 52 weeks.");
return unit + " W";
}
示例9: SendSession
public void SendSession(DateTimeOffset start, DateTimeOffset end, int sessionId, Guid version)
{
//TODO: errors at this level should be treated as critical
DateTime utcActualStart = start.Subtract(Settings.SessionWarmUp).UtcDateTime;
_logger.InfoFormat("Sending StartSessionRequest with SessionId: {0} Version: {1} Start(UTC): {2}",
version, sessionId, utcActualStart);
StartSessionRequest startRequest = new StartSessionRequest { Version = version, SessionId = sessionId};
Send(startRequest, utcActualStart);
DateTime utcActualEnd = end.Add(Settings.SessionCoolDown).UtcDateTime;
_logger.InfoFormat("Sending EndSessionRequest with SessionId: {0} Version: {1} End(UTC): {2}",
version, sessionId, utcActualEnd);
EndSessionRequest endRequest = new EndSessionRequest { Version = version, SessionId = sessionId };
Send(endRequest, utcActualEnd);
}
示例10: SummaryTable
/// <summary>
/// Gets the statistical information
/// </summary>
/// <param name="totalCasesNum">The number of total cases</param>
/// <param name="passedNum">The number of passed cases</param>
/// <param name="failedNum">The number of failed cases</param>
/// <param name="testRunStartTime">The start time of the run</param>
/// <param name="testRunEndTime">The end time of the run</param>
/// <returns>Return statistical information about this test</returns>
public string SummaryTable(long totalCasesNum,
long passedNum,
long failedNum,
DateTimeOffset testRunStartTime,
DateTimeOffset testRunEndTime)
{
DataType.RunSummary sry = new DataType.RunSummary()
{
TotalCount = totalCasesNum,
FailedCount = failedNum,
PassedCount = passedNum,
InconclusiveCount = totalCasesNum - passedNum - failedNum,
PassRate = totalCasesNum == 0 ? 0 : (float)passedNum * 100 / totalCasesNum,
StartTime = testRunStartTime.ToLocalTime().ToString("MM/dd/yyyy HH:mm:ss"),
EndTime = testRunEndTime.ToLocalTime().ToString("MM/dd/yyyy HH:mm:ss"),
Duration = testRunEndTime.Subtract(testRunStartTime).ToString(@"hh\:mm\:ss")
};
return (serializer.Serialize(sry));
}
示例11: Subtract_DateTimeOffset
public static void Subtract_DateTimeOffset(DateTimeOffset dt1, DateTimeOffset dt2, TimeSpan expected)
{
Assert.Equal(expected, dt1 - dt2);
Assert.Equal(expected, dt1.Subtract(dt2));
}
示例12: Subtract_TimeSpan
public static void Subtract_TimeSpan(DateTimeOffset dt, TimeSpan ts, DateTimeOffset expected)
{
Assert.Equal(expected, dt - ts);
Assert.Equal(expected, dt.Subtract(ts));
}
示例13: ToUnixTimeStamp
public static long ToUnixTimeStamp(DateTimeOffset dateTime)
{
var ts = dateTime.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
return (long)ts;
}
示例14: CleanOffset
private DateTimeOffset CleanOffset(DateTimeOffset offset)
{
offset = offset.Subtract(TimeSpan.FromMilliseconds(offset.Millisecond));
return offset.Subtract(TimeSpan.FromSeconds(offset.Second));
}
示例15: TestOperators
public static void TestOperators()
{
DateTimeOffset date1 = new DateTimeOffset(new DateTime(1996, 6, 3, 22, 15, 0, DateTimeKind.Utc));
DateTimeOffset date2 = new DateTimeOffset(new DateTime(1996, 12, 6, 13, 2, 0, DateTimeKind.Utc));
DateTimeOffset date3 = new DateTimeOffset(new DateTime(1996, 10, 12, 8, 42, 0, DateTimeKind.Utc));
// diff1 gets 185 days, 14 hours, and 47 minutes.
TimeSpan diff1 = date2.Subtract(date1);
Assert.Equal(new TimeSpan(185, 14, 47, 0), diff1);
// date4 gets 4/9/1996 5:55:00 PM.
DateTimeOffset date4 = date3.Subtract(diff1);
Assert.Equal(new DateTimeOffset(new DateTime(1996, 4, 9, 17, 55, 0, DateTimeKind.Utc)), date4);
// diff2 gets 55 days 4 hours and 20 minutes.
TimeSpan diff2 = date2 - date3;
Assert.Equal(new TimeSpan(55, 4, 20, 0), diff2);
// date5 gets 4/9/1996 5:55:00 PM.
DateTimeOffset date5 = date1 - diff2;
Assert.Equal(new DateTimeOffset(new DateTime(1996, 4, 9, 17, 55, 0, DateTimeKind.Utc)), date5);
}