Wednesday, February 9, 2011

I’m bored, so I code. Stupid extension methods that might be useful. (Probably not)

Chad Myers keeps tweeting things about dates, and it reminded me of something I read about some cool extension methods to make using dates easier. I can’t find the thing I read (mostly because I didn’t look), but I figured I’d go blog it really quick.

I wanna add some extension methods for int to make getting Dates easier. Something like this

   1:   [Test]
   2:          public void days()
   3:          {
   4:              var timespan = 1.Days();
   5:              Assert.AreEqual(1,timespan.Days);
   6:   
   7:          }

Oh, cool…. Easy to make it work too. Just like this.


   1:  public static TimeSpan Days(this int days)
   2:          {
   3:              return new TimeSpan(days,0,0,0,0);
   4:          }


Seriously though if I need to get a timespan for one day, really… Look at line 3, that’s some ugly stuff, aint it? I like the extension method.


Lets make it a little more valuable.


   1:  [Test]
   2:          public void ago()
   3:          {
   4:              var expected = DateTime.Today.AddDays(-2);
   5:              var actual = 2.Days().Ago();
   6:              Assert.AreEqual(expected,actual);
   7:          }

Simple enough to make it work, right?


   1:  public static DateTime Ago(this TimeSpan timeSpan)
   2:          {
   3:              return DateTime.Now.Subtract(timeSpan);
   4:          }


Ok, so now lets get really cute.


   1:  [Test]
   2:          public void honest_abe()
   3:          {
   4:              var expected = DateTime.Now.AddYears(-87);
   5:              var actual = 4.Score().And(7).Years().Ago();
   6:   
   7:              Assert.AreEqual(expected.Year,actual.Year);
   8:          }


Wow, I’m stupid, huh? Hehe…


Here’s what I did.


   1:   public static class DateExtensions
   2:      {
   3:          public static TimeSpan Days(this int days)
   4:          {
   5:              return new TimeSpan(days,0,0,0,0);
   6:          }
   7:          public static TimeSpan Hours(this int hours)
   8:          {
   9:              return new TimeSpan(hours,0,0);
  10:          }
  11:          public static TimeSpan Minutes(this int minutes)
  12:          {
  13:              return new TimeSpan(0,minutes,0);
  14:          }
  15:          public static TimeSpan Years(this int years)
  16:          {
  17:              var leapYears = years/4;
  18:              return new TimeSpan(365*years + leapYears, 0, 0, 0);
  19:          }
  20:          public static DateTime Ago(this TimeSpan timeSpan)
  21:          {
  22:              return DateTime.Now.Subtract(timeSpan);
  23:          }
  24:          public static int Score(this int val)
  25:          {
  26:              return val*20;
  27:          }
  28:          public static int And(this int left,int right)
  29:          {
  30:              return left + right;
  31:          }
  32:      }

Wow, what a stupid blog huh? I think my next blog post is gunna be cool though… think Amazon, think cloud, think Ellemy.CQRS and publishing…

1 comment:

  1. nice method to kill time.
    very interesting

    ReplyDelete