EN
C# / .NET - get current machine time in seconds
10 points
C# / .NET it is possible to take machine time in seconds in following way.
xxxxxxxxxx
1
public static class TimeUtils
2
{
3
public static long GetSeconds()
4
{
5
double timestamp = Stopwatch.GetTimestamp();
6
double seconds = timestamp / Stopwatch.Frequency;
7
8
return (long)seconds;
9
}
10
}
Example:
xxxxxxxxxx
1
long t1 = TimeUtils.GetSeconds();
2
3
Thread.Sleep(1000);
4
5
long t2 = TimeUtils.GetSeconds();
6
long dt = t2 - t1;
7
8
Console.WriteLine(t2 + " - " + t1 + " = " + dt + " s");
Output:
xxxxxxxxxx
1
200206 - 200205 = 1 s
Notes:
Stopwatch.GetTimestamp
returns time from timer mechanism - system up time.- The best application of this time could be measuring how much time took some operations.