2

Is there a way to get a DateTime object from a unix timestamp and hour offset? I want to be able to print a formatted string, for example Wed Apr 02 17:15:06 2014.

I have:

  1. A unix timestamp: 1396451852.
  2. An offset from my hour zone, +1 hour.

Note: I get the timestamp from an internet service and want to make the conversion in the Arduino.

  • To clarify: You have a unix time-stamp on your arduino and you want to make the conversion there? Could the supplier of the timestamp instead provide the formatted string? – JRobert Apr 2 '14 at 15:23
  • Yes! i get this unix time-stamp from internet. – lasote Apr 2 '14 at 15:24
2

The seconds since (UNIX) epoch relies on a database.

New editions of the database and code are published as changes warrant, usually several times per year.

Your options can:

  • Reduce the timezone database to the timezone you are actually interested in;
  • Don't forget about daylight savings time for you specific timezone;
  • The leap years are well known too and reasonably easy to program;
  • The leap seconds are decided upon usually only couple months before they are introduced and therefore cannot be accounted for in an algorithm.

Conclusion

  • So can you convert epoch to a time/date format? Yes.
  • Will it be accurate? It'll drift away over time (months or years)

Advise

If you retrieve epoch from an Internet service, why not use (another?) webserver that has the full timezone database available to convert it to accurate date/time, or 'synchronize' against every once in a while?

|improve this answer|||||
  • +1 For letting me know that leap seconds exist AND for the great answer. – Ricardo Apr 4 '14 at 17:16
1

The Arduino Time Library looks like it can do it. Or have a look at the strftime() function from unix and unix-oid environments. There is source to strftime() here. It's pretty large but you can probably cut it down considerably to just the format parts you need.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.