-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringTimer.cs
More file actions
150 lines (132 loc) · 4.46 KB
/
StringTimer.cs
File metadata and controls
150 lines (132 loc) · 4.46 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/// <summary>
/// Seconds converter to various string time formats.
/// </summary>
public class StringTimer
{
/// <summary>
/// Converts seconds to a string like mm:ss and hh:mm:ss if the seconds count is more than 3600.
/// </summary>
public static string ElectronicFormat(int time)
{
string displayedTime = "";
if (time < 60)
{
if (time < 10)
displayedTime = $"00:0{time}";
else
displayedTime = $"00:{time}";
}
else if ((time >= 60) && (time < 3600))
{
if ((int)(time / 60.0) < 10)
{
if (time % 60 < 10)
displayedTime = $"0{(int)(time / 60)}:0{time % 60}";
else
displayedTime = $"0{(int)(time / 60)}:{time % 60}";
}
else
{
if (time % 60 < 10)
displayedTime = $"{(int)(time / 60)}:0{time % 60}";
else
displayedTime = $"{(int)(time / 60)}:{time % 60}";
}
}
else if (time >= 3600)
{
int hour = 0;
if (time / 3600 > 1)
{
hour = ((int)time / 3600) - 1;
time -= (3600 * hour);
}
if (hour + 1 < 10)
{
if ((int)((time - 3600) / 60) < 10)
{
if (time % 60 < 10)
displayedTime = $"0{hour + 1}:0{(int)((time - 3600) / 60)}:0{time % 60}";
else
displayedTime = $"0{hour + 1}:0{(int)((time - 3600) / 60)}:{time % 60}";
}
else
{
if (time % 60 < 10)
displayedTime = $"0{hour + 1}:{(int)((time - 3600) / 60)}:0{time % 60}";
else
displayedTime = $"0{hour + 1}:{(int)((time - 3600) / 60)}:{time % 60}";
}
}
else
{
if ((int)((time - 3600) / 60) < 10)
{
if (time % 60 < 10)
displayedTime = $"{hour + 1}:0{(int)((time - 3600) / 60)}:0{time % 60}";
else
displayedTime = $"{hour + 1}:0{(int)((time - 3600) / 60)}:{time % 60}";
}
else
{
if (time % 60 < 10)
displayedTime = $"{hour + 1}:{(int)((time - 3600) / 60)}:0{time % 60}";
else
displayedTime = $"{hour + 1}:{(int)((time - 3600) / 60)}:{time % 60}";
}
}
}
return displayedTime;
}
/// <summary>
/// Converts seconds to a string like _s, _m _s if the seconds count is more than 60 and _h _m _s if the seconds count is more than 3600.
/// </summary>
public static string TextFormat(int time)
{
string displayedTime = "";
if (time < 60)
{
displayedTime = $"{time}s";
}
else if ((time >= 60) && (time < 3600))
{
if (time % 60 == 0)
displayedTime = $"{(int)(time / 60)}m";
else
displayedTime = $"{(int)(time / 60)}m {time % 60}s";
}
else if (time >= 3600)
{
int hour = 0;
if (time / 3600 > 1)
{
hour = ((int)time / 3600) - 1;
time -= (3600 * hour);
}
if ((int)((time - 3600) / 60) == 0)
{
if (time % 60 == 0)
displayedTime = $"{hour + 1}h";
else
displayedTime = $"{hour + 1}h {time % 60}s";
}
else
{
if (time % 60 == 0)
displayedTime = $"{hour + 1}h {(int)((time - 3600) / 60)}m";
else
displayedTime = $"{hour + 1}h {(int)((time - 3600) / 60)}m {time % 60}s";
}
}
return displayedTime;
}
}
/*
How to use:
1. Just call the public static methods "ElectronicFormat" or "TextFormat" from your script.
Documentation:
- The integer parameter "time" is the displayed time, which will be converted to the "string time" format.
You can also pass the float parameter just by using: (int)someFloatValue.
Comment:
- You can use this method for timers or clocks in your game.
*/