-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path016 - Human readable duration format.cs
90 lines (86 loc) · 1.88 KB
/
016 - Human readable duration format.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
// Human readable duration format
// C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /debug+ "016 - Human readable duration format.cs"
public class HumanTimeFormat
{
public static string formatDuration(int seconds)
{
List<string> list = new List<string>();
if (seconds < 0)
throw new ArgumentException("Invalid parameter");
if (seconds == 0)
return "now";
int s = seconds % 60;
if (s > 0)
{
if (s > 1)
list.Add(string.Format("{0} seconds", s));
else
list.Add("1 second");
}
seconds /= 60;
int m = seconds % 60;
if (m > 0)
{
if (m > 1)
list.Add(string.Format("{0} minutes", m));
else
list.Add("1 minute");
}
seconds /= 60;
int h = seconds % 24;
if (h > 0)
{
if (h > 1)
list.Add(string.Format("{0} hours", h));
else
list.Add("1 hour");
}
seconds /= 24;
int d = seconds % 365;
if (d > 0)
{
if (d > 1)
list.Add(string.Format("{0} days", d));
else
list.Add("1 day");
}
int y = seconds / 365;
if (y > 0)
{
if (y > 1)
list.Add(string.Format("{0} years", y));
else
list.Add("1 year");
}
if (list.Count == 1)
return list[0];
StringBuilder buffer = new StringBuilder();
for (int i = (list.Count - 1); i >= 0; i--)
{
if (buffer.Length > 0)
{
if (i == 0)
buffer.Append(" and ");
else
buffer.Append(", ");
}
buffer.Append(list[i]);
}
return buffer.ToString();
}
}
public static class KataTest
{
public static void Main()
{
Console.WriteLine("[{0}]", HumanTimeFormat.formatDuration(0));
Console.WriteLine("[{0}]", HumanTimeFormat.formatDuration(1));
Console.WriteLine("[{0}]", HumanTimeFormat.formatDuration(62));
Console.WriteLine("[{0}]", HumanTimeFormat.formatDuration(120));
Console.WriteLine("[{0}]", HumanTimeFormat.formatDuration(3662));
}
}