CopyCat Rails’ Time Extensions

I’ve become a fan of Ruby’s simple syntax. My favorite thing is the ActiveSupport’s Numeric Time Extensions. I’m starting a library of useful extensions, and I’m going to be adding a number of shortcuts to mock these extensions.

For those of you that don’t want to follow the link, check out the syntax:

  
  # equivalent to Time.now.advance(:months => 1)
  1.month.from_now

  # equivalent to Time.now.advance(:years => 2)
  2.years.from_now

  # equivalent to Time.now.advance(:months => 4, :years => 5)
  (4.months + 5.years).from_now

Here is what I’ve gotten so far (it only includes int for now):

public static class DateExtensionsThatMockRuby
    {
        /// <summary>
        /// Retrieve the number of ticks for x Days.
        /// <example>long ticks = 5.Days();</example>
        /// </summary>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Days(this int i)
        {
            return new TimeSpan(i, 0, 0, 0).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Hours.
        /// </summary>
        /// <example>long ticks = 15.Hours();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Hours(this int i)
        {
            return new TimeSpan(0, i, 0, 0).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Minutes
        /// </summary>
        /// <example>long ticks = 97.Minutes();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Minutes(this int i)
        {
            return new TimeSpan(0, i, 0).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Seconds
        /// </summary>
        /// <example>long ticks = 3000.Seconds();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Seconds(this int i)
        {
            return new TimeSpan(0, 0, i).Ticks;
        }

        /// <summary>
        /// Retrieve the number of ticks for x Milliseconds
        /// </summary>
        /// <example>long ticks = 3000.Milliseconds();</example>
        /// <param name="i"></param>
        /// <returns>ticks</returns>
        public static long Milliseconds(this int i)
        {
            return new TimeSpan(0, 0, 0, 0, i).Ticks;
        }

        /// <summary>
        /// Retrieve a DateTime object from ticks
        /// <example>DateTime dt = 236423690923466.AsDateTime();</example>
        /// /// <example>DateTime dt = 10.Days().AsDateTime();</example>
        /// </summary>
        /// <param name="ticks"></param>
        /// <returns>DateTime</returns>
        public static DateTime AsDateTime(this long ticks)
        {
            return new DateTime(ticks);
        }

        /// <summary>
        /// Retrieve a DateTime object from ticks
        /// <example>DateTime dt = 236423690923466.AsDateTime(DateTimeKind.Utc);</example>
        /// /// <example>DateTime dt = 10.Days().AsDateTime(DateTimeKind.Utc);</example>
        /// </summary>
        /// <param name="ticks"></param>
        /// <returns>DateTime</returns>
        public static DateTime AsDateTime(this long ticks, DateTimeKind kind)
        {
            return new DateTime(ticks, kind);
        }
    }