public IGroupByStartEndTimesConfiguratorOptional <T> Months(int months)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();
            DateTime           d     = Serie.StartTime?.ToUniversalTime().ToLocalTime() ?? first.TimeUtc.ToLocalTime();

            int startMonth = d.Month;

            if (12 % months == 0)
            {
                startMonth = startMonth - (startMonth % months) + 1;
            }

            DateTime currentDate = new DateTime(d.Year, startMonth, 1, 0, 0, 0, DateTimeKind.Local);

            return(GroupByTime(currentDate, Serie.EndTime, dt =>
            {
                dt = dt.ToLocalTime();
                int year = dt.Year;
                int month = dt.Month;
                int newMonth = month + months;
                year += (newMonth - 1) / 12;
                month = ((newMonth - 1) % 12) + 1;
                return new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Local).ToUniversalTime();
            }));
        }
        public IGroupByStartEndTimesConfiguratorOptional <T> Days(int days, int startHour)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();
            DateTime           d     = Serie.StartTime?.ToUniversalTime().ToLocalTime() ?? first.TimeUtc.ToLocalTime();

            DateTime currentDate = new DateTime(d.Year, d.Month, d.Day, startHour, 0, 0, DateTimeKind.Local);

            return(GroupByTime(currentDate, Serie.EndTime, dt => dt + TimeSpan.FromDays(days)));
        }
        public IGroupByStartEndTimesConfiguratorOptional <T> Span(TimeSpan timeSpan, TimeSpan?minTimeSpan)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();

            if (minTimeSpan != null && timeSpan < minTimeSpan.Value)
            {
                timeSpan = minTimeSpan.Value;
            }

            return(GroupByTime(Serie.StartTime ?? first.TimeUtc, Serie.EndTime, dt => dt + timeSpan));
        }
        public IGroupByStartEndTimesConfiguratorOptional <T> Years(int years)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();
            DateTime           d     = Serie.StartTime?.ToUniversalTime().ToLocalTime() ?? first.TimeUtc.ToLocalTime();

            DateTime currentDate = new DateTime(d.Year, 1, 1, 0, 0, 0, DateTimeKind.Local);

            return(GroupByTime(currentDate, Serie.EndTime, dt =>
            {
                dt = dt.ToLocalTime();
                return new DateTime(dt.Year + years, 1, 1, 0, 0, 0, DateTimeKind.Local).ToUniversalTime();
            }));
        }
        public IGroupByStartEndTimesConfiguratorOptional <T> Weeks(int weeks, DayOfWeek startDay = DayOfWeek.Monday)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();
            DateTime           d     = Serie.StartTime?.ToUniversalTime().ToLocalTime() ?? first.TimeUtc.ToLocalTime();

            var startDate = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0, DateTimeKind.Local);

            while (startDate.DayOfWeek != startDay)
            {
                startDate = startDate - TimeSpan.FromDays(1);
            }

            return(GroupByTime(startDate, Serie.EndTime, dt => dt + TimeSpan.FromDays(weeks * 7)));
        }
Beispiel #6
0
        /// <summary>
        ///     Calculates the time span where a specified condition (predicate) is true
        ///     e.g. TimeWhere(v => v == 9.6f)?.TotalMinutes
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate">true, if added to time span</param>
        /// <returns>Time Span / use for example TotalMinutes to get a value of type T again</returns>
        public TimeSpan?TimeWhere(Func <T, bool> predicate)
        {
            if (!Rows.Any())
            {
                return(null);
            }
            TimeSpan?timeSpan = TimeSpan.Zero;
            var      rows     = Rows;

            ISingleDataRow <T> prevRow = null;

            for (int i = 0; i < rows.Count; i++)
            {
                ISingleDataRow <T> newRow = rows[i];
                if (i == 0 && PreviousRow != null && StartTime != null)
                {
                    if (predicate(PreviousRow.Value))
                    {
                        timeSpan += (newRow.TimeUtc - StartTime.Value);
                    }
                }
                else
                {
                    if (prevRow != null && predicate(prevRow.Value))
                    {
                        timeSpan += (newRow.TimeUtc - prevRow.TimeUtc);
                    }
                }

                prevRow = newRow;
            }

            if (NextRow != null && EndTime != null)
            {
                if (prevRow != null && predicate(prevRow.Value))
                {
                    timeSpan += (EndTime.Value - prevRow.TimeUtc);
                }
            }

            return(timeSpan);
        }
        public IGroupByStartEndTimesConfiguratorOptional <T> Hours(int hours)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();
            DateTime           d     = Serie.StartTime?.ToUniversalTime() ?? first.TimeUtc;

            int startHour = d.Hour;

            if (24 % hours == 0)
            {
                //change start minute to even minute
                startHour = startHour - (startHour % hours);
            }

            DateTime currentDate = new DateTime(d.Year, d.Month, d.Day, startHour, 0, 0, DateTimeKind.Utc);

            return(GroupByTime(currentDate, Serie.EndTime, dt => dt + TimeSpan.FromHours(hours)));
        }
        public IGroupByStartEndTimesConfiguratorOptional <T> Seconds(int seconds)
        {
            if (!Serie.Rows.Any())
            {
                return(this);
            }
            ISingleDataRow <T> first = Serie.Rows.First();
            DateTime           d     = Serie.StartTime?.ToUniversalTime() ?? first.TimeUtc;

            int startSeconds = d.Second;

            if (60 % seconds == 0)
            {
                //change start minute to even minute
                startSeconds = startSeconds - (startSeconds % seconds);
            }

            DateTime currentDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, startSeconds, DateTimeKind.Utc);

            return(GroupByTime(currentDate, Serie.EndTime, dt => dt + TimeSpan.FromSeconds(seconds)));
        }
Beispiel #9
0
        private IQuerySerie <T> ReadRows <T>(FileStream fs, BinaryReader binaryReader, DateTime start,
                                             DateTime stop, DateTime? @from = null, DateTime?to = null) where T : struct
        {
            //TODO: optimize duplicated code with ReadRows
            ISingleDataRow <T> firstRow = null;
            var rows = new List <ISingleDataRow <T> >();
            var data = new QuerySerie <T>(rows, from, to)
            {
                IsDbSerie = true
            };

            while (fs.Position < fs.Length)
            {
                var readRow = _rowReaderWriter.ReadRow <T>(binaryReader);

                if (readRow.TimeUtc >= start)
                {
                    if (readRow.TimeUtc <= stop)
                    {
                        rows.Add(readRow);
                    }
                }
                else
                {
                    firstRow = readRow;
                }

                if (readRow.TimeUtc >= stop)
                {
                    data.NextRow = readRow;
                    break;
                }
            }

            data.Name        = Metadata.Name;
            data.PreviousRow = firstRow;

            return(data);
        }
Beispiel #10
0
 public static NamedDataRow <T> Named <T>(this ISingleDataRow <T> rows, string name) where T : struct
 {
     return(new NamedDataRow <T>(name, rows));
 }
Beispiel #11
0
 public NamedDataRow(string name, ISingleDataRow <T> rows)
 {
     Name = name;
     Rows = rows;
 }