Repository navigation
Expand file tree
/
Copy pathUtilities.cs
More file actions
196 lines (170 loc) · 5.73 KB
/
Copy pathUtilities.cs
File metadata and controls
196 lines (170 loc) · 5.73 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace QuizFlash
{
public class Utilities
{
// To Get the Initials of a name
public static string GetInitials(string username)
{
var names = username.Split(' ');
if (names.Length >= 2)
{
return $"{names[0][0]}{names[1][0]}";
}
if (names.Length == 1)
{
return $"{names[0][0]}";
}
return "NN";
}
// To Get the current time in Epoch
public static long GetCurrentTimeInEpoch() {
DateTime currentTime = DateTime.UtcNow;
DateTimeOffset dateTimeOffset = new DateTimeOffset(currentTime);
return dateTimeOffset.ToUnixTimeSeconds();
}
//Determining the grade of a student based on marks
public static string GetGrade(double marks)
{
switch (marks)
{
case double n when (n >= 90):
return "A+";
case double n when (n >= 80):
return "A";
case double n when (n >= 70):
return "B";
case double n when (n >= 60):
return "C";
case double n when (n >= 50):
return "D";
default:
return "F";
}
}
// To Convert Epoch to Relative time (just now, 2 hours ago etc.)
public static string ConvertEpochToRelativeTime(long epochTimestamp)
{
var dateTime = DateTimeOffset.FromUnixTimeSeconds(epochTimestamp);
var timeSpan = DateTime.UtcNow - dateTime.UtcDateTime;
if (timeSpan.TotalDays >= 1)
{
return $"{(int)timeSpan.TotalDays} days ago";
}
if (timeSpan.TotalHours >= 1)
{
return $"{(int)timeSpan.TotalHours} hours ago";
}
if (timeSpan.TotalMinutes >= 1)
{
return $"{(int)timeSpan.TotalMinutes} minutes ago";
}
return "just now";
}
// This is to get the relative time of the future
public static string ConvertEpochToRelativeTimeFuture(long epochTimestamp)
{
var dateTime = DateTimeOffset.FromUnixTimeSeconds(epochTimestamp).UtcDateTime;
var now = DateTime.UtcNow;
var timeSpan = dateTime - now;
if (timeSpan.TotalDays >= 1)
{
return $"in {(int)timeSpan.TotalDays} days";
}
if (timeSpan.TotalHours >= 1)
{
return $"in {(int)timeSpan.TotalHours} hours";
}
if (timeSpan.TotalMinutes >= 1)
{
return $"in {(int)timeSpan.TotalMinutes} minutes";
}
return "just now";
}
// To Convert Eoch to ISO String
public static string ConvertEpochToIsoString(long epochTimestamp)
{
var dateTime = DateTimeOffset.FromUnixTimeMilliseconds(epochTimestamp * 1000);
return dateTime.UtcDateTime.ToString("o");
}
// To Convert ISO String to Date
public static string ConvertIsoStringToDate(string isoString)
{
DateTime parsedDateTime = DateTime.Parse(isoString);
return parsedDateTime.ToString("d");
}
// To Convert EPOCH to Custom Time String
public static string ConvertEpochToCustomString(long epochTimestamp)
{
var dateTimeUtc = DateTimeOffset.FromUnixTimeMilliseconds(epochTimestamp * 1000);
var dateTimeLocal = dateTimeUtc.ToLocalTime();
string formattedDate = dateTimeLocal.ToString("dd MMMM yyyy hh:mm tt", CultureInfo.InvariantCulture);
return formattedDate;
}
// To Get the Time in Hours and minutes If we give minutes
public static string GetFormattedTime(int totalMinutes)
{
if (totalMinutes < 60)
{
return $"{totalMinutes} Minutes";
}
int hours = totalMinutes / 60;
int minutes = totalMinutes % 60;
if (minutes == 0)
{
return $"{hours} Hours";
}
return $"{hours} Hours {minutes} Minutes";
}
// To get the Device name
public static string GetDeviceName()
{
try
{
string hostName = Dns.GetHostName();
return hostName;
}
catch (Exception)
{
return "Unknown";
}
}
// To get the OS Type
public static int GetOperatingSystemType()
{
try
{
int osType;
PlatformID platform = Environment.OSVersion.Platform;
switch (platform)
{
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
osType = 0;
break;
case PlatformID.Unix:
osType = 1;
break;
case PlatformID.MacOSX:
osType = 2;
break;
default:
osType = 3;
break;
}
return osType;
}
catch (Exception)
{
return 3;
}
}
}
}