Shop Learn
Converting from UTC to local time — Parallax Forums

Converting from UTC to local time

HI All,

I have been working on putting a new GPS into an older project. I have a nixie clock that I made in the 90's and then sometime after the Prop1 came out I converted it to a Prop. My old GPS had a setting for UTC offset but the new module does not. Is there a slick way to convert to local time? I am -7 or -8 hours from UTC.

Thanks!

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,057
    edited 2021-08-12 18:03

    I can't think of a hard way to do it! It's really easy: just convert the UTC hours to an integer, and add your timezone offset to it. If the result is below zero, add 24 and subtract one from the day. If that result is below one set it to the number of days from the previous month, and subtract one from the month. And if that result is below one, set it to 12, and subtract one from the year.

    -Phil

  • JonathanJonathan Posts: 1,023

    Hi Phil,

    Yup, that is what I had come up with. I seem to remember that there was some slick way of doing it. I have been looking around but haven't found it. I also used to have some way of figuring out if it was DST or standard time but I haven't found that yet either. I must have lost some code along the way or I just don't know where to look in my old files.

    Many thanks!

  • ersmithersmith Posts: 5,049

    @Jonathan said:
    HI All,

    I have been working on putting a new GPS into an older project. I have a nixie clock that I made in the 90's and then sometime after the Prop1 came out I converted it to a Prop. My old GPS had a setting for UTC offset but the new module does not. Is there a slick way to convert to local time? I am -7 or -8 hours from UTC.

    Thanks!

    You don't mention what language you're using, but if it's C then there are standard library functions for converting between UTC and local time. PropGCC and flexspin have them, Catalina probably does as well.

  • JonathanJonathan Posts: 1,023

    I am using Spin, I should have mentioned that. I have been meaning to look at flexspin, I will give that a shot,

    Thanks!

  • Dave HeinDave Hein Posts: 6,336
    edited 2021-08-12 21:44

    You could convert it to unix time, make the time zone adjustment, and then convert back to year, month, day, hour, minute and seconds. I have routines written in Spin that do the conversions.

    EDIT: I've attached unixtime.spin that contains the routines I mentioned. The routines include a parameter for the timezone offset.

  • @"Dave Hein" said:
    You could convert it to unix time, make the time zone adjustment, and then convert back to year, month, day, hour, minute and seconds. I have routines written in Spin that do the conversions.

    This is my solution as well. It is a bit more complex but it works everywhere, always, and also makes compensating for DST a snap. All you need to know is the TZ offset from UTC in seconds, the date/time DST starts (if DST is observed) and the length DST runs (238 days in the US, if I remember correctly).

    If you know you’ll never need to leave the “mainstream” timezones, then Phil’s solution works great for time zones with an integer offset from UTC. But it doesnt work on those “off-by-30-minute” oddballs auch as found in India, Sri Lanka, Afghanistan, Iran, Myanmar, Newfoundland, Regions of Australia, Venezuela, Nepal, Chatham Islands, and the Marquesas Islands. If true “anywhere” compatability is needed, you’ll need to use the more complex method.

  • JonathanJonathan Posts: 1,023

    @"Dave Hein" Thanks a ton! It will be a week until I am back where the project is. I will try to grok your code between now and then.

Sign In or Register to comment.