Advertisement
Guest User

Untitled

a guest
Nov 26th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class HumanTimeFormat
  6. {
  7.     [Flags]
  8.     enum TimeItmes { none = 0, seconds = 1, minutes = 2, hours = 4, days = 8, years = 16 }
  9.     private static readonly int year = 60 * 60 * 24 * 365;
  10.     private static readonly int day = 60 * 60 * 24;
  11.     private static readonly int hour = 60 * 60;
  12.     private static readonly int minute = 60;
  13.     private int[] _data = new int[5];
  14.     private TimeItmes _timeItmes;
  15.     HumanTimeFormat(int second)
  16.     {
  17.         while (second >= year)
  18.         {
  19.             second -= year;
  20.             _data[0]++;
  21.             _timeItmes |= TimeItmes.years;
  22.         }
  23.         while (second >= day)
  24.         {
  25.             second -= day;
  26.             _data[1]++;
  27.             _timeItmes |= TimeItmes.days;
  28.         }
  29.         while (second >= hour)
  30.         {
  31.             second -= hour;
  32.             _data[2]++;
  33.             _timeItmes |= TimeItmes.hours;
  34.         }
  35.         while (second >= minute)
  36.         {
  37.             second -= minute;
  38.             _data[3]++;
  39.             _timeItmes |= TimeItmes.minutes;
  40.         }
  41.         if (second > 0)
  42.         {
  43.             _data[4] = second;
  44.             _timeItmes |= TimeItmes.seconds;
  45.         }
  46.     }
  47.  
  48.     public string ToString()
  49.     {
  50.         if (_timeItmes == TimeItmes.none)
  51.         {
  52.             return "now";
  53.         }
  54.         List<string> components = new();
  55.         if ((_timeItmes & TimeItmes.years) == TimeItmes.years)
  56.         {
  57.             components.Add($"{_data[0]} year{(_data[0] > 1 ? "s" : "")}");
  58.         }
  59.         if ((_timeItmes & TimeItmes.days) == TimeItmes.days)
  60.         {
  61.             components.Add($"{_data[1]} day{(_data[1] > 1 ? "s" : "")}");
  62.         }
  63.         if ((_timeItmes & TimeItmes.hours) == TimeItmes.hours)
  64.         {
  65.             components.Add($"{_data[2]} hour{(_data[2] > 1 ? "s" : "")}");
  66.         }
  67.         if ((_timeItmes & TimeItmes.minutes) == TimeItmes.minutes)
  68.         {
  69.             components.Add($"{_data[3]} minute{(_data[3] > 1 ? "s" : "")}");
  70.         }
  71.         if ((_timeItmes & TimeItmes.seconds) == TimeItmes.seconds)
  72.         {
  73.             components.Add($"{_data[4]} second{(_data[4] > 1 ? "s" : "")}");
  74.         }
  75.  
  76.         return components.Count switch
  77.         {
  78.             1 => components[0],
  79.             2 => $"{components[0]} and {components[1]}",
  80.             _ => $"{string.Join(", ", components.Take(components.Count - 1))} and {components[^1]}"
  81.         };
  82.     }
  83.  
  84.     public static string formatDuration(int seconds) => new HumanTimeFormat(seconds).ToString();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement