forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetDateCommand.cs
More file actions
541 lines (454 loc) · 15.3 KB
/
Copy pathGetDateCommand.cs
File metadata and controls
541 lines (454 loc) · 15.3 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Globalization;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Internal;
namespace Microsoft.PowerShell.Commands
{
#region get-date
/// <summary>
/// implementation for the get-date command
/// </summary>
[Cmdlet(VerbsCommon.Get, "Date", DefaultParameterSetName = "net", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113313")]
[OutputType(typeof(string), ParameterSetName = new string[] { "UFormat", "net" })]
[OutputType(typeof(DateTime), ParameterSetName = new string[] { "net" })]
public sealed class GetDateCommand : Cmdlet
{
#region parameters
/// <summary>
/// Allows user to override the date/time object that will be processed
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Alias("LastWriteTime")]
public DateTime Date
{
get
{
return _date;
}
set
{
_date = value;
_dateSpecified = true;
}
}
private DateTime _date;
private bool _dateSpecified;
/// <summary>
/// Allows the user to override the year
/// </summary>
[Parameter]
[ValidateRangeAttribute(1, 9999)]
public int Year
{
get
{
return _year;
}
set
{
_year = value;
_yearSpecified = true;
}
}
private int _year;
private bool _yearSpecified;
/// <summary>
/// Allows the user to override the month
/// </summary>
[Parameter]
[ValidateRangeAttribute(1, 12)]
public int Month
{
get
{
return _month;
}
set
{
_month = value;
_monthSpecified = true;
}
}
private int _month;
private bool _monthSpecified;
/// <summary>
/// Allows the user to override the day
/// </summary>
[Parameter]
[ValidateRangeAttribute(1, 31)]
public int Day
{
get
{
return _day;
}
set
{
_day = value;
_daySpecified = true;
}
}
private int _day;
private bool _daySpecified;
/// <summary>
/// Allows the user to override the hour
/// </summary>
[Parameter]
[ValidateRangeAttribute(0, 23)]
public int Hour
{
get
{
return _hour;
}
set
{
_hour = value;
_hourSpecified = true;
}
}
private int _hour;
private bool _hourSpecified;
/// <summary>
/// Allows the user to override the minute
/// </summary>
[Parameter]
[ValidateRangeAttribute(0, 59)]
public int Minute
{
get
{
return _minute;
}
set
{
_minute = value;
_minuteSpecified = true;
}
}
private int _minute;
private bool _minuteSpecified;
/// <summary>
/// Allows the user to override the second
/// </summary>
[Parameter]
[ValidateRangeAttribute(0, 59)]
public int Second
{
get
{
return _second;
}
set
{
_second = value;
_secondSpecified = true;
}
}
private int _second;
private bool _secondSpecified;
/// <summary>
/// Allows the user to override the millisecond
/// </summary>
[Parameter]
[ValidateRangeAttribute(0, 999)]
public int Millisecond
{
get
{
return _millisecond;
}
set
{
_millisecond = value;
_millisecondSpecified = true;
}
}
private int _millisecond;
private bool _millisecondSpecified;
/// <summary>
/// This option determines the default output format used to display the object get-date emits
/// </summary>
[Parameter]
public DisplayHintType DisplayHint { get; set; } = DisplayHintType.DateTime;
/// <summary>
/// Unix format string
/// </summary>
[Parameter(ParameterSetName = "UFormat")]
public string UFormat { get; set; }
/// <summary>
/// Unix format string
/// </summary>
[Parameter(ParameterSetName = "net")]
public string Format { get; set; }
#endregion
#region methods
/// <summary>
/// get the time
/// </summary>
protected override void ProcessRecord()
{
DateTime dateToUse = DateTime.Now;
int offset;
// use passed date object if specified
if (_dateSpecified)
{
dateToUse = Date;
}
//use passed year if specified
if (_yearSpecified)
{
offset = Year - dateToUse.Year;
dateToUse = dateToUse.AddYears(offset);
}
//use passed month if specified
if (_monthSpecified)
{
offset = Month - dateToUse.Month;
dateToUse = dateToUse.AddMonths(offset);
}
//use passed day if specified
if (_daySpecified)
{
offset = Day - dateToUse.Day;
dateToUse = dateToUse.AddDays(offset);
}
//use passed hour if specified
if (_hourSpecified)
{
offset = Hour - dateToUse.Hour;
dateToUse = dateToUse.AddHours(offset);
}
//use passed minute if specified
if (_minuteSpecified)
{
offset = Minute - dateToUse.Minute;
dateToUse = dateToUse.AddMinutes(offset);
}
//use passed second if specified
if (_secondSpecified)
{
offset = Second - dateToUse.Second;
dateToUse = dateToUse.AddSeconds(offset);
}
//use passed millisecond if specified
if (_millisecondSpecified)
{
offset = Millisecond - dateToUse.Millisecond;
dateToUse = dateToUse.AddMilliseconds(offset);
dateToUse = dateToUse.Subtract(TimeSpan.FromTicks(dateToUse.Ticks % 10000));
}
if (UFormat != null)
{
//format according to UFormat string
WriteObject(UFormatDateString(dateToUse));
}
else if (Format != null)
{
//format according to Format string
// Special case built-in primitives: FileDate, FileDateTime.
// These are the ISO 8601 "basic" formats, dropping dashes and colons
// so that they can be used in file names
if (String.Equals("FileDate", Format, StringComparison.OrdinalIgnoreCase))
{
Format = "yyyyMMdd";
}
else if (String.Equals("FileDateUniversal", Format, StringComparison.OrdinalIgnoreCase))
{
dateToUse = dateToUse.ToUniversalTime();
Format = "yyyyMMddZ";
}
else if (String.Equals("FileDateTime", Format, StringComparison.OrdinalIgnoreCase))
{
Format = "yyyyMMddTHHmmssffff";
}
else if (String.Equals("FileDateTimeUniversal", Format, StringComparison.OrdinalIgnoreCase))
{
dateToUse = dateToUse.ToUniversalTime();
Format = "yyyyMMddTHHmmssffffZ";
}
WriteObject(dateToUse.ToString(Format, CultureInfo.CurrentCulture));
}
else
{
//output DateTime object wrapped in an PSObject with DisplayHint attached
PSObject outputObj = new PSObject(dateToUse);
PSNoteProperty note = new PSNoteProperty("DisplayHint", DisplayHint);
outputObj.Properties.Add(note);
WriteObject(outputObj);
}
} // EndProcessing
/// <summary>
/// This is more an implementation of the UNIX strftime
/// </summary>
private string UFormatDateString(DateTime dateTime)
{
DateTime epoch = DateTime.Parse("January 1, 1970", System.Globalization.CultureInfo.InvariantCulture);
int offset = 0;
StringBuilder sb = new StringBuilder();
// folks may include the "+" as part of the format string
if (UFormat[0] == '+')
{
offset++;
}
for (int i = offset; i < UFormat.Length; i++)
{
if (UFormat[i] == '%')
{
i++;
switch (UFormat[i])
{
case 'A':
sb.Append("{0:dddd}");
break;
case 'a':
sb.Append("{0:ddd}");
break;
case 'B':
sb.Append("{0:MMMM}");
break;
case 'b':
sb.Append("{0:MMM}");
break;
case 'h':
sb.Append("{0:MMM}");
break;
case 'C':
sb.Append(dateTime.Year / 100);
break;
case 'c':
sb.Append("{0:ddd} {0:MMM} ");
sb.Append(StringUtil.Format("{0,2} ", dateTime.Day));
sb.Append("{0:HH}:{0:mm}:{0:ss} {0:yyyy}");
break;
case 'D':
sb.Append("{0:MM/dd/yy}");
break;
case 'd':
sb.Append("{0:dd}");
break;
case 'e':
sb.Append(StringUtil.Format("{0,2}", dateTime.Day));
break;
case 'H':
sb.Append("{0:HH}");
break;
case 'I':
sb.Append("{0:hh}");
break;
case 'j':
sb.Append(dateTime.DayOfYear);
break;
case 'k':
sb.Append("{0:HH}");
break;
case 'l':
sb.Append("{0:hh}");
break;
case 'M':
sb.Append("{0:mm}");
break;
case 'm':
sb.Append("{0:MM}");
break;
case 'n':
sb.Append("\n");
break;
case 'p':
sb.Append("{0:tt}");
break;
case 'R':
sb.Append("{0:HH:mm}");
break;
case 'r':
sb.Append("{0:hh:mm:ss tt}");
break;
case 'S':
sb.Append("{0:ss}");
break;
case 's':
sb.Append(dateTime.Subtract(epoch).TotalSeconds);
break;
case 'T':
sb.Append("{0:HH:mm:ss}");
break;
case 'X':
sb.Append("{0:HH:mm:ss}");
break;
case 't':
sb.Append("\t");
break;
case 'u':
sb.Append((int)dateTime.DayOfWeek);
break;
case 'U':
sb.Append(dateTime.DayOfYear / 7);
break;
case 'V':
sb.Append((dateTime.DayOfYear / 7) + 1);
break;
case 'G':
sb.Append("{0:yyyy}");
break;
case 'g':
sb.Append("{0:yy}");
break;
case 'W':
sb.Append(dateTime.DayOfYear / 7);
break;
case 'w':
sb.Append((int)dateTime.DayOfWeek);
break;
case 'x':
sb.Append("{0:MM/dd/yy}");
break;
case 'y':
sb.Append("{0:yy}");
break;
case 'Y':
sb.Append("{0:yyyy}");
break;
case 'Z':
sb.Append("{0:zz}");
break;
default:
sb.Append(UFormat[i]);
break;
}
}
else
{
// It's not a known format specifier, so just append it
sb.Append(UFormat[i]);
}
}
return StringUtil.Format(sb.ToString(), dateTime);
} // UFormatDateString
#endregion
} // GetDateCommand
#endregion
#region DisplayHintType enum
/// <summary>
/// Display Hint type
/// </summary>
public enum DisplayHintType
{
/// <summary>
/// Display preference Date-Only
/// </summary>
Date,
/// <summary>
/// Display preference Time-Only
/// </summary>
Time,
/// <summary>
/// Display preference Date and Time
/// </summary>
DateTime
}
#endregion
} // namespace Microsoft.PowerShell.Commands