Advertisement
dimipan80

Chat Logger

May 28th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. /* Write a program that receives several messages and the current date, sorts those messages by date and prints the time from the last message to the current date.
  2.  * The messages will be in format [message] / [date], where date is in format dd-mm-YYYY hours:min:secs. The messages should be sorted by date and their text should be printed in <div></div> tags on a separate line.
  3.  * Finally, the time since the most recent message should be printed in the following way: a few moments ago if it was less than 1 minute ago; [full minutes] minute(s) ago if it was less than 1 hour ago; [full hours] hour(s) ago if it was less than 24 hours ago on the same day; yesterday if it was the previous date, regardless of the time difference; [date] if it was earlier than yesterday, where date is in format dd-mm-YYYY.
  4.  * The resulting timestamp should be printed as follows:  <p>Last active: <time>[timestamp]</time></p>.
  5.  * The current time will be received on the first input line. The messages come will be received on the next input lines, each message on a separate line. The input ends with the command "END".
  6.  * Each message should be printed in its own <div></div> tags, on a separate line. On the final line, the difference between the current date and the last message date should be printed in the format described above. Ensure the HTML special characters are correctly encoded (use SecurityElement.Excape).
  7.  * All input dates will be in format dd-mm-YYYY hours:min:secs. The received messages will always be in format [message] / [date] (with space around the delimiter '/'). */
  8.  
  9. namespace Chat_Logger
  10. {
  11.     using System;
  12.     using System.Globalization;
  13.     using System.Collections.Generic;
  14.     using System.Security;
  15.     using System.Text.RegularExpressions;
  16.     using System.Threading;
  17.  
  18.     class ChatLogger
  19.     {
  20.         private static SortedDictionary<DateTime, string> messagesList;
  21.  
  22.         static void Main(string[] args)
  23.         {
  24.             Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  25.  
  26.             DateTime currentTime = DateTimeParseCustomFormatInputString(Console.ReadLine());
  27.  
  28.             messagesList = new SortedDictionary<DateTime, string>();
  29.  
  30.             GetMessagesFromInput();
  31.  
  32.             PrintMessagesInfo(currentTime);
  33.         }
  34.  
  35.         private static DateTime DateTimeParseCustomFormatInputString(string inputDateTimeStr)
  36.         {
  37.             const string inputDateTimeFormatPattern = "dd-MM-yyyy HH:mm:ss";
  38.             return DateTime.ParseExact(inputDateTimeStr, inputDateTimeFormatPattern, CultureInfo.InvariantCulture);
  39.         }
  40.  
  41.         private static void GetMessagesFromInput()
  42.         {
  43.             Regex rgx = new Regex(@"(.+?)\s+\/\s+(\d{2}-\d{2}-\d{4}\s\d{2}:\d{2}:\d{2})");
  44.             string inputLine = Console.ReadLine();
  45.             while (inputLine != "END")
  46.             {
  47.                 Match match = rgx.Match(inputLine);
  48.                 string message = match.Groups[1].Value;
  49.                 DateTime messageTime =
  50.                     DateTimeParseCustomFormatInputString(match.Groups[2].Value);
  51.                 messagesList.Add(messageTime, message);
  52.  
  53.                 inputLine = Console.ReadLine();
  54.             }
  55.         }
  56.  
  57.         private static void PrintMessagesInfo(DateTime startTime)
  58.         {
  59.             int counter = 0;
  60.             foreach (var entry in messagesList)
  61.             {
  62.                 Console.WriteLine("<div>{0}</div>",
  63.                         SecurityElement.Escape(entry.Value));
  64.  
  65.                 if (counter == messagesList.Count - 1)
  66.                 {
  67.                     Console.WriteLine("<p>Last active: <time>{0}</time></p>",
  68.                         GetDifferenceBetweenCurrentTimeAndLastMessageTime(startTime, entry.Key));
  69.                 }
  70.  
  71.                 counter++;
  72.             }
  73.         }
  74.  
  75.         private static string GetDifferenceBetweenCurrentTimeAndLastMessageTime(DateTime checkTime, DateTime lastMessageTime)
  76.         {
  77.             TimeSpan diffTime = checkTime - lastMessageTime;
  78.             string checkDate = checkTime.ToShortDateString();
  79.             string lastDate = lastMessageTime.ToShortDateString();
  80.  
  81.             DateTime tempDate = lastMessageTime.AddDays(1);
  82.             if (tempDate.Day < checkTime.Day)
  83.             {
  84.                 return string.Format("{0}-{1}-{2}",
  85.                     lastMessageTime.Day.ToString().PadLeft(2, '0'),
  86.                     lastMessageTime.Month.ToString().PadLeft(2, '0'), lastMessageTime.Year);
  87.             }
  88.  
  89.             if (tempDate.Day == checkTime.Day)
  90.             {
  91.                 return "yesterday";
  92.             }
  93.  
  94.             if (lastDate == checkDate && diffTime.Hours >= 1)
  95.             {
  96.                 return string.Format("{0} hour(s) ago", diffTime.Hours);
  97.             }
  98.  
  99.             return diffTime.TotalMinutes >= 1 ? string.Format("{0} minute(s) ago", diffTime.Minutes) : "a few moments ago";
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement