-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeUtils.cs
94 lines (75 loc) · 2.91 KB
/
TimeUtils.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
91
92
93
94
using System;
using System.Collections.Generic;
using System.Text;
namespace VSTSTimeTrackerAddin
{
public class TimeUtils
{
/// <summary>
/// Gets a DateTime from a time string in the format hh:mm:ss. This static member is useful
/// for creating DateTime objects for time counts.
/// </summary>
/// <param name="textTime"></param>
/// <param name="timeCountStart"></param>
/// <returns></returns>
public static DateTime GetStartDateTimeFromText(string textTime, DateTime timeCountStart)
{
string[] timePiecesArr = textTime.Split(":".ToCharArray());
if (timePiecesArr.Length != 3)
return new DateTime(0);
TimeSpan ts = new TimeSpan(int.Parse(timePiecesArr[0]), int.Parse(timePiecesArr[1]), int.Parse(timePiecesArr[2]));
timeCountStart = timeCountStart.Subtract(ts);
return timeCountStart;
}
/// <summary>
/// Gets a TimeSpan from a Decimal time (in hours).
/// </summary>
/// <param name="decTime"></param>
/// <returns></returns>
public static TimeSpan GetTimeSpanFromDecimal(Decimal decTime)
{
int h = Decimal.ToInt32(decTime);
Decimal aux = decTime - h;
int m = Decimal.ToInt32(aux * 60);
aux = aux * 60 - m;
int s = Decimal.ToInt32(aux * 60);
return new TimeSpan(h, m, s);
}
/// <summary>
/// Gets a Decimal time (in hours) from a TimeSpan object.
/// </summary>
/// <param name="tsTime"></param>
/// <returns></returns>
public static Decimal GetDecimalFromTimeSpan(TimeSpan tsTime)
{
return new Decimal(tsTime.TotalHours);
}
/// <summary>
/// Converts a TimeSpan object to a time string in the format hh:mm:ss.
/// </summary>
/// <param name="ts"></param>
/// <returns></returns>
public static string GetTimeStringFromTimeSpan(TimeSpan ts)
{
double aux = ts.TotalHours;
int h = (int)Math.Truncate(aux);
aux = (aux - h)*60;
int m = (int)Math.Truncate(aux);
aux = (aux - m) * 60;
int s = (int)Math.Truncate(aux);
return h.ToString("00")+":"+m.ToString("00")+":"+s.ToString("00");
}
/// <summary>
/// Converts a time string in the format hh:mm:ss to a TimeSpan object.
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static TimeSpan GetTimeSpanFromTimeString(string time)
{
string[] timePiecesArr = time.Split(":".ToCharArray());
if (timePiecesArr.Length != 3)
return TimeSpan.Zero;
return new TimeSpan(int.Parse(timePiecesArr[0]), int.Parse(timePiecesArr[1]), int.Parse(timePiecesArr[2]));
}
}
}