5 Replies Latest reply on Dec 8, 2011 7:11 AM by eignert

    Convert TimeStepString to TimeSpan




      I've read through the PITimeserver help but could not find an easy way to convert a timestep string as "30s" or "10d" into a .Net type TimeSpan...
      Is there a best practice for achieving this? I'd need this functionality to split a time range for a query into multiple intervals and therefor I need to know this timestep...
      I think the "ParseTimeStepString" method could be a good start but how do you recommend to use it?



        • Re: Convert TimeStepString to TimeSpan

          Hi Thomas,


          I did not find a nice way to do it using the 'ParseTimeStepString' from the ITimeIntervals2 interface.


          When using DynamicTime, and use a reference date that subtracts from the aquired date it works. It's not 100% elegant, but I think it does the job.


          One sidenote: DynamicTime.InputString expects an offset notation with a + or a -, so "30s" will not parse, but "+30s" or "-30s" will. That's why this method assumes a positive offset when none is given.

          private static TimeSpan GetTimespanFromTimeOffsetNotation(string notation)
                      if (!notation.StartsWith("+") && !notation.StartsWith("-"))
                          notation = "+" + notation;
                      var refTime = DateTime.Today;
                      DynamicTime time = new DynamicTime();
                      time.InputString = notation;
                      time.ReferenceTime = new PITime() { LocalDate = refTime };
                      var newTime = time.LocalDate;
                      var diff = newTime.Subtract(refTime);
                      return diff;



          and you can use it like this:

            var diff1 = GetTimespanFromTimeOffsetNotation("30s");
                      var diff2 = GetTimespanFromTimeOffsetNotation("-5d");
                      var diff3 = GetTimespanFromTimeOffsetNotation("+1y");