mktime() always uses local time zone and converts from UTC
If you’re working with UTC times, you may be upset to find that mktime() always converts using your local time zone thus wrecking your UTC times. Instead of mktime() use:
- Linux/Posix: timegm()
- Windows: _mkgmtime()
For example, let’s use timegm() to make a UTC time which we will pass to SolarAzEl() to calculate the Solar Elevation at that time, it must be passed a UTC time! See this post if you are interested in SolarAzEl().
// // Make UTC time for 10:16:00 on 5th. May 2022 // and use it to calculate the Solar Elevation // at the time via SolarAzEl(). SolarAzEl() // requires a UTC time! // tm utc; // tm_year is time since 1900 utc.tm_year = 2022 - 1900; // Month is zero based, i.e. Jan is month 0, May is 4 utc.tm_mon = 5 - 1; utc.tm_mday = 5; utc.tm_hour = 10; utc.tm_min = 16; utc.tm_sec = 00; utc.tm_isdst = 0; // Get UTC time_t val, do not change for local time offset! tim = timegm(&utc); // or _mkgmtime() on windows // // Now calculate Solar Elevation at // GPS coordinates 52.975, -6.0494 // at sea level using the UTC time. // double altitude = 0; double Az = 0.0; double El = 0.0; double lat = 52.975; double lon = -6.0494; SolarAzEl(tim, lat, lon, 0, &Az, &El); printf("Solar Azimuth: %f\n", Az); printf("Solar Elevation: %f\n", El); //