DateTime, the Epoch and DocumentDb

后端 未结 3 925
时光说笑
时光说笑 2020-12-19 10:56

So I read this very interesting blog on working with datetime in Azure DocumentDb. The problem being that, right now, Azure DocumentDb does not support range search on datet

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 11:45

    In my experience i haven't encountered a more 'established' standard than the UNIX epoch. This being said, some architectural/technological aspects of time storage have been discussed before: Timestamps and time zone conversions in Java and MySQL

    I would ask why risk using your own convention? It's a risk because: what if some time you will want to add hours to your day count, maybe to be able to order people based on when exactly during the day they were born. The question can be extended to: what if at some point you want to measure more generic or more fine-grained moments; you would have to translate your entire feature, possibly throughout many layers of your application, to a more generic mechanism/convention. Another (similar) question would be: will you always measure once-in-a-lifetime events for the people in your database or will they be able to create new, unlimited events? As the number of events increases the risk of collision increases too and a day count would not be as suitable as a timestamp measured in seconds or milliseconds.

    UNIX time is basically ubiquitous, you have special methods for getting it in most programming languages. The time-keeping architecture i will always support & implement in my projects is this: http://www.currentmillis.com/tutorials/system-currentTimeMillis.html

    As also stated in my answer to the question linked above, the advantages of storing time as milliseconds since the UNIX epoch are:

    • architecture clarity: server side works with UTC, client side shows the time through its local timezone
    • database simplicity: you store a number (milliseconds) rather than complex data structures like DateTimes
    • programming efficiency: in most programming languages you have date/time objects capable of taking milliseconds since Epoch when constructed (which allows for automatic conversion to client-side timezone)

    Because you mentioned C#, DateTime.MinValue comes to mind. This would basically be the year 0 (midnight, 1st of January).

    Also, this would be some code which would allow you to get the millis since your chosen reference date (whatever it is) but note that 1900 is still different than .NET's 'epoch' (DateTime.MinValue)

    // Unix Epoch
    (DateTime.UtcNow - new DateTime (1970, 1, 1)).TotalMilliseconds
    // NTP Epoch
    (DateTime.UtcNow - new DateTime (1900, 1, 1)).TotalMilliseconds
    

提交回复
热议问题