Standardizing the date string

Our formatting of dates doesn't make use of the widely-used ISO standard text format. To be more compatible with other languages, we should properly encode the datetime object in a standard string and parse a standard string.

As we're already treating dates as a special case, this seems to be a sensible implementation. It can be done without too much change to our encoding and decoding. Consider this small change to the encoding:

    if isinstance(object, datetime.datetime): 
        fmt= "%Y-%m-%dT%H:%M:%S" 
        return dict( 
            __class__="datetime.datetime.strptime", 
            __args__=[object.strftime(fmt), fmt], 
            __kw__={} 
        ) 

The encoded output names the static method datetime.datetime.strptime() and provides the argument-encoded datetime as well as the format to be used to decode it. The output for a post now looks like the following snippet:

            { 
                "__args__": [], 
                "__class__": "Post_J", 
                "__kw__": { 
                    "title": "Anchor Follies", 
                    "tags": [ 
                        "#RedRanger", 
                        "#Whitby42", 
                        "#Mistakes" 
                    ], 
                    "rst_text": "Some witty epigram.", 
                    "date": { 
                        "__args__": [ 
                            "2013-11-18T15:30:00", 
                            "%Y-%m-%dT%H:%M:%S" 
                        ], 
                        "__class__": "datetime.datetime.strptime", 
                        "__kw__": {} 
                    } 
                } 
            } 

This shows us that we now have an ISO-formatted date instead of individual fields. We've also moved away from the object creation using a class name. The __class__ value is expanded to be a class name or a static method name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset