Webservice - How to pass timezone information in a datetime element

I was given wsdl and I need to create a web service following its specifications; I am using visual studio 2010. Among others, there is also a definition of this complex type:

    <xsd:complexType name="Person">
        <xsd:sequence>
            <xsd:element name="surname" type="xsd:string"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="birthDate" nillable="true" type="xsd:dateTime"/>
        </xsd:sequence>
    </xsd:complexType>

Using VS, I got the following cs (I don’t remember how I did it, but I followed the instructions found on the Internet):

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://XXX/Submitter/")]
    public partial class Person {
        private string surnameField;
        private string nameField;
        private System.Nullable<System.DateTime> birthDateField;

        /// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string surname {
            get {
                return this.surnameField;
            }
            set {
                this.surnameField = value;
            }
        }
        /// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
        /// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
        public System.Nullable<System.DateTime> birthDate {
            get {
                return this.birthDateField;
            }
            set {
                this.birthDateField = value;
            }
        }

And everything is correct: it compiles, starts and gives the expected result; the only problem is that the other side that gave me wsdl when calling my web service expects to get the date of birth field as

2013-02-15T17:28:00+01:00

with time zone information while the result is similar to

2015-11-17T18:30:11

no time zone.

, DateTime?, , , ; ?

+4
2

, XSD , .NET - SOAP.

.NET - DateTime DateTimeOffset. , xs:datetime DateTimeOffset, , . xs:datetime DateTime Kind , , .

  • DateTime.Kind DateTimeKind.Unspecified, .

  • DateTime.Kind DateTimeKind.Utc, Z, +00:00.

  • DateTime.Kind - DateTimeKind.Local, , .

, DateTime.SpecifyKind, , .ToUniversalTime() .ToLocalTime(). , , , TimeZoneInfo. - , Unspecified, .

, . DateTimeOffset, SOAP.

, xs:datetime . xs:date, , "2013-02-15". , .NET, Date System.Time corefxlab . , , SOAP .

, - XML SOAP. - JSON REST. .

+2

, Jon Skeet . ​​:

public struct DateTimeWithZone
{
    private readonly DateTime utcDateTime;
    private readonly TimeZoneInfo timeZone;

    public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone)
    {
        utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone); 
        this.timeZone = timeZone;
    }

    public DateTime UniversalTime { get { return utcDateTime; } }

    public TimeZoneInfo TimeZone { get { return timeZone; } }

    public DateTime LocalTime
    { 
        get 
        { 
            return TimeZoneInfo.ConvertTime(utcDateTime, timeZone); 
        }
    }        
}
0

Source: https://habr.com/ru/post/1617494/


All Articles