How do you convert a UTC tm structure to a UTC time_t structure? mktime would work except it converts the time back to local time.

Here is what I have so far:

``````time_t t = time(NULL);
tm *ptm = gmtime(&t);``````

Thanks,
Justin

## All 11 Replies

time_t is a black box, the right way to use it is through the inferface of functions that work with it. It could represent the time in increments of skittles strewn across the compiler developer's desk and still work perfectly when used the right way. ;)

What are you trying to do that requires intimate knowledge of how time_t is represented?

Hah, interesting perspective of time_t.

What I am trying to do is to get the UTC epoch time from the local time, is there an easier way to do that?

>What I am trying to do is to get the UTC epoch time from the local time
The epoch is usually based on UTC, so your question is kind of confusing. :( If you're lucky, this will work to find out both the UTC and local epoch, but it's not portable because time_t is a black box and setting it to 0 isn't guaranteed to be meaningful:

``````#include <ctime>
#include <iostream>

int main()
{
using namespace std;

time_t zero = 0;

cout << asctime(gmtime(&zero));
cout << asctime(localtime(&zero));
}``````

I'm confused. When I do:

``cout << time(NULL);``

That outputs the local epoch time, should it not according to your statement? gmtime outputs the time as UTC, but it is not in epoch format. Is there a way to convert it to epoch format?

`time(NULL)` doesn't give you the epoch, it gives you the current time for whatever type and value that is on your compiler, but the number of skittles since development time is a good assumption. ;)

Ed's guess is that what you mean by epoch isn't what Edward means. What are you expecting as the epoch format?

On most unix compilers, time(NULL) returns the number of seconds since January 1, 1970. If I do time(NULL) right now, it gives me 1218651371 (in EST). What I want it to give me is 1218665771, the UTC time of the local time. Does that make sense?

Woohoo! Edward now understands. :) This still isn't portable because it assumes that a time_t value of 0 is the epoch, but it should give you the result you need. difftime() gives you the number of seconds between two time_t values:

``````#include <ctime>
#include <iostream>

int main()
{
using namespace std;

time_t now = time(0);

// Local seconds since the epoch
cout << fixed << difftime(now, 0) << '\n';

// UTC seconds since the epoch
cout << fixed << difftime(mktime(gmtime(&now)), 0) << '\n';
}``````

Thanks, I think we almost have it. I just ran it and got:

1218659645.000000
1218677645.000000

The difference between the two is five hours, but this is being run from a pc set to EST, so I would expect a difference of four hours. Is there something wrong with the c++ code or the way the pc is setup? Doing the date command in the terminal window brings up:
Wed Aug 13 16:37:13 EDT 2008
So it looks like it is set to the correct timezone.

Thanks,
Justin

So that says that I shouldn't manually modify time_t, but I still don't think a difference of five hours is correct.

>this is being run from a pc set to EST, so I would expect a difference of four hours
EST is UTC-5; how do you figure it should be 4 hours?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.