简体   繁体   中英

Storing date as time in millis

I try to represent date objects in a data storage without the hassle of Date object in Java. So I thought of using just a time in milliseconds and store the UTC time zone as well. I thought about using simple shift routines to combine everything in a single long as time zone is just 5bits (+/-12).

Can someone see any problem with this? What other compact storage schemes (other than textual representation) of date exist and how do they compare to this?

I think you're under valuing granularity in your time zone and over valuing the need for bits in the timestamp.

A long has has 8 bytes for this purpose.

Lets say you allow yourself 2 bytes for the time zone. That leaves you with 6 for the timestamp. 6*8 = 48 bits for a timestamp.

The largest number a 48 bit unsigned integer can handle is 281474976710655.

Divide by 1000 to get from miliseconds to seconds 281474976710

Punch that number into an epoch converter : 10889-08-02T05:31:50+00:00

That's the year 10,889 when we're in 2,015.

Just use 2 bytes for the timezone. You've got the space. That will easily allow you to represent the timezone as minutes offset +-24 hours. And since it's whole bytes, the packing code will be simpler to comprehend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM