1

I am very restricted in memory usage.

I need to store a datetime in my program. Precision is one second. Only 4 bytes for one datetime value.

What is the best way to achieve this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
tmporaries
  • 1,523
  • 8
  • 25
  • 39
  • 2
    POSIX time stamp probably, which clocks in 1 second since 1st January 1970 I believe – Creris Feb 06 '14 at 20:05
  • Close voted: Question is too broad (opinion based) as it stands! – πάντα ῥεῖ Feb 06 '14 at 20:05
  • @TheOne Too narrow with 32 bits in general! Depends on the precision the OP finally want's to achieve. – πάντα ῥεῖ Feb 06 '14 at 20:07
  • 2
    What @TheOne says, but beware the [Y2038 problem](http://en.wikipedia.org/wiki/Year_2038_problem). – Mark Ransom Feb 06 '14 at 20:08
  • 1
    @MarkRansom What about calculations for dates **before** 1st January 1970. Unlikely to get such _'time stamps'_ of course ... (can't upvote anything right now actually, would have upvoted your comment though!) – πάντα ῥεῖ Feb 06 '14 at 20:11
  • What time interval need to be represented? – KonstantinL Feb 06 '14 at 20:12
  • @πάνταῥεῖ the standard uses a signed 32-bit value so you can go back to 1902. Which points out an interesting hack, one way to avoid the Y2038 problem is to use unsigned integers - but then you're left with a Y2106 problem. – Mark Ransom Feb 06 '14 at 20:16
  • @MarkRansom Well, you're explaining my concerns quite well :) ... I think the OP needs to define a certain point of relation/precision for his _'time stamps'_, to get an appropriate answer ... – πάντα ῥεῖ Feb 06 '14 at 20:22

1 Answers1

4

Pick the earliest date/time you need to represent and mark that your epoch. Then use a four byte unsigned type as seconds-since-epoch. Be aware this will only give you a ~68 year range but that's all you can get with your restrictions.

Mark B
  • 95,107
  • 10
  • 109
  • 188