Attached you will find source code of DateTimeConverter.cs.

Regards,
Michael


On 7/10/06, Matthew Raymer <[EMAIL PROTECTED]> wrote:
> Michael,
>
> No problem.  When I get around to it, I'll write a DateTimeConverter.
>
>
> Matthew
>
> On 7/10/06, Michael Schwarz < [EMAIL PROTECTED]> wrote:
> >
> > Yes, it will still be the local time zone. I have done a poll which
> > model is better, and 78% selected the current implementation. But you
> > can simply change this by implementing your own DateTimeConverter.
> > Maybe I will add a configuration for web.config to allow the second
> > way.
> >
> > Regards,
> > Michael
> >
> >
> > On 7/10/06, Matthew Raymer <[EMAIL PROTECTED]> wrote:
> > > Michael,
> > >
> > > Does DateTime still automatically change the time zone from client to
> > > server?
> > >
> > > You might recall that you and talked about this a few months ago.
> > >
> > > In one of my projects, we ended up passing our data information as a
> string
> > > because the conversion was destroying our data (and causing huge amounts
> of
> > > confusion because the conversion was occurring transparently.)
> > >
> > > Matthew
> > >
> > >
> > > On 7/10/06, Michael Schwarz <[EMAIL PROTECTED] > wrote:
> > > >
> > >
> > > Hi,
> > >
> > > as there was a little discussion about serialization of .NET types and
> > > deserialization of these genereted JSON strings I have updated
> > > Ajax.NET Professional (version 6.7.9.1 ) to allow parsing of new Date
> > > statements, too. You can use the JSON generated string to do a
> > > deserialization right after, now.
> > >
> > > I tried to write some other tests to see how these three frameworks
> > > compare when running serialize() and deserialze() methods. JSON.NET
> > > was not able to serialize a simple DateTime instance. When using a
> > > DateTime as property in a class it could be serialized, but
> > > deserializing didn't work because of the wrong date format (which is
> > > different here in Germany and a  lot of other countries). Atlas is
> > > working similar to the AjaxPro serialization, but not using UTC.
> > >
> > > I started to add more properties/fields to the Person class example to
> > > see which .NET data types are really working?
> > >
> > > [AjaxPro.AjaxNoTypeUsage]
> > > public class Person
> > > {
> > >   public MyColor color = MyColor.Yellow;
> > >   public MyColor2 color2 = MyColor2.Red;  // not working with Atlas
> > >   public DateTime dt = DateTime.Now;
> > >   public float f = 2.3F;
> > >   public double d = 1.1;
> > >   public int i = 999;
> > >   public int[] I = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
> > >   public bool b = false;
> > >   public char c = '@';
> > >   public List<string> s = new List<string>();
> > >   public decimal dec = 0.1M;
> > >   public string S = "\r\n\t\f\b?{\\r\\n\"\'";
> > >
> > >   public Person()
> > >   {
> > >     s.Add("Hello World");
> > >     s.Add("öäüÖÄÜ\\'{new Date(12345);[EMAIL PROTECTED]");
> > >     s.Add(" ");
> > >   }
> > > }
> > >
> > > public enum MyColor
> > > {
> > >   Black, Red, Yellow, White
> > > }
> > >
> > > public enum MyColor2 : byte
> > > {
> > >   Black, Red, Yellow, White
> > > }
> > >
> > > The class above is only working with AjaxPro. JSON.NET and Atlas have
> > > both problems with non-US formats like dates or the decimal seperator.
> > > Compare following JSON outputs:
> > >
> > > Ajax.NET Professional:
> > >
> > > {"color":2,"color2":1,"dt":new Date( Date.UTC(2006,6,9,12,18,22,704)),
> > >
> "f":2.3,"d":1.1,"i":999,"I":[1,2,3,4,5,6,7,8,9],"b":false,"c":"@",
> > > "s":["Hello World","öäüÖÄÜ\\'{new Date(12345);[EMAIL PROTECTED]","
> > >         "],
> > > "dec":0.1,"S":"\r\n\t\f\b?{\\r\\n\"'"}
> > >
> > > JSON.NET:
> > >
> > > {"color":2,"color2":1,"dt":"07/09/2006 14:18:22",
> > >
> "f":2.3,"d":1.1,"i":999,"I":[1,2,3,4,5,6,7,8,9],"b":false,"c":64,
> > > "s":["Hello World","öäüÖÄÜ\\'{new Date(12345);[EMAIL PROTECTED]","
> > >         "],
> > > "dec":"0.1","S":"\r\n\?{\\r\\n\"'"}
> > >
> > > Atlas:
> > >
> > > {"color":2, MISSING color2!! "dt":new Date(1152454818040),
> > > "f":2.3,"d":
> > > 1.1,"i":999,"I":[1,2,3,4,5,6,7,8,9],"b":false,"c":"@",
> > > "s":["Hello World","öäüÖÄÜ\\\'{new Date(12345);[EMAIL PROTECTED]","
> > >         "],
> > > "dec":0.1,"S":"\r\n\?{\\\r\\n\"\'"}
> > >
> > > If you have a deep look in the JSON output you will see a lot of
> > > different serializations. There are some that are ok if the
> > > deserialization is only used for .NET, but using the JSON output in
> > > JavaScript it will very bad if you do not get a real Date object or if
> > > a character is represented as the ASCII code (see JSON.NET that will
> > > serialize the @ sign to 64): The color2 property is using a byte enum
> > > which is not supported by Atlas, I simple removed this for the test
> > > above.
> > >
> > > Next, I tried to use the JSON output to create a new instance of the
> > > Person class. Hm, again only Ajax.NET Professional is working correct.
> > > Atlas cannot convert any value with a decimal seperator if it is using
> > > non-US culture info.
> > >
> > >
> > > --
> > > Best regards | Schöne Grüße
> > > Michael
> > >
> > > Microsoft MVP - Most Valuable Professional
> > > Microsoft MCAD - Certified Application Developer
> > >
> > > http://weblogs.asp.net/mschwarz/
> > > http://www.schwarz-interactive.de/
> > > mailto: [EMAIL PROTECTED]
> > >
> > >
> > > NRN: No Reply Necessary
> > > RYN: Reply with 'Yes' or 'No'
> > > AYQ: Answering Your question
> > > ATC: Attachement is important
> > > 1QM: One Question Message
> > > MQM: Multiple Question Message
> > > FYA: For your Archive
> > > FYI: For your information (NRN)
> > > WFR: Waiting for your Reply/Advice/Permission
> > > AET: Answer Expected Today/this Week/within a Month
> > > RAF: Read and Forward (jokes, quotes, interesting)
> > > ----------------------------------------------
> > > More information: http://opensubject.pbwiki.com/
> > > >
> > >
> > >
> > >
> > >
> > >
> >
> >
> > --
> > Best regards | Schöne Grüße
> > Michael
> >
> > Microsoft MVP - Most Valuable Professional
> > Microsoft MCAD - Certified Application Developer
> >
> > http://weblogs.asp.net/mschwarz/
> > http://www.schwarz-interactive.de/
> > mailto:[EMAIL PROTECTED]
> >
> >
> >
> >
>
>
>
> --
> OpenSubject: for efficient mail
> ----------------------------------------------
>
> NRN: No Reply Necessary
> RYN: Reply with 'Yes' or 'No'
> AYQ: Answering Your question
> ATC: Attachement is important
> 1QM: One Question Message
> MQM: Multiple Question Message
> FYA: For your Archive
> FYI: For your information (NRN)
> WFR: Waiting for your Reply/Advice/Permission
> AET: Answer Expected Today/this Week/within a Month
> RAF: Read and Forward (jokes, quotes, interesting)
> ----------------------------------------------
> More information: http://opensubject.pbwiki.com/
> >
>
>
>
>
>


-- 
Best regards | Schöne Grüße
Michael

Microsoft MVP - Most Valuable Professional
Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz/
http://www.schwarz-interactive.de/
mailto:[EMAIL PROTECTED]


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Ajax.NET Professional" group.

To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]

For more options, visit this group at http://groups.google.com/group/ajaxpro

The latest downloads of Ajax.NET Professional can be found at 
http://www.ajaxpro.info
-~----------~----~----~----~------~----~------~--~---
/*
 * MS   06-04-25        removed unnecessarily used cast
 * MS   06-05-23        using local variables instead of "new Type()" for get 
De-/SerializableTypes
 * MS   06-07-09    added new Date and new Date(Date.UTC parsing
 * 
 */
using System;
using System.Text.RegularExpressions;

namespace AjaxPro
{
        /// <summary>
        /// Provides methods to serialize and deserialize a DateTime object.
        /// </summary>
        public class DateTimeConverter : IJavaScriptConverter
        {
                public DateTimeConverter() : base()
                {
                        m_serializableTypes = new Type[] { typeof(DateTime) };
                        m_deserializableTypes = new Type[] { typeof(DateTime) };
                }

                public override object Deserialize(IJavaScriptObject o, Type t)
                {
            JavaScriptObject ht = o as JavaScriptObject;

            if (o is JavaScriptSource)
            {
                // new Date(Date.UTC(2006,6,9,5,36,18,875))

                string s = o.ToString();

                if (s.StartsWith("new Date(Date.UTC(") && s.EndsWith("))"))
                {
                    s = s.Substring(18, s.Length - 20);

                    // add more checks 0...2000, 0..11, 1..31, 0..23, 0..59, 
0..59, 0..999
                    Regex r = new 
Regex(@"(\d{4}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,2}),(\d{1,3})");
                    Match m = r.Match(s);

                    if (m.Groups.Count != 8)
                        throw new NotSupportedException();

                    int Year = int.Parse(m.Groups[1].Value);
                    int Month = int.Parse(m.Groups[2].Value) + 1;
                    int Day = int.Parse(m.Groups[3].Value);
                    int Hour = int.Parse(m.Groups[4].Value);
                    int Minute = int.Parse(m.Groups[5].Value);
                    int Second = int.Parse(m.Groups[6].Value);
                    int Millisecond = int.Parse(m.Groups[7].Value);

                    DateTime d = new DateTime(Year, Month, Day, Hour, Minute, 
Second, Millisecond);
                    return 
d.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d).TotalMinutes);
                }
                else if (s.StartsWith("new Date(") && s.EndsWith(")"))
                {
                    long nanosecs = long.Parse(s.Substring(9, s.Length - 10)) * 
10000;
#if(NET20)
                    nanosecs += new DateTime(1970, 1, 1, 0, 0, 0, 0, 
DateTimeKind.Utc).Ticks;
                    DateTime d1 = new DateTime(nanosecs, DateTimeKind.Utc);
#else
                    nanosecs += new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks;
                    DateTime d1 = new DateTime(nanosecs);
#endif
                    
                    return 
d1.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d1).TotalMinutes);
                }
            }

                        if(ht == null)
                                throw new NotSupportedException();

                        int Year2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Year"], typeof(int));
                        int Month2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Month"], typeof(int));
                        int Day2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Day"], typeof(int));
                        int Hour2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Hour"], typeof(int));
                        int Minute2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Minute"], typeof(int));
                        int Second2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Second"], typeof(int));
                        int Millisecond2 = 
(int)JavaScriptDeserializer.Deserialize(ht["Millisecond"], typeof(int));

                        DateTime d2 = new DateTime(Year2, Month2, Day2, Hour2, 
Minute2, Second2, Millisecond2);
                        return 
d2.AddMinutes(TimeZone.CurrentTimeZone.GetUtcOffset(d2).TotalMinutes);
                }

                public override string Serialize(object o)
                {
                        if(!(o is DateTime))
                                throw new NotSupportedException();
                        
                        DateTime dt = Convert.ToDateTime(o);
                        dt = dt.ToUniversalTime();

                        return String.Format("new 
Date(Date.UTC({0},{1},{2},{3},{4},{5},{6}))",
                                dt.Year,
                                dt.Month -1,
                                dt.Day,
                                dt.Hour,
                                dt.Minute,
                                dt.Second,
                                dt.Millisecond);
                }
        }
}

Reply via email to