/// <summary> /// Add time and value to the timeseries. The added record will be placed according the the /// specified time. If the time specified already exists the corresponding value will be overwritten /// with the new value /// </summary> /// <param name="timeValue">time and value to add</param> public void AddValue(DateTime time, double value) { //Comment by JAG: Var det ikke bedre at indsætte dem i en usorteret list og så sortere hvis det bliver nødvendigt. Lige som med TimeSpan. TimestampValue timeValue = new TimestampValue(time, value); if (this.Items.Count == 0 || this.Items[Items.Count - 1].Time < timeValue.Time) { Items.Add(timeValue); } else if (this.items[0].Time > timeValue.Time) //add the record to the beginning of the list { Items.Insert(0, timeValue); } else //overwrite values if time already exists in the list or insert according to the time. { foreach (TimestampValue tv in items) { if (tv.Time == timeValue.Time) { tv.Value = timeValue.Value; } } int timeValuesListCount = items.Count; for (int i = 0; i < timeValuesListCount - 1; i++) { if (items[i].Time < timeValue.Time && timeValue.Time < items[i + 1].Time) { Items.Insert(i + 1, timeValue); } } } }
/// <summary> /// Add time and values to the end of the timeseries. /// Time is calculated based on the times for the last two existing records. If only /// one record exists in the time series, the time step length is set to one day. If /// the timeseries is empty, the time for the added record will be January 1st 2020. /// </summary> public override void AppendValue(double value) { if (items.Count >= 2) { int yearDiff = items[items.Count - 1].Time.Year - items[items.Count - 2].Time.Year; int monthDiff = items[items.Count - 1].Time.Month - items[items.Count - 2].Time.Month + yearDiff * 12; int dayDiff = items[items.Count - 1].Time.Day - items[items.Count - 2].Time.Day; int hourDiff = items[items.Count - 1].Time.Hour - items[items.Count - 2].Time.Hour; int minuteDiff = items[items.Count - 1].Time.Minute - items[items.Count - 2].Time.Minute; int secondDiff = items[items.Count - 1].Time.Second - items[items.Count - 2].Time.Second; DateTime newDateTime; if (monthDiff != 0 && dayDiff == 0 && hourDiff == 0 && minuteDiff == 0 && secondDiff == 0) { newDateTime = items[items.Count - 1].Time.AddMonths(monthDiff); } else { newDateTime = items[items.Count - 1].Time.AddTicks(items[items.Count - 1].Time.Ticks - items[items.Count - 2].Time.Ticks); } Items.Add(new TimestampValue(newDateTime, value)); } else if (items.Count == 1) { DateTime newDateTime = new DateTime(items[0].Time.Ticks); newDateTime = newDateTime.AddDays(1); Items.Add(new TimestampValue(newDateTime, value)); } else if (items.Count == 0) { TimestampValue timeValue = new TimestampValue(); timeValue.Value = value; Items.Add(timeValue); } else { throw new Exception("unexpected exception when adding time series record"); } }
public TimestampValue(TimestampValue obj) : this() { val = obj.Value; time = obj.Time; }
public void Equals() { TimestampValue tsv1 = new TimestampValue(new DateTime(2010, 1, 1), 5.5); TimestampValue tsv2 = new TimestampValue(new DateTime(2010, 1, 1), 5.5); Assert.AreEqual(tsv1, tsv2); tsv2.Time = new DateTime(2010, 1, 3); Assert.AreNotEqual(tsv1, tsv2); tsv2 = new TimestampValue(new DateTime(2010, 1, 1), 5.5); Assert.AreEqual(tsv1, tsv2); tsv2.Value = 44; Assert.AreNotEqual(tsv1, tsv2); }
public void Value() { TimestampValue timeValue = new TimestampValue(); timeValue.Value = 4.3; Assert.AreEqual(4.3, timeValue.Value); }
public void TimestampValue() { // Default constructor TimestampValue timeValue = new TimestampValue(); DateTime dateTime = new DateTime(2010, 01, 01, 17, 18, 35); timeValue.Time = dateTime; timeValue.Value = 5.4; Assert.AreEqual(5.4, timeValue.Value); Assert.AreEqual(2010, timeValue.Time.Year); Assert.AreEqual(35, timeValue.Time.Second); // Second constructor timeValue = new TimestampValue(dateTime, 5.4); Assert.AreEqual(5.4, timeValue.Value); Assert.AreEqual(2010, timeValue.Time.Year); Assert.AreEqual(35, timeValue.Time.Second); // Third constructor TimestampValue tsv1 = new TimestampValue(new DateTime(2010,1,1), 4.4); TimestampValue tsv2 = new TimestampValue(tsv1); Assert.AreEqual(tsv1.Time, tsv2.Time); Assert.AreEqual(tsv1.Value, tsv2.Value); }
public void PropertyChangedEvent() { TimestampValue timeValue = new TimestampValue(new DateTime(2010, 12, 03, 20, 34, 23), 4.3); timeValue.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(timeValue_PropertyChanged); eventWasRaised = false; timeValue.Value = 22.2; Assert.IsTrue(eventWasRaised); eventWasRaised = false; timeValue.Time = new DateTime(2010, 01, 01, 12, 12, 12); Assert.IsTrue(eventWasRaised); }
public void Time() { TimestampValue timeValue = new TimestampValue(); DateTime dateTime = new DateTime(2010,12,03,20,34,23); timeValue.Time = dateTime; Assert.AreEqual(dateTime, timeValue.Time); }
private void Summarize(int MaxCount, BaseTimeSeries bts) { TimespanSeries tspan = bts as TimespanSeries; TimestampSeries tstam = bts as TimestampSeries; if (tspan != null) { if (tspan.Items.Count > MaxCount) { List<TimespanValue> temp = new List<TimespanValue>(); DateTime Start = tspan.Items.First().StartTime; DateTime End = tspan.Items.Last().EndTime; double periodDays = End.Subtract(Start).TotalDays / MaxCount; for (int i = 0; i < MaxCount; i++) { TimespanValue TValue = new TimespanValue(Start.AddDays(i * periodDays), Start.AddDays((i + 1) * periodDays), 0); TValue.Value = tspan.GetValue(TValue.StartTime, TValue.EndTime); temp.Add(TValue); } tspan.Items.Clear(); foreach (var v in temp) tspan.Items.Add(v); } } if (tstam != null) { if (tstam.Items.Count > MaxCount) { List<TimestampValue> temp = new List<TimestampValue>(); DateTime Start = tstam.Items.First().Time; DateTime End = tstam.Items.Last().Time; double periodDays = End.Subtract(Start).TotalDays / MaxCount; for (int i = 0; i < MaxCount; i++) { TimestampValue TValue = new TimestampValue(Start.AddDays(i * periodDays), 0); TValue.Value = tstam.GetValue(TValue.Time); temp.Add(TValue); } tstam.Items.Clear(); foreach (var v in temp) tstam.Items.Add(v); } } }