Best practices for DateTime serialization in .NET 3.5

前端 未结 6 1766
执念已碎
执念已碎 2020-12-01 10:45

Some 4 years back, I followed this MSDN article for DateTime usage best practices for building a .Net client on .Net 1.1 and ASMX web services (with SQL 2000 server as the b

6条回答
  •  温柔的废话
    2020-12-01 11:22

    As long as your web services layer and client layer use the .NET DateTime type, it should serialize and deserialize properly as an SOAP-standard local date/time w/ Timezone information such as:

    2008-09-15T13:14:36.9502109-05:00

    If you absolutely, positively must know the timezone itself (i.e. the above could be Eastern Standard Time or Central Daylight Time), you need to create your own datatype which exposes those pieces as such:

    [Serializable]
    public sealed class MyDateTime
    {
        public MyDateTime()
        {
            this.Now = DateTime.Now;
            this.IsDaylightSavingTime = this.Now.IsDaylightSavingTime();
            this.TimeZone = this.IsDaylightSavingTime
                ? System.TimeZone.CurrentTimeZone.DaylightName
                : System.TimeZone.CurrentTimeZone.StandardName;
        }
    
        public DateTime Now
        {
            get;
    
            set;
        }
    
        public string TimeZone
        {
            get;
    
            set;
        }
    
        public bool IsDaylightSavingTime
        {
            get;
    
            set;
        }
    }
    

    then your response would look like:

    2008-09-15T13:34:08.0039447-05:00
    Central Daylight Time
    true
    

提交回复
热议问题