peteroupc/CBOR-Java

Date Format Tag 0

Closed this issue · 1 comments

Hi, thank you for the great project.

I am currenty developing a server with your library, but I couldn't find a proper conversion for date based on tag 0. As I wanted to fix this, I couldn't build the sources. The test "TestFromJSONString" fails, although this seems not due to the changes I made (I reverted them once completely, but it still failed.). Do I miss something.

Btw. this is my first attempt to fix Tag 0, what do you think?


package com.upokecenter.cbor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

/*
 * Created by SharpDevelop.
 * User: Peter
 * Date: 2/28/2014
 * Time: 11:49 AM
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

/**
 * Description of CBORTag0.
 */
class CBORTag0 implements ICBORTag, ICBORConverter<Date> {

    static void AddConverter() {
        CBORObject.AddConverter(Date.class, new CBORTag0());
    }
    
    
    
    /**
     * Not documented yet.
     *
     * @return A CBORTypeFilter object.
     */
    public CBORTypeFilter GetTypeFilter() {
        return CBORTypeFilter.TextString;
    }

    /**
     * {@inheritDoc}
     *
     * Not documented yet.
     */
    public CBORObject ValidateObject(final CBORObject obj) {
        if (obj.getType() != CBORType.TextString) {
            throw new CBORException("Not a text String");
        }
        return obj;
    }

    public CBORObject ToCBORObject(Date obj) {
        String format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("Zulu"));
        return CBORObject.FromObjectAndTag(sdf.format(obj), 0);
    }
}

Closing; this feature will likely be included in version 4.0. Meanwhile, please comment on how to improve how this library converts to and from CBOR objects.