Advertisement
cortez

Beer Time

Sep 22nd, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Globalization;
  4.  
  5. namespace Problem_10__Beer_Time
  6. {
  7.     class BeerTime
  8.     {
  9.         public static void Main ()
  10.         {
  11.             long input = 0;
  12.  
  13.             try
  14.             {
  15.                 input = Convert.ToDateTime (Console.ReadLine ()).Ticks;
  16.             }
  17.             catch (FormatException e)
  18.             {
  19.                 Console.WriteLine (e.Message);
  20.                 return;
  21.             }
  22.  
  23.             // Let's count the ticks between times.
  24.  
  25.             // There was an issue with the fact that 1pm should be calculated as a date before midnight(the day before)
  26.             // and 3pm is after midnight, so on the next day.
  27.             // Here's how I devided to fix it. There are better ways for sure...
  28.             long midNight = Convert.ToDateTime ("00:00 AM").Ticks;
  29.             long start = Convert.ToDateTime ("01:00 PM").Ticks;
  30.             long end = Convert.ToDateTime ("03:00 AM").Ticks;
  31.  
  32.             bool isBeforeEndAfterMidnight = (input >= midNight) && (input < end);
  33.             bool isAfterStartAfterMidnight = (input >= midNight) && (input >= start);
  34.  
  35.             // Checking if it's beer time already...
  36.             if (isAfterStartAfterMidnight || isBeforeEndAfterMidnight)
  37.             {
  38.                 Console.WriteLine ("beer time");
  39.             }
  40.             else
  41.             {
  42.                 Console.WriteLine ("non-beer time");
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement