Example #1
0
        /// <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);
        }
        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)
                );
        }
Example #3
0
        /// <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;
            }
        }
Example #4
0
 /// <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());
     }
 }
Example #5
0
 /// <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);
     }
 }
 // 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;
 }