rdavin

More AFTime extensions

Blog Post created by rdavin Employee on Aug 31, 2012

As my Friday is winding down before a 3-day weekend, I'm in limbo.  Too early to head out to happy hour but too late to start with a new project.  Sounds like a good time to do a quick blog.  I've been working with AF 2.5 and discovered that 3 of my 12 custom data references will need to override some RDA methods.  In my code I frequently needed to widen a time range by 1 second on each end.  This isn't hard to do.  It would look something like:

 
AFTimeRange tr = new AFTimeRange(new AFTime(timeContext.StartTime.UtcSeconds - 1.0), new AFTime(timeContext.EndTime.UtcSeconds + 1.0));

 

 

If one carefully reads across the line, one should be able to see what is going on there.  One nit picky issue I have it that its an eyeful.  One must carefully read across it.  It would be nicer to comprehend what is going on without paying so much attention to it.  Compare that to:

 
AFTimeRange tr = new AFTimeRange(timeContext.StartTime.AddSeconds(-1.0), timeContext.EndTime.AddSeconds(1.0));

 

 

To me, its just a little easier read.  While there isn't actually an .AddSeconds method available to the AFTime object, there is nothing stopping me from creating my own extension method.  Be sure to put any extension methods in a static class.  Note to VB users: a static class in C# is the equivalent of a Module in VB.

 
static public AFTime AddSeconds(this AFTime time, double seconds)
{
    return new AFTime(time.UtcSeconds + seconds);
}

 

 

If I'm going to take time to create such a method, I want it to do just a little bit more than be a convenient method than make it easier on the eyes.  I probably should have it safely add seconds to a given time.  There are 2 general possibilities for an unsafe operation.  One would be if resulting sum of UtcSeconds and seconds would be outside the range of an AFTime object.  The other would be if seconds would ever be NaN, +/- infinity, or if the sum of seconds + UtcSeconds was greater than a double's MaxValue.  I choose to ignore the 2nd possibility within my code because it will never happen. As for the first case, I seriously doubt the output time would ever exceed AFTime.MaxValue but I will include it for completeness.  However there is a rare but likely enough situation where the input time could be AFTime.MinValue (i.e. 1/1/1970) and you do not want to subtract 1 second from it.  So one possible safe version would look like:

 
static public AFTime AddSeconds(this AFTime time, double seconds)
{
    double sum = time.UtcSeconds + seconds;
    if (sum <= AFTime.MinValue.UtcSeconds)
    {
        // Rare but still very likely possibility that variable ‘time’ could be 1/1/1970. 
        return AFTime.MinValue;
    }
    else if (sum >= AFTime.MaxValue.UtcSeconds)
    {
        // Seriously doubt this would ever happen but included for completeness. 
        return AFTime.MaxValue;
    }
    else
    {
        return new AFTime(sum);
    }
}

 

 

I really do love extension methods.  Not long after creating the above, I needed an .AddHours method too!  Here's what it looked like:

 
static public AFTime AddHours(this AFTime time, double hours)
{
    return AddSeconds(time, hours * 60.0 * 60.0);
}

 

 

That about wraps this up.  Still got some time left in my work day.  Guess I will go sharpen pencils or something.

Outcomes