std:time() returns invalid time.

It is said that std:time function from <time.h> should return the same
time if called same time, no matter what computer and timezone it's configured
however

1
2
3
using namespace std
time_t current; time(&current);
cout << "time:" << current << endl; 


On my personal pc it returns GMT-5, on vps it did same till all of the sudden
it started returning GMT+3. Very awkward.
Shouldn't it return GMT+0 anyways?

Thanks!
Last edited on
http://www.cplusplus.com/reference/ctime/time/

Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime, gmtime or difftime).

Here is a function I'm using in a project of mine, to display the current time. This should illustrate what salem c said:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <time.h>

using namespace std;

void displayTime()
{
	time_t rawtime;
	struct tm * timeinfo;
	char buffer[80];

	time(&rawtime);
	timeinfo = localtime(&rawtime); //gets local time

	strftime(buffer, 80, "%A %d %B ___ %R", timeinfo); //lets you format the way you want to output time and/or date
	puts(buffer);
}

int main()
{
    displayTime();
    
    return 0;
}


references:
time() : http://www.cplusplus.com/reference/ctime/time/
localtime() : http://www.cplusplus.com/reference/ctime/localtime/
strftime() : http://www.cplusplus.com/reference/ctime/strftime/

Hope this helps
Last edited on
The code you have posted does not compile.

Instead of
 
time_t current; time(current);
write
 
time_t current; time(&current);
or
 
time_t current = time(nullptr);
@Peter87, My bad, forgot the & *updated*

@salem c, @H00G0
very interesting, I saw something like that around.
I'm looking a way to get the same time_t on GMT+0 no matter the os timezone or machine.
If i'm not wrong localtime returns time on os's timezone, perhaps gmtime.
Now we are getting tm struct but I want it to be time_t, I don't see any
function to convert tm into time_t however I did found this
1
2
3
4
5
		time_t timegm(tm* _Tm)
		{
			auto t = mktime(_Tm);
			return t + (mktime(localtime(&t)) - mktime(gmtime(&t)));
		}


That whole thing seems rather too complex for such simple request.

@TheIdeasMan
Something I did looked into however since I was unable to find someone else having the same issue with time(), i figured it wouldn't be no different, I would still get different results on those different machines.

Figured if system's own time is messed up, there's nothing to do. Even tho
by comparing window's clock, they seem right expect on different timezones. It's weird.
I do have a program running which messes with timezones. It's purpose was to change os timezone to fool browser.

For now I ended up writing a php function what displays time() for the server, I downloaded html and converted it into time_t. I do it once a program starts and then just counting seconds on separate thread. oh yeah, this whole thing gave me many headaches.

Last edited on
What makes you think that time() is returning one timezone or another? The code you posted should display an integer. When I put your code in a program and ran it just now, it prints
time:1549206207


There may also be slight (or large) differences depending on how accurate the computer's clock is.

If you aren't picky about the format and your program is single-threaded, the easiest way to display the time is via ctime(). If it's multi-threaded, see if your compiler supports ctime_r() which is thread safe.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    time_t current;
    time(&current);
    cout << "time:" << current << endl;
    cout << ctime(&current) << '\n';
}


Run the program in my current timezone (EST):
$ ./foo
time:1549206645
Sun Feb  3 10:10:45 2019

Run the program 6 seconds later with timezone set to GMT:
$ TZ=GMT ./foo
time:1549206651
Sun Feb  3 15:10:51 2019

Topic archived. No new replies allowed.