Menu

Patch to support DateTime in serializer

Developers
2008-12-10
2013-01-04
  • Geert De Peuter

    Geert De Peuter - 2008-12-10

    I hope this diff will be usable.
    This is a unified diff against AMF 1.9 beta 2.
    After applying this patch, you will be able to send php native DateTime objects back (so they will be serialized properly).

    Hopefully this will be incorporated in the codebase.
    The intval($d->format('u') / 1000) would only be useful for people who have their dates created with microsecond precision.

    -- Geert

    Index: AMFSerializer.php

    --- AMFSerializer.php    (revision 451)
    +++ AMFSerializer.php    (working copy)
    @@ -715,6 +715,13 @@
                     $this->writeAmf3Array($d, true);
                     return;
                 }
    +                elseif(is_a($d, 'DateTime'))
    +            {
    +                #$this->outBuffer .= "\6";
    +                #$this->writeAmf3String("Got a DateTime");
    +                $this->writeAmf3DateTime($d);
    +                return;
    +            }
                 else
                 {
                     $this->writeAmf3Object($d);
    @@ -906,39 +913,15 @@
             //Now we close the open object
             $this->outBuffer .= "\1";
         }
    -
    -    /*
    -    public void WriteAMF3DateTime(DateTime value)
    -    {
    -        if( !_objectReferences.Contains(value) )
    -        {
    -            _objectReferences.Add(value, _objectReferences.Count);
    -            int handle = 1;
    -            WriteAMF3IntegerData(handle);
    -
    -            // Write date (milliseconds from 1970).
    -            DateTime timeStart = new DateTime(1970, 1, 1, 0, 0, 0);
    -
    -            string timezoneCompensation = System.Configuration.ConfigurationSettings.AppSettings["timezoneCompensation"];
    -            if( timezoneCompensation != null && ( timezoneCompensation.ToLower() == "auto" ) )
    -            {
    -                value = value.ToUniversalTime();
    -            }
    -           
    -            TimeSpan span = value.Subtract(timeStart);
    -            long milliSeconds = (long)span.TotalMilliseconds;
    -            long date = BitConverter.DoubleToInt64Bits((double)milliSeconds);
    -            this.WriteLong(date);
    -        }
    -        else
    -        {
    -            int handle = (int)_objectReferences[value];
    -            handle = handle << 1;
    -            WriteAMF3IntegerData(handle);
    -        }
    -    }
    -    */
        
    +    function writeAmf3DateTime(DateTime $d)
    +    {
    +        $this->writeByte(0x08);
    +        $this->writeAmf3Int(1);
    +        $this->writeDouble($d->format('U') * 1000 + intval($d->format('u')/1000));
    +        return;
    +    }
    +    
         function getAmf3Int($d)
         {
             $d &= 0x1fffffff;

     
    • J B

      J B - 2009-07-23

      Geert, I cant thank you enough for this! Really helped me a lot.

       
  • Geert De Peuter

    Geert De Peuter - 2010-03-04

    Any chance this is going to be picked up for a later release ?

     
  • ariels

    ariels - 2010-03-18

    Hi,
    I'll look into adding it.
    Ariel

     

Log in to post a comment.

MongoDB Logo MongoDB