8000 Special case DateTime/TimeSpan in Format-Custom · Issue #18142 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content
Special case DateTime/TimeSpan in Format-Custom #18142
@powercode

Description

@powercode

Produce one-line output in Format-Custom for DateTime by default

Dates and times get really spammy in Format-Custom

This simple object with a datetime

[pscustomobject]@{Time = [datetime]::now} | format-custom

produces 148 lines of output!


class PSCustomObject
{
  Time = 
    class DateTime
    {
      Date = 
        class DateTime
        {
          Date = 
            class DateTime
            {
              Date = 
                class DateTime
                {
                  Date = 
                    class DateTime
                    {
                      Date = 2022-09-21 00:00:00
                      Day = 21
                      DayOfWeek = Wednesday
                      DayOfYear = 264
                      Hour = 0
                      Kind = Local
                      Millisecond = 0
                      Minute = 0
                      Month = 9
                      Second = 0
                      Ticks = 637993152000000000
                      TimeOfDay = 00:00:00
                      Year = 2022
                      DateTime = den 21 september 2022 00:00:00
                    }
                  Day = 21
                  DayOfWeek = Wednesday
                  DayOfYear = 264
                  Hour = 0
                  Kind = Local
                  Millisecond = 0
                  Minute = 0
                  Month = 9
                  Second = 0
                  Ticks = 637993152000000000
                  TimeOfDay = 
                    class TimeSpan
                    {
                      Ticks = 0
                      Days = 0
                      Hours = 0
                      Milliseconds = 0
                      Minutes = 0
                      Seconds = 0
                      TotalDays = 0
                      TotalHours = 0
                      TotalMilliseconds = 0
                      TotalMinutes = 0
                      TotalSeconds = 0
                    }
                  Year = 2022
                  DateTime = den 21 september 2022 00:00:00
                }
              Day = 21
              DayOfWeek = Wednesday
              DayOfYear = 264
              Hour = 0
              Kind = Local
              Millisecond = 0
              Minute = 0
              Month = 9
              Second = 0
              Ticks = 637993152000000000
              TimeOfDay = 
                class TimeSpan
                {
                  Ticks = 0
                  Days = 0
                  Hours = 0
                  Milliseconds = 0
                  Minutes = 0
                  Seconds = 0
                  TotalDays = 0
                  TotalHours = 0
                  TotalMilliseconds = 0
                  TotalMinutes = 0
                  TotalSeconds = 0
                }
              Year = 2022
              DateTime = den 21 september 2022 00:00:00
            }
          Day = 21
          DayOfWeek = Wednesday
          DayOfYear = 264
          Hour = 0
          Kind = Local
          Millisecond = 0
          Minute = 0
          Month = 9
          Second = 0
          Ticks = 637993152000000000
          TimeOfDay = 
            class TimeSpan
            {
              Ticks = 0
              Days = 0
              Hours = 0
              Milliseconds = 0
              Minutes = 0
              Seconds = 0
              TotalDays = 0
              TotalHours = 0
              TotalMilliseconds = 0
              TotalMinutes = 0
              TotalSeconds = 0
            }
          Year = 2022
          DateTime = den 21 september 2022 00:00:00
        }
      Day = 21
      DayOfWeek = Wednesday
      DayOfYear = 264
      Hour = 9
      Kind = Local
      Millisecond = 945
      Minute = 36
      Month = 9
      Second = 31
      Ticks = 637993497919455174
      TimeOfDay = 
        class TimeSpan
        {
          Ticks = 345919455174
          Days = 0
          Hours = 9
          Milliseconds = 945
          Minutes = 36
          Seconds = 31
          TotalDays = 0,400369739784722
          TotalHours = 9,60887375483333
          TotalMilliseconds = 34591945,5174
          TotalMinutes = 576,53242529
          TotalSeconds = 34591,9455174
        }
      Year = 2022
      DateTime = den 21 september 2022 09:36:31
    }
}

I suggest that we should special-case DateTime in the custom formatted to only print the string same string as we get when we limit the depth:

[pscustomobject]@{Obj = [PSCustomObject]@{Time = [datetime]::now}} | fc -Depth 1
class PSCustomObject
{
  Obj =
    class PSCustomObject
    {
      Time = 2022-09-21 09:38:35
    }
}

So that

[pscustomobject]@{Time = [datetime]::now} | format-custom

would output

class PSCustomObject
{
    Time = 2022-09-21 09:38:35
}

Maybe use -Force to get the full recursive version?

Proposed technical implementation details (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Interactive-Consolethe console experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0