File size: 18,134 Bytes
8c763fb | 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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Text;
namespace Microsoft.PowerShell.Commands
{
#region get-date
/// <summary>
/// Implementation for the get-date command.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Date", DefaultParameterSetName = DateAndFormatParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096615")]
[OutputType(typeof(string))]
[OutputType(typeof(DateTime), ParameterSetName = new[] { DateAndFormatParameterSet, UnixTimeSecondsAndFormatParameterSet })]
public sealed class GetDateCommand : Cmdlet
{
#region parameters
/// <summary>
/// Allows user to override the date/time object that will be processed.
/// </summary>
[Parameter(ParameterSetName = DateAndFormatParameterSet, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = DateAndUFormatParameterSet, 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;
// The const comes from DateTimeOffset.MinValue.ToUnixTimeSeconds()
private const long MinimumUnixTimeSecond = -62135596800;
// The const comes from DateTimeOffset.MaxValue.ToUnixTimeSeconds()
private const long MaximumUnixTimeSecond = 253402300799;
/// <summary>
/// Gets or sets whether to treat a numeric input as ticks, or unix time.
/// </summary>
[Parameter(ParameterSetName = UnixTimeSecondsAndFormatParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = UnixTimeSecondsAndUFormatParameterSet, Mandatory = true)]
[ValidateRange(MinimumUnixTimeSecond, MaximumUnixTimeSecond)]
[Alias("UnixTime")]
public long UnixTimeSeconds
{
get
{
return _unixTimeSeconds;
}
set
{
_unixTimeSeconds = value;
_unixTimeSecondsSpecified = true;
}
}
private long _unixTimeSeconds;
private bool _unixTimeSecondsSpecified;
/// <summary>
/// Allows the user to override the year.
/// </summary>
[Parameter]
[ValidateRange(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]
[ValidateRange(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]
[ValidateRange(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]
[ValidateRange(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]
[ValidateRange(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]
[ValidateRange(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]
[ValidateRange(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 = DateAndUFormatParameterSet, Mandatory = true)]
[Parameter(ParameterSetName = UnixTimeSecondsAndUFormatParameterSet, Mandatory = true)]
public string UFormat { get; set; }
/// <summary>
/// DotNet format string.
/// </summary>
[Parameter(ParameterSetName = DateAndFormatParameterSet)]
[Parameter(ParameterSetName = UnixTimeSecondsAndFormatParameterSet)]
[ArgumentCompletions("FileDate", "FileDateUniversal", "FileDateTime", "FileDateTimeUniversal")]
public string Format { get; set; }
/// <summary>
/// Gets or sets a value that converts date to UTC before formatting.
/// </summary>
[Parameter]
public SwitchParameter AsUTC { 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;
}
else if (_unixTimeSecondsSpecified)
{
dateToUse = DateTimeOffset.FromUnixTimeSeconds(UnixTimeSeconds).LocalDateTime;
}
// 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 (AsUTC)
{
dateToUse = dateToUse.ToUniversalTime();
}
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(dateToUse);
PSNoteProperty note = new("DisplayHint", DisplayHint);
outputObj.Properties.Add(note);
WriteObject(outputObj);
}
}
/// <summary>
/// This is more an implementation of the UNIX strftime.
/// </summary>
private string UFormatDateString(DateTime dateTime)
{
int offset = 0;
StringBuilder sb = new();
// 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 'C':
sb.Append(dateTime.Year / 100);
break;
case 'c':
sb.Append("{0:ddd} {0:dd} {0:MMM} {0:yyyy} {0:HH}:{0:mm}:{0:ss}");
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 'F':
sb.Append("{0:yyyy}-{0:MM}-{0:dd}");
break;
case 'G':
sb.Append(StringUtil.Format("{0:0000}", ISOWeek.GetYear(dateTime)));
break;
case 'g':
int isoYearWithoutCentury = ISOWeek.GetYear(dateTime) % 100;
sb.Append(StringUtil.Format("{0:00}", isoYearWithoutCentury));
break;
case 'H':
sb.Append("{0:HH}");
break;
case 'h':
sb.Append("{0:MMM}");
break;
case 'I':
sb.Append("{0:hh}");
break;
case 'j':
sb.Append(StringUtil.Format("{0:000}", dateTime.DayOfYear));
break;
case 'k':
sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour));
break;
case 'l':
sb.Append("{0,2:%h}");
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(StringUtil.Format("{0:0}", dateTime.ToUniversalTime().Subtract(DateTime.UnixEpoch).TotalSeconds));
break;
case 'T':
sb.Append("{0:HH:mm:ss}");
break;
case 't':
sb.Append('\t');
break;
case 'U':
sb.Append(dateTime.DayOfYear / 7);
break;
case 'u':
int dayOfWeek = dateTime.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)dateTime.DayOfWeek;
sb.Append(dayOfWeek);
break;
case 'V':
sb.Append(StringUtil.Format("{0:00}", ISOWeek.GetWeekOfYear(dateTime)));
break;
case 'W':
sb.Append(dateTime.DayOfYear / 7);
break;
case 'w':
sb.Append((int)dateTime.DayOfWeek);
break;
case 'X':
sb.Append("{0:HH:mm:ss}");
break;
case 'x':
sb.Append("{0:MM/dd/yy}");
break;
case 'Y':
sb.Append("{0:yyyy}");
break;
case 'y':
sb.Append("{0:yy}");
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);
}
#endregion
private const string DateAndFormatParameterSet = "DateAndFormat";
private const string DateAndUFormatParameterSet = "DateAndUFormat";
private const string UnixTimeSecondsAndFormatParameterSet = "UnixTimeSecondsAndFormat";
private const string UnixTimeSecondsAndUFormatParameterSet = "UnixTimeSecondsAndUFormat";
}
#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
}
|