Data Representation

I’m currently working on a suite of programs (which sounds a hell of a lot fancier than, “a couple of apps and a database,” — word to the wise, &c.) that deals with people. A person record has, as an option, a birthdate. You never know, someone, somewhere, might like to commemorate great-great-great-uncle Ralph’s birthday somehow.

Anyway, because I am a contrary old geezer, I’m writing all this stuff in Swift. There’s a server and a client, so of course I need to serialize the data as it gets transported between the two. Ultimately, the data resides in a database (because of course it does) and that’s currently Postgres — could become MySQL or something else, but who cares, there’s a swell abstraction layer between my code and the actual database. So, the birthday is really a date; I’m not doing astrology so I don’t care about where in the world the birth happened, nor when during the day. I just want a year, month, and day. And of course, I know, there are lots of calendars that are currently in use, so whatever, I still need some kind of an instant to store.

So. ISO-8601 is a sort-of standard, and the link here goes to Wikipedia rather than the actual specification because reading a standard can cause blindness and I don’t want that on my conscience. It defines a way that dates are supposed to be represented in text form. It looks kind of like “year-month-day T hour:minute:second.nanoseconds Z offset” where “offset” is what you need to add (or subtract) to GMT to get the local time zone.

I started off just trying to use the default serialization of the Date object in Swift, but that produces a bare integer (probably seconds since the epoch). So obviously, that didn’t work (if it had, I wouldn’t be writing this). Next, I tried being explicit about it on the client side. But that’s a problem, because if I’m going to all the trouble of doing an ISO 8601 encoded date, that includes a time zone, and that’s baloney because the client application isn’t collecting a time of day. So now I’m thinking, fine, I need to collect a Gregorian date and from that date, extract the year, month, and day, and then store those. Because the effect I’m trying to produce is that when a user looks at a calendar they can say, “Oh, tomorrow is the 105th anniversary of the birth of aunt Helen.”

For crying out loud. So no, I should not store a birthday as a Date object; I should store it as my own weird non-standard business object and translate it upon reading it from storage.

Published by pirateguillermo

I play the bagpipes. I program computers. I support my family in their various endeavors, and I enjoy my wonderful life.

2 thoughts on “Data Representation

  1. pirateguillermo – Santa Cruz, CA – I play the bagpipes. I program computers. I support my family in their various endeavors, and I enjoy my wonderful life.
    pirateguillermo says:

    Right?!

    My favorite part is where Apple provides `ISO8601DateFormatter` as a class to format dates specifically to — you guessed it — ISO 8601, but that class does not conform to `DateFormatter`, and thus cannot be used to format dates for JSON encoding.

Leave a ReplyCancel reply