Skip to content

DateTimeOffset should be treated as a primitive for serialization #3172

Closed

Description

@Jaykul

Among the many new types in .NET added to work around problems in the legacy types there is a new System.DateTimeOffset, which is just a System.DateTime plus an Offset (i.e. the original timezone) -- I won't explain why this is useful, but MSDN does.

This class should just work in PowerShell, just like the original DateTime.

Currently it doesn't, because when you pass it over a remote connection it comes out as Deserialized.DateTimeOffset and the scripter is forced to manually re-create a DateTimeOffset object.

Steps to reproduce

This works great:

Invoke-Command { 
    param([DateTimeOffset]$Time) 
    $Time.UtcDateTime, $Time.LocalDateTime, $Time.DateTime
} -Args ([DateTimeOffset]::Now)

But the same command, with a -ComputerName, fails, because of remote deserializing...

Invoke-Command -ComputerName localhost { 
    param([DateTimeOffset]$Time) 
    $Time.UtcDateTime, $Time.LocalDateTime, $Time.DateTime
} -Args ([DateTimeOffset]::Now)

Expected behavior

Passing DateTimeOffset should just work even over remote connections.

Actual behavior

Cannot pass System.DateTimeOffset to remote instances. Must re-construct it from Deserialized.System.DateTimeOffset

Activity

  1. iSazonov commented on Feb 22, 2017

    @iSazonov
    Collaborator

    Serialization of primitive types is based on XmlConvert class but CoreCLR is still not support ToDateTimeOffset

    If the Issue will be approved, then we should come back to it after we move on .Net Standart 2.0.

  2. Jaykul commented on Feb 27, 2017

    @Jaykul
    ContributorAuthor

    What do you mean Ilya (@iSazonov), it looks like it's already there, even in the version you shipped in 6.0.0.10?

    https://github.com/dotnet/corefx/blob/f0ccd9742065ee2aa296448fe3dd38ef6ff77af9/src/System.Private.Xml/src/System/Xml/XmlConvert.cs#L863

    PS>[XmlConvert] | gm -sta | where Name -match DateTime
    
       TypeName: System.Xml.XmlConvert
    
    Name             MemberType Definition
    ----             ---------- ----------
    ToDateTime       Method     static datetime ToDateTime(string s, System.Xml.XmlDateTimeSerializationMode dateTimeOption)
    ToDateTimeOffset Method     static System.DateTimeOffset ToDateTimeOffset(string s), static System.DateTimeOffset ToDateTimeOffset(string s, string format), static System.DateTi...
    
    PS>[XmlConvert].Assembly
    
    GAC    Version        Location
    ---    -------        --------
           v4.0.30319     C:\Program Files\PowerShell\6.0.0.10\System.Xml.ReaderWriter.dll
    
    PS>[XmlConvert]::ToString([DateTimeOffset]::Now)
    2017-02-26T23:58:52.0082884-05:00
    
    PS>[XmlConvert]::ToDateTimeOffset( [XmlConvert]::ToString([DateTimeOffset]::Now) )
    
    DateTime      : 2/26/2017 11:59:36 PM
    UtcDateTime   : 2/27/2017 4:59:36 AM
    LocalDateTime : 2/26/2017 11:59:36 PM
    Date          : 2/26/2017 12:00:00 AM
    Day           : 26
    DayOfWeek     : Sunday
    DayOfYear     : 57
    Hour          : 23
    Millisecond   : 639
    Minute        : 59
    Month         : 2
    Offset        : -05:00:00
    Second        : 36
    Ticks         : 636237503766399149
    UtcTicks      : 636237683766399149
    TimeOfDay     : 23:59:36.6399149
    Year          : 2017
    
  3. iSazonov commented on Feb 27, 2017

    @iSazonov
    Collaborator

    Joel Bennett (@Jaykul) Good catch! Thanks!
    (I figured that we use an older version. Strange that I couldn't make the fix in code. Most likely I should did dotnet restore)

  4. BrucePay commented on Mar 3, 2017

    @BrucePay
    Collaborator

    PowerShell remoting uses it's own serialization mechanism as described in protocol specification MS-PSRP, section 2.2.5. By design, a limited, fixed set of types deserialize with fidelity. This list is available in MS-PSRP section 2.2.5.1 (see https://msdn.microsoft.com/en-us/library/dd305931.aspx )
    Adding support for new primitive types requires changes to both the code/protocol and protocol specifications. The serialization code is in
    src\System.Management.Automation\engine\Serialization.cs
    src\System.Management.Automation\engine\SerializationStrings.cs
    (Note: you can use the types files and Update-TypeData to specify custom deserializations in the local runspace but these need to be set up in each session.)
    For PowerShell 6.0, it seems reasonable that we expand the set of types we serialize with fidelity but we should do this deliberately. I suggest opening an RFC for this so we can get a canonical list of the new types we want to handle.

  5. added
    WG-Remoting
    Issue-Enhancement
    on Mar 3, 2017
  6. iSazonov commented on Oct 8, 2017

    @iSazonov
    Collaborator

    Can we implement a generic algorithm based on a schema to being more flexible?

  7. added
    Needs-Triage
    on Jul 16, 2021
  8. Jaykul commented on Feb 2, 2022

    @Jaykul
    ContributorAuthor

    I don't understand why this is taking years. How hard is this?

    Can't you just write a converter like this DateTimeOffsetConverter and register it for DateTimeOffset?

  9. PaulHigin commented on Apr 7, 2022

    @PaulHigin
    Contributor

    @WG-Remoting
    Remoting working group: Adding new remoting types requires updating the remoting protocol version and associated documents, which is a very large work item. It is unlikely that we would do this just for a single new type, but this could be part of a planned protocol upgrade that encompasses many enhancements. There is currently no plan to do this, but in the meantime, types can be deserialized into concrete types using PowerShell's extended type system over remoting.

  10. removed
    Needs-Triage
    on Apr 7, 2022
  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. added
    Resolution-No Activity
    on Nov 16, 2023
  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  16. wbuck commented on Feb 13, 2024

    @wbuck

    How can I work around this issue? When call a powershell script via a C# application I'm getting an exception regarding mismatched types: System.DateTimeOffset vs Deserialized.DateTimeOffset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-No ActivityWG-Remoting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions