Advertisement
Filkolev

Chat Logger

May 27th, 2015
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Security;
  6. using System.Text.RegularExpressions;
  7.  
  8. public class ChatLogger
  9. {
  10.     public static void Main()
  11.     {
  12.         DateTime today = DateTime.ParseExact(
  13.             Console.ReadLine(),
  14.             "dd-MM-yyyy HH:mm:ss",
  15.             CultureInfo.InvariantCulture);
  16.  
  17.         var messages = new SortedDictionary<DateTime, string>();
  18.  
  19.         string input = Console.ReadLine();
  20.  
  21.         while (input != "END")
  22.         {
  23.             string[] tokens = Regex.Split(input, @"\s+\/\s+");
  24.  
  25.             string message = tokens[0];
  26.             DateTime date = DateTime.ParseExact(
  27.             tokens[1],
  28.             "dd-MM-yyyy HH:mm:ss",
  29.             CultureInfo.InvariantCulture);
  30.  
  31.             messages.Add(date, message);
  32.  
  33.             input = Console.ReadLine();
  34.         }
  35.  
  36.         foreach (var message in messages)
  37.         {
  38.             Console.WriteLine("<div>{0}</div>", SecurityElement.Escape(message.Value));
  39.         }
  40.  
  41.         var mostRecentPostDate = messages.Last().Key;
  42.         string time = string.Empty;
  43.  
  44.         var difference = (today - mostRecentPostDate);
  45.  
  46.         if (mostRecentPostDate.AddDays(1).Date == today.Date)
  47.         {
  48.             time = "yesterday";
  49.         }
  50.         else if (difference.TotalDays > 1)
  51.         {
  52.             time = string.Format("{0:dd-MM-yyyy}", mostRecentPostDate);
  53.         }
  54.         else
  55.         {
  56.             var seconds = (int)difference.TotalSeconds;
  57.  
  58.             if (seconds < 60)
  59.             {
  60.                 time = "a few moments ago";
  61.             }
  62.             else if (seconds < 60 * 60)
  63.             {
  64.                 time = string.Format(
  65.                     "{0} minute(s) ago",
  66.                     (int)difference.TotalMinutes);
  67.             }
  68.             else
  69.             {
  70.                 time = string.Format(
  71.                     "{0} hour(s) ago",
  72.                     (int)difference.TotalHours);
  73.             }
  74.         }
  75.  
  76.         Console.WriteLine("<p>Last active: <time>{0}</time></p>", time);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement