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 8508445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:09:46+00:00 2026-06-11T03:09:46+00:00

I want to convert a UTC date & time given in numbers for year,

  • 0

I want to convert a UTC date & time given in numbers for year, month, day, etc. to a time_t. Some systems offer functions like mkgmtime or timegm for this purpose but that is not standard and does not exist on my Solaris system.

The only solution I have found so far involves setting the local time zone to UTC with setenv and then call mktime. However this approach is not thread-safe, slow, not portable and even generates a memory leak on my system.

I have also seen approaches that tried to determine the current UTC offset using gmtime and then adding that to the result of mktime. But as far as I have seen all those approaches had gaps. After all, the conversion from the local time to UTC is not unique.

What do you think is the best solution?

  • 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-11T03:09:48+00:00Added an answer on June 11, 2026 at 3:09 am

    I have decided to implement my own version of mkgmtime and it was easier than I thought.

    const int SecondsPerMinute = 60;
    const int SecondsPerHour = 3600;
    const int SecondsPerDay = 86400;
    const int DaysOfMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    bool IsLeapYear(short year)
    {
        if (year % 4 != 0) return false;
        if (year % 100 != 0) return true;
        return (year % 400) == 0;
    }
    
    time_t mkgmtime(short year, short month, short day, short hour, short minute, short second)
    {
        time_t secs = 0;
        for (short y = 1970; y < year; ++y)
            secs += (IsLeapYear(y)? 366: 365) * SecondsPerDay;
        for (short m = 1; m < month; ++m) {
            secs += DaysOfMonth[m - 1] * SecondsPerDay;
            if (m == 2 && IsLeapYear(year)) secs += SecondsPerDay;
        }
        secs += (day - 1) * SecondsPerDay;
        secs += hour * SecondsPerHour;
        secs += minute * SecondsPerMinute;
        secs += second;
        return secs;
    }
    

    My main concern was that mkgmtime must be consistent with gmtime. Such that gmtime(mktime(t)) returns the original input values. Therefore I have compared the results for all multiples of 61 between 0 and MAX_INT for time_t and they are indeed equal (at least on my system). Therefore the above routine is correct.

    This outcome also means that the C library does not take leap seconds into account, which is a bad thing in itself but good for my purpose. The two functions will stay consistent for a long time. To be absolutely sure, my Timestamp class that uses this function always performs a quick check on program start and proves the consistency for a couple of meaningful values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have datetime objects in UTC time and I want to convert them
I'm working with Ruby. I want to convert UTC to various time conversion. I
How to convert UTC time into date time format in flex. I am using
I use MySQL DATETIME column to store date & time. Dates are in UTC.
I have a location (city, state), date, and time and I want to convert
I want to convert for example a particular date 12-11-2008 11:33:04.510 to UTC datetime.
I have a UTC date string, 2011-10-30T18:30:00Z, and I want to convert it to
I want to convert Strings like 20000603163334 GST or 20000603163334 -0300 to UTC time.
I am struggling a bit to convert the UTC time to the date format
I want to convert the timestamp object(fetched from models) according to given UTC offset

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.