Tuesday 22 January 2013

DateTime Start and End of Day Extension Methods

Another quick post with some useful extension methods for the System.DateTime struct. I've been working a lot with the DateTime type over the past couple of weeks. One particular use case has been to pass a "start" and an "end" DateTime to some SQL Server stored procedure. The stored procedure then returns some data based on some timestamp field that falls between the start and end DateTime's passed in.

In the code I was maintaining, the previous developer had created a DateTime value using DateTime.Now, which returns a DateTime with the time part also populated to the current system time. So when passing this DateTime to the stored proc as a "start" DateTime, the proc was looking from the time-part onwards on the specific day represented by the DateTime. Likewise, when using a similarly created DateTime for the "end" DateTime, the stored proc only returned rows upto the time-part. This was undesired behaviour because the original intention was to retrieve all of the records between two specified days (inclusive of the start and end days).

I decided to tackle this at the application-level. This was mainly to keep the stored procedure flexible so that it can continue to handle retrieval of records at the more granular time level if required. I decided to implement this functionality by adding two extension methods on DateTime, namely, ResetTimeToStartOfDay and ResetTimeToEndOfDay. These return new DateTime values with the time part appropriately set. The idea is to call ResetTimeToStartOfDay on the "start" DateTime and ensure that the returned DateTime is sent to the stored procedure. Likewise, call ResetTimeToEndOfDay on the "end" DateTime. I've included the implementation of the two methods below - very simple but useful to have in your library.

public static DateTime ResetTimeToStartOfDay(this DateTime dateTime)
{
   return new DateTime(
      dateTime.Year, 
      dateTime.Month, 
      dateTime.Day, 
      0, 0, 0, 0);
}

public static DateTime ResetTimeToEndOfDay(this DateTime dateTime)
{
   return new DateTime(
      dateTime.Year, 
      dateTime.Month, 
      dateTime.Day, 
      23, 59, 59, 999);
}

Thursday 17 January 2013

Highcharts JS Library - Overlapping Points

I'm currently working on a small ASP.NET MVC4 application that reports some live numerical data in a chart. My manager pointed me to Highcharts JS which is a neat and simple to setup library. The particular chart type we chose to use was this.

The chart sits in its own partial view. It is periodically polling an action that returns data in JSON format for it to plot along an X-axis representing time. If you take a look at the JavaScript for the demo of the chart we decided to use (link above), it has two places where points are plotted.

Firstly, it plots points when you define a series for the chart - this is where I am plotting all the initial data accumulated since the view was requested by a user. Then, I'm plotting any subsequent data that has just been accumulated using a timer that is defined in the load event for the chart (this is the part that polls the action I mentioned earlier).

When plotting the chart in this way, I ran into an issue when we had no initial data to plot. In this scenario, the chart initially (and correctly) displays as empty. However, as data was coming in from my timer calls to the action - only one point would ever be plotted. After investigating this, it turned out that it was because I was passing in the boolean value true for the "shift" parameter to the Series.addPoint function.

This parameter tells the chart library to shift the graph to the left (thus removing the left-most plot) and hence my last plotted point was always being lost on each poll to the action. This in-turn gave the illusion that my points were being overwritten. To fix this, I passed in a boolean expression that tests if the series has 30 points plotted - this ensured that the chart always displays 30 plotted points at the most and resolved the problem of "overwriting" plotted points!