Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 3.11 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Dealing with hours with no minutes when rounding DateTime to the 30 minutes
  2. protected DateTime GetStartTime()
  3. {
  4.     var spanTicks = TimeSpan.FromMinutes(30).Ticks;
  5.     var ticks = (Time.Now.Ticks + spanTicks - 1) / spanTicks;
  6.  
  7.     return new DateTime(ticks * spanTicks);
  8. }
  9.        
  10. var now = DateTime.Now;
  11. DateTime result = now.AddMinutes(now.Minute >= 30 ? (60-now.Minute) : (30-now.Minute));
  12. result = result.AddSeconds(-1* result.Second); // To reset seconds to 0
  13. result = result.AddMilliseconds(-1* result.Millisecond); // To reset milliseconds to 0
  14.        
  15. protected DateTime GetStartTime() {
  16.     var now = DateTime.Now;
  17.     int addHours;
  18.     int minute;
  19.     if (now.Minute >= 30) {
  20.         addHour = 1;
  21.         minute = 0;
  22.     } else {
  23.         addHour = 0;
  24.         minute = 30;
  25.     }
  26.     return new DateTime(now.Year, now.Month, now.Day, hour, minute, 0).AddHours(addHours);
  27. }
  28.        
  29. protected DateTime GetStartTime()
  30. {
  31.     DateTime dt=DateTime.Now;
  32.     if (dt.Minute<30)
  33.     {
  34.         dt=dt.AddMinutes(30-dt.Minute);
  35.     }
  36.     else
  37.     {
  38.         dt=dt.AddMinutes(60-dt.Minute);
  39.     }
  40.  
  41.     //dt now has the upcoming half-hour border
  42.  
  43.     //...
  44.  
  45. }
  46.        
  47. private static readonly long _ticksIn30Mins = TimeSpan.FromMinutes(30).Ticks;
  48.  
  49. protected DateTime GetRoundedTime(DateTime inputTime)
  50. {    
  51.     long currentTicks = inputTime.Ticks;
  52.     return new DateTime(currentTicks.RoundUp(_ticksIn30Mins));
  53. }
  54.  
  55. public static class ExtensionMethods
  56. {    
  57.     public static long RoundUp(this long i, long toTicks)    
  58.     {        
  59.         return (long)(Math.Round(i / (double)toTicks,
  60.                   MidpointRounding.AwayFromZero)) * toTicks;
  61.     }
  62. }
  63.        
  64. var currentTime = DateTime.Now;
  65. var rounded = GetRoundedTime(currentTime);
  66. if (rounded == currentTime)
  67. {
  68.     rounded = new DateTime(rounded.Ticks + _ticksIn30Mins);
  69. }
  70.        
  71. using System;
  72. using System.Collections.Generic;
  73. using System.Linq;
  74. using System.Text;
  75.  
  76. namespace ConsoleApplication1
  77. {
  78.     class Program
  79.     {
  80.         private static readonly long _ticksIn30Mins = TimeSpan.FromMinutes(30).Ticks;
  81.  
  82.         static void Main(string[] args)
  83.         {
  84.             WriteDateString(new DateTime(2012, 01, 18, 09, 45, 11, 152));
  85.             WriteDateString(new DateTime(2012, 01, 18, 12, 15, 11, 999));
  86.             WriteDateString(new DateTime(2012, 01, 18, 12, 00, 00, 000));
  87.  
  88.             Console.ReadLine();
  89.         }
  90.  
  91.         private static void WriteDateString(DateTime dateTime)
  92.         {
  93.             Console.WriteLine("Before: {0}, After: {1}", dateTime, GetRoundedTime(dateTime));
  94.         }
  95.  
  96.         private static DateTime GetRoundedTime(DateTime inputTime)
  97.         {
  98.             long currentTicks = inputTime.Ticks;
  99.             var rounded = new DateTime(currentTicks.RoundUp(_ticksIn30Mins));
  100.             if (rounded == inputTime)
  101.             {
  102.                 rounded = new DateTime(rounded.Ticks + _ticksIn30Mins);
  103.             }
  104.             return rounded;
  105.         }
  106.     }
  107.  
  108.     public static class ExtensionMethods
  109.     {
  110.         public static long RoundUp(this long i, long toTicks)
  111.         {
  112.             return (long)(Math.Round(i / (double)toTicks, MidpointRounding.AwayFromZero)) * toTicks;
  113.         }
  114.     }  
  115. }