本文整理汇总了C#中DateTimeOffset.GetValueOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# DateTimeOffset.GetValueOrDefault方法的具体用法?C# DateTimeOffset.GetValueOrDefault怎么用?C# DateTimeOffset.GetValueOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DateTimeOffset
的用法示例。
在下文中一共展示了DateTimeOffset.GetValueOrDefault方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Dates
/// <summary>
/// Returns random datetimeoffset gen
/// </summary>
/// <param name="min">if not supplied, DateTimeOffset.MinValue is used</param>
/// <param name="max">if not supplied, DateTimeOffset.MaxValue is used</param>
public Func<DateTimeOffset> Dates(DateTimeOffset? min = null, DateTimeOffset? max = null)
{
var minDate = min.GetValueOrDefault(DateTimeOffset.MinValue);
var maxDate = max.GetValueOrDefault(DateTimeOffset.MaxValue);
if (minDate >= maxDate)
throw new ArgumentOutOfRangeException("min >= max");
var ticksSpan = maxDate.Ticks - minDate.Ticks;
var factory = _random.Numbers.Doubles().BetweenZeroAndOne();
return () => new DateTimeOffset(Convert.ToInt64(minDate.Ticks + ((double)ticksSpan * factory())), minDate.Offset);
}
示例2: GetTransactionsToComputeAccountTotal
public IEnumerable<TransactionsToComputeAccountTotal> GetTransactionsToComputeAccountTotal(DateTimeOffset startDate, DateTimeOffset? endDate = null)
{
if (endDate == null)
{
endDate = DateTime.Now;
}
string dateFormat = "yyyy-MM-dd";
string query = @"
SELECT * FROM [dbo].[TransactionsToComputeAccountTotal] (@startDate, @endDate)
";
string sd = startDate.Date.ToString(dateFormat);
string ed = endDate.GetValueOrDefault().Date.ToString(dateFormat);
return this.context.Database.SqlQuery<TransactionsToComputeAccountTotal>(query,
new SqlParameter("@startDate", sd),
new SqlParameter("@endDate", ed)
);
}
示例3: Run
/// <summary>
/// Runs the specified date time.
/// </summary>
/// <param name="dateTime">The date time</param>
public void Run(DateTimeOffset? dateTime = null)
{
DateTimeOffset now = dateTime.GetValueOrDefault(DateTimeOffset.UtcNow);
if (now > this.Recurrence.DateEnd)
{
this.Status = TaskItemStatus.Stopped;
return;
}
if (!this.Recurrence.CheckDate(now))
{
return;
}
if (this.Status != TaskItemStatus.Running)
{
return;
}
else if (now.Subtract(this.LastRun) < this.Recurrence.Interval)
{
return;
}
this.LastRun = now;
CancellationTokenSource cancellationTokenSource;
if (this.Lifetime != TimeSpan.Zero)
{
cancellationTokenSource = new CancellationTokenSource(this.Lifetime);
}
else
{
cancellationTokenSource = new CancellationTokenSource();
}
TaskActionParameters cronActionParameters = new TaskActionParameters(this, now, cancellationTokenSource);
this.ActiveActions.Add(cronActionParameters);
Action actionToRun = () =>
{
for (int i = this.Repeat; i > 0; i--) {
this.Action(cronActionParameters);
}
this.ActiveActions.Remove(cronActionParameters);
};
Task lastTask = Task.Run(actionToRun, cancellationTokenSource.Token);
if (this.Recurrence.Interval == TimeSpan.Zero)
{
this.Status = TaskItemStatus.Stopped;
}
}
示例4: WriteValue
/// <summary>
/// Writes a <see cref="Nullable{DateTimeOffset}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{DateTimeOffset}"/> value to write.</param>
public virtual void WriteValue(DateTimeOffset? value)
{
if (value == null)
{
WriteNull();
}
else
{
WriteValue(value.GetValueOrDefault());
}
}
示例5: WriteValue
/// <summary>
/// Writes a <see cref="Nullable{DateTimeOffset}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{DateTimeOffset}"/> value to write.</param>
public virtual void WriteValue(DateTimeOffset? value, string format = null) {
if (value == null) {
WriteNull();
} else {
WriteValue(value.GetValueOrDefault(), format);
}
}
示例6: UnixTimestamp
// methods
/// <summary>
/// Converts to Unix Timestamp.
/// </summary>
/// <param name="datetime">DateTime to be converted</param>
/// <returns>Unix Timestamp</returns>
public static double UnixTimestamp(DateTimeOffset? datetime)
{
DateTimeOffset epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
return (datetime.GetValueOrDefault(DateTimeOffset.UtcNow) - epoch).TotalSeconds;
}