Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:07:21+00:00 2026-06-11T18:07:21+00:00

This one has been stumping me for a good minute now. I am working

  • 0

This one has been stumping me for a good minute now. I am working on a program that is written in C++ that I need to be able to send times encoded in ISO8601 between two different servers. The major stickler here seems to be Windows.

Thus far, I have ported over strptime() from NetBSD in order to easily read a date/time string off the wire and turn it into a tm*. However, I am totally stumped when it comes to correctly turning it into a localized tm* so I can then display this time to the user in their local timezone using strftime(). A naive approach would be to simply take the difference between localtime(time(0)) and gmtime(time(0)), but this wouldn’t be consistent for an arbitrary date where daylight savings time is involved.

The approach I’ve been taking so far is to use only POSIX date and time functions and to write Windows shims when necessary (done for strptime() so far, and timegm() perhaps in the future if necessary). I would prefer to keep doing that, but the simple fact is that the tm* struct in Windows is missing some fields that Linux and OSX do, so I might not have a choice but to write non-portable code that is guarded by #if WIN32. I also would really prefer not to have to pull in boost for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:07:22+00:00Added an answer on June 11, 2026 at 6:07 pm

    Given a struct tm *utc_tm you can turn it into a time_t utc using (nonportable) timegm, then turn that into a struct tm *local_tm using localtime:

    time_t utc = timegm(utc_tm);
    struct tm local_tm;
    localtime_r(&utc, &local_tm);
    

    From what I can tell, the simplest implementations of timegm involve doing the conversion yourself using a month table and leap year calculation. Alternatively, if your strptime fills in tm_yday correctly you can use the formula from https://stackoverflow.com/a/9076848/567292:

    tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
        (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
        ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has been stumping me for a while. But I'm no expert. This
I've been working on some MySQL Joins but this one has got me a
This one has been driving me nuts. I have a div that has it's
This one has been driving me nuts for a few days now and I've
This one has been puzzling my for some time now. Let's imagine a class
This one has been bugging me for a while now, but I never seem
This one has been bugging me for a while now. Is there a way
This one has been bothering me for a while now, How should we store
This one has been haunting me for quite a while now.. I have been
this one has been puzzling me for a few days now and I feel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.