forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewTimeSpanCommand.cs
More file actions
137 lines (113 loc) · 3.98 KB
/
Copy pathNewTimeSpanCommand.cs
File metadata and controls
137 lines (113 loc) · 3.98 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
using Dbg = System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// implementation for the new-timespan command
/// </summary>
[Cmdlet(VerbsCommon.New, "TimeSpan", DefaultParameterSetName = "Date",
HelpUri = "http://go.microsoft.com/fwlink/?LinkID=113360", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(TimeSpan))]
public sealed class NewTimeSpanCommand : PSCmdlet
{
#region parameters
/// <summary>
/// This parameter indicates the date the time span begins;
/// it is used if two times are being compared
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Date")]
[Alias("LastWriteTime")]
public DateTime Start
{
get
{
return _start;
}
set
{
_start = value;
_startSpecified = true;
}
}
private DateTime _start;
private bool _startSpecified;
/// <summary>
/// This parameter indicates the end of a time span. It is used if two
/// times are being compared. If one of the times is not specified,
/// the current system time is used.
/// </summary>
[Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "Date")]
public DateTime End
{
get
{
return _end;
}
set
{
_end = value;
_endSpecified = true;
}
}
private DateTime _end;
private bool _endSpecified = false;
/// <summary>
/// Allows the user to override the day
/// </summary>
[Parameter(ParameterSetName = "Time")]
public int Days { get; set; } = 0;
/// <summary>
/// Allows the user to override the hour
/// </summary>
[Parameter(ParameterSetName = "Time")]
public int Hours { get; set; } = 0;
/// <summary>
/// Allows the user to override the minute
/// </summary>
[Parameter(ParameterSetName = "Time")]
public int Minutes { get; set; } = 0;
/// <summary>
/// Allows the user to override the second
/// </summary>
[Parameter(ParameterSetName = "Time")]
public int Seconds { get; set; } = 0;
#endregion
#region methods
/// <summary>
/// Calculate and write out the appropriate timespan
/// </summary>
protected override void ProcessRecord()
{
// initally set start and end time to be equal
DateTime startTime = DateTime.Now;
DateTime endTime = startTime;
TimeSpan result;
switch (ParameterSetName)
{
case "Date":
if (_startSpecified)
{
startTime = Start;
}
if (_endSpecified)
{
endTime = End;
}
result = endTime.Subtract(startTime);
break;
case "Time":
result = new TimeSpan(Days, Hours, Minutes, Seconds);
break;
default:
Dbg.Diagnostics.Assert(false, "Only one of the specified parameter sets should be called.");
return;
}
WriteObject(result);
} // EndProcessing
#endregion
} // NewTimeSpanCommand
} // namespace Microsoft.PowerShell.Commands