Or, at least that’s what I am calling it. The .NET framework provides quite a rich set of data structures for dealing with Dates. You’ve got DateTime which represents a single point in time, You’ve got TimeSpan which represents a interval.
However I noticed something somewhat absent- a DateTime Range, with a start time, and an ending time. This came about because I required a way to coalesce a set of DateTime’s organized as Starting and Ending time so that there weren’t any overlaps. For example, if I had datetimes for one interval from 9AM to 12PM, another from 8AM to 11AM, and a third from 5PM to 9PM, the coalesced result would be two intervals, one from 8AM to 12PM, and one from 5PM to 9PM. Basically, the point was to “simplify” a set of datetime intervals so that overlapping time would not be counted.
At first, I figured it would be built-in functionality of one of the DateTime classes. It’s not. so I did some googling, no real luck there either. So, I decided to write my own “DateRange” class that encapsulated a Start Time and an Ending time and had the functionality I required.
Obviously, the first thing we know is that the class is going to need Starting Time and Ending Time. Of course, once we’ve defined the data structures, the tricky part will be the “coalescing” code. The implementations for which can be found in the “CoalesceRanges” and “Coalesce” methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
public class DateRange : ICloneable { private DateTime _StartTime, _EndTime; public DateTime StartTime { get { return _StartTime;}} public DateTime EndTime { get { return _EndTime; }} public TimeSpan Span { get { return EndTime-StartTime; } set { _EndTime = StartTime+value; } } public DateRange(DateTime sTime, DateTime eTime) { if (sTime < eTime) _StartTime = sTime; else _StartTime=eTime; if (sTime > eTime) _EndTime = sTime; else _EndTime=eTime; } public override string ToString() { return "Range:(" + StartTime.ToString() + ")-(" + EndTime.ToString() + ")"; } public static DateRange[] Coalesce(DateRange RangeA, DateRange RangeB) { bool CondA = RangeA.StartTime > RangeB.EndTime; bool CondB = RangeA.EndTime < RangeB.StartTime; bool overlaps = !(CondA || CondB); // bool overlaps = (RangeA.StartTime > RangeB.EndTime) && (RangeA.EndTime < RangeB.StartTime); //overlap exists if neither A or B is true. if (overlaps) { //overlap exists. //create a new starttime the lower of the given two... DateTime newstart, newEnd; if (RangeA.StartTime < RangeB.StartTime) newstart = RangeA.StartTime; else newstart = RangeB.StartTime; if (RangeA.EndTime > RangeB.EndTime) newEnd = RangeA.EndTime; else newEnd = RangeB.EndTime; return new DateRange[] { new DateRange(newstart, newEnd) }; } else { //there is no overlap, return a daterange array with both the same elements. return new DateRange[] { (DateRange)RangeA.Clone(), (DateRange)RangeB.Clone() }; } } public DateRange[] Coalesce(DateRange Otherdaterange) { return DateRange.Coalesce(this, Otherdaterange); } public static DateRange[] CoalesceRanges(DateRange[] ranges) { //coalesces/simplifies a given set of date ranges to the simplest form. For example: /* a * |----| * b * |----| c * |----| * */ //will return an array of two Dateranges: /* a * |-----| * b * |----| * */ //this is the "simplest" reduction of what the previous ranges were. List<daterange> userange = ranges.ToList(); List<daterange> workalist = userange; List<daterange> result = new List<daterange>(); //algorithm //set a flag indicating a coalesce was discovered to true. //iterate while said flag is true. //set flag to false as loop begins. //iterate through every possible pair X and Y in the List. skip iterations where X==Y. //for each possible pairing: //coalesce the two DateRanges. If the results coalesce (the return value array has a length of 1) //mark x and y for removal, set the new Array to be AddRanged() to the List, and break out of both loops, and set the flag saying that //a coalesce was discovered. bool coalescefound =true; while (coalescefound) { List<daterange> removalmarked = new List<daterange>(); List<daterange> addmarked = new List<daterange>(); bool breakouter=false; coalescefound=false; foreach (DateRange x in workalist) { foreach (DateRange y in workalist) { if (x != y) { DateRange[] coalresult = DateRange.Coalesce(x, y); if (coalresult.Length == 1) { coalescefound=true; //add new array to be added... addmarked.AddRange(coalresult); //remove both x and y. We can't remove them here since we are iterating... removalmarked.Add(x); removalmarked.Add(y); breakouter=true; break; } } } if (breakouter) break; //break out of outer iterator if flag set. } //add and remove the marked items. foreach (var removeme in removalmarked) { workalist.Remove(removeme); } foreach (var addme in addmarked) { workalist.Add(addme); } } return workalist.ToArray(); } public object Clone() { return new DateRange(StartTime, EndTime); } } |
For this case I’ve decided that I would make the class Immutable. This should make our accessors simpler to implement. As a result, the StartTime and EndTime implementations only define get accessors. I also add a “Span” parameter that actually does change the EndTime in it’s setter, so I guess I lied. Oh well. Anyways, the meat of the class is in the various coalescing methods, which makes sense since that is what I sort of wrote the class to do. the class could surely be fleshed out with other, useful methods and properties, but as it is now it meets my own needs admirably so I’ve not really expanded it past what I’ve got here.
Have something to say about this post? Comment!