Advertisement
NPSF3000

Simple Huge Time

Sep 7th, 2012
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Test0_34
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var a = 1532655487231;
  14.             var b = 7456328877414;
  15.             var c = Math.Abs(a - b);
  16.  
  17.             Console.WriteLine("Time Between A and B");
  18.             Console.WriteLine("In seconds: {0}", c);
  19.             Console.WriteLine("In minutes: {0}", c / 60);
  20.             Console.WriteLine("In hours: {0}", c / 60 / 60);
  21.             Console.WriteLine("In days: {0}", c / 60 / 60 / 24);
  22.             Console.WriteLine("In weeks: {0}", c / 60 / 60 / 24 / 7);
  23.             Console.WriteLine("In months: {0}", c / 60 / 60 / 24 / 30);
  24.             Console.WriteLine("In years: {0}", c / 60 / 60 / 24 / 365);
  25.             Console.WriteLine("In millenia: {0}", c / 60 / 60 / 24 / 365 / 1000);
  26.             Console.WriteLine("");
  27.             Console.WriteLine("------------------");
  28.             Console.WriteLine("");
  29.             Console.WriteLine("Pretty Print Time");
  30.  
  31.             Func<long, string> timeToString = t =>
  32.                 {
  33.                     var millenia = t / (60L * 60 * 24 * 365 * 1000);
  34.                     t = t % (60L * 60 * 24 * 365 * 1000);
  35.                     var years = t / (60L * 60 * 24 * 365);
  36.                     t = t % (60L * 60 * 24 * 365);
  37.                     var months = t / (60L * 60 * 24 * 30);
  38.                     t = t % (60L * 60 * 24 * 30);
  39.                     var weeks = t / (60L * 60 * 24 * 7);
  40.                     t = t % (60L * 60 * 24 * 7);
  41.                     var days = t / (60L * 60 * 24);
  42.                     t = t % (60L * 60 * 24);
  43.                     var hours = t / (60L * 60);
  44.                     t = t % (60L * 60);
  45.                     var mins = t / (60L);
  46.                     var seconds = t % (60L);
  47.  
  48.                     return string.Concat(
  49.                      millenia > 0 ? millenia.ToString() : "",
  50.                      millenia > 0 ? " millenia " : "",
  51.                      years > 0 ? years.ToString() : "",
  52.                      years > 0 ? " years " : "",
  53.                      months > 0 ? months.ToString() : "",
  54.                      months > 0 ? " months " : "",
  55.                      weeks > 0 ? weeks.ToString() : "",
  56.                      weeks > 0 ? " weeks " : "",
  57.                      days > 0 ? days.ToString() : "",
  58.                      days > 0 ? " days " : "",
  59.                      hours > 0 ? hours.ToString() : "",
  60.                      hours > 0 ? " hours " : "",
  61.                      mins > 0 ? mins.ToString() : "",
  62.                      mins > 0 ? " minutes " : "",
  63.                      seconds > 0 ? seconds.ToString() : "",
  64.                      seconds > 0 ? " seconds " : ""
  65.                      );
  66.                 };
  67.  
  68.             Console.WriteLine("a {0}", timeToString(a));
  69.             Console.WriteLine("b {0}", timeToString(b));
  70.             Console.WriteLine("c {0}", timeToString(c));
  71.             Console.ReadLine();
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement