DateTimeOffsetAdapter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / DateTimeOffsetAdapter.cs / 1 / DateTimeOffsetAdapter.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Runtime.Serialization
{ 
    using System;
    using System.Reflection; 
    using System.Xml; 
    using System.Globalization;
 
    [DataContract(Name = "DateTimeOffset", Namespace = "http://schemas.datacontract.org/2004/07/System")]
#if USE_REFEMIT
    public struct DateTimeOffsetAdapter
#else 
    internal struct DateTimeOffsetAdapter
#endif 
    { 
        DateTime utcDateTime;
        short offsetMinutes; 

        public DateTimeOffsetAdapter(DateTime dateTime, short offsetMinutes)
        {
            this.utcDateTime = dateTime; 
            this.offsetMinutes = offsetMinutes;
        } 
 
        [DataMember(Name = "DateTime", IsRequired = true)]
        public DateTime UtcDateTime 
        {
            get { return utcDateTime; }
            set { utcDateTime = value; }
        } 

        [DataMember(Name = "OffsetMinutes", IsRequired = true)] 
        public short OffsetMinutes 
        {
            get { return offsetMinutes; } 
            set { offsetMinutes = value; }
        }

        public static DateTimeOffset GetDateTimeOffset(DateTimeOffsetAdapter value) 
        {
            try 
            { 
                switch (value.UtcDateTime.Kind)
                { 
                    case DateTimeKind.Unspecified:
                        return new DateTimeOffset(value.UtcDateTime, new TimeSpan(0, value.OffsetMinutes, 0));

                    //DateTimeKind.Utc and DateTimeKind.Local 
                    //Read in deserialized DateTime portion of the DateTimeOffsetAdapter and convert DateTimeKind to Unspecified.
                    //Apply ofset information read from OffsetMinutes portion of the DateTimeOffsetAdapter. 
                    //Return converted DateTimeoffset object. 
                    default:
                        DateTimeOffset deserialized = new DateTimeOffset(value.UtcDateTime); 
                        return deserialized.ToOffset(new TimeSpan(0, value.OffsetMinutes, 0));
                }
            }
            catch (ArgumentException exception) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(value.ToString(CultureInfo.InvariantCulture), "DateTimeOffset", exception)); 
            } 
        }
 
        public static DateTimeOffsetAdapter GetDateTimeOffsetAdapter(DateTimeOffset value)
        {
            return new DateTimeOffsetAdapter(value.UtcDateTime, (short)value.Offset.TotalMinutes);
        } 

        public string ToString(IFormatProvider provider) 
        { 
            return "DateTime: " + this.UtcDateTime + ", Offset: " + this.OffsetMinutes;
        } 

    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK