Couchbase.lite.mapping and datetime format

I know that Couchbase.lite.mapping is not officially supported. However, I have a question that someone may know the answer to.

The standard ISO8601 format that datetimes are mapped to in the database at the moment is:

2019-10-22T13:16:05.4344880+02:00

However, the format used at the server end in the solution that I integrate up to is:

2019-10-22T13:16:05+0200

I.e. no colon in the time offset and no nano seconds.

The backend system was developed 6-7 years ago and it is not really a viable solution to change the format for all existing dates in the database…

So question is: How can I set another datetime format in the mapping plugin? I have written a little more about it here: https://github.com/couchbaselabs/Couchbase.Lite.Mapping/issues/16 - where I have also tried to use a custom converter without success.

1 Like

The person who was making that is no longer at the company so it’s entered into a period where probably no one will look after it. Hopefully someone in the community will take it up if they need it.

I appreciate that Jim. However, I find that this initiative is such a good one for developers like myself that I would really recommend that someone with sufficient knowledge would keep it alive. It just makes using CBLite with C# so much easier!

Unfortunately, I’m not (yet) at a level where I can take over maintenance of such a project. But I’ll be glad to help in whatever way I can.

I have the same request though mine is just to ensure DateTimeOffsets are always stored in UTC - I know, it kinda defeats the TimeZoneOffset purpose but I want to use 8601 and I don’t want the offset - at least for now… I’m handling it by using two properties on my models that get converted with the same backing field - one for the conversion process and one for use in the App.

The JsonIgnore annotation filters out the property I don’t want saved.

        private DateTimeOffset _dtCreated = DateTimeOffset.UtcNow;
        [JsonProperty("dt_created")] 
        public DateTimeOffset DtCreatedUtc
        {
            get => _dtCreated.UtcDateTime;
        }
        
        [JsonIgnore]
        public DateTimeOffset DtCreated {
            get => _dtCreated;
            set => _dtCreated = value;
        }

Well, the issues I had were “fixed” by subsequent updates to the mapping lib.

1 Like

Awesome! Glad you got it “fixed”.

I ended up with this solution in my mobile apps which I don’t love but it works. Posting in case it’s helpful for some other Internet searcher.

        public const string DateTimeFormat = "yyyy-MM-ddTHH:mm:sszzz";

        private DateTimeOffset _dtCreated = DateTimeOffset.UtcNow;
        [JsonProperty("dt_created")] 
        public string StrCreatedUtc
        {
            get => _dtCreated.UtcDateTime.ToString(DateTimeFormat);
            set => _dtCreated = DateTimeOffset.Parse(value);
        }
        
        [JsonIgnore]
        public DateTimeOffset DtCreated {
            get => _dtCreated;
            set => _dtCreated = value;
        }