Jump to content

1969 return for localtime_s in C++

Go to solution Solved by Mr_KoKa,

localtime function converts timestamp into calendar time. The second param is time to convert (your t variable) and since your is 0 then it is 0 (which is 1970 + timezone).

try:

    time_t t = time(NULL);

 

Well I have been researching why I keep getting the epoch time instead of the current time:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	time_t t = 0;
	tm time;
	char buffer[26];
	errno_t err;

	err = localtime_s(&time, &t);

	if (err)
	{
		cout << "ERROR" << endl;
		cin.get();
		return 0;
	}
	cout << "This is the time:" << endl;
	cout << time.tm_mon << "/" << time.tm_mday << "/" << time.tm_year + 1900 << endl;
	cout << "Hour: " << time.tm_hour << endl << "Min: " << time.tm_min << endl;

	err = asctime_s(buffer, 26, &time);

	cout << endl << "Now with asc conversion: " << endl;
	cout << "This is the time:" << endl << buffer;
	cin.get();
	return 0;
}

 

Its not throwing an error or anything but when I run this program I get:

Quote

This is the time:
11/31/1969
Hour: 19
Min: 0 

Now with asc conversion:
This is the time:
Wed Dec 31 19:00:00 1969


EDIT: I realized that localtime_s is a conversion from a time point to a tm structure. I thought it returns the current time. I have to first set time_t t to the current time point.

I set it to 0 because it was causing errors by being null.

Link to comment
Share on other sites

Link to post
Share on other sites

localtime function converts timestamp into calendar time. The second param is time to convert (your t variable) and since your is 0 then it is 0 (which is 1970 + timezone).

try:

    time_t t = time(NULL);

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Mr_KoKa said:

localtime function converts timestamp into calendar time. The second param is time to convert (your t variable) and since your is 0 then it is 0 (which is 1970 + timezone).

try:


    time_t t = time(NULL);

 

Yes, I realized that 10min after posting. I already edited to show that I realized that.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, ProfoundDisputes said:

Yes, I realized that 10min after posting. I already edited to show that I realized that.

Which is annoying because I have been trying to figure it out for 2+ hours...

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Mr_KoKa said:

localtime function converts timestamp into calendar time. The second param is time to convert (your t variable) and since your is 0 then it is 0 (which is 1970 + timezone).

try:


    time_t t = time(NULL);

 

Thanks for telling me how to get the current time with time(NULL). I was getting nowhere looking online for some reason. I was in the middle of trying to use:

 

std::chrono::system_clock::now()

Which I couldn't figure out what it returned.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, ProfoundDisputes said:

Thanks for telling me how to get the current time with time(NULL). I was getting nowhere looking online for some reason. I was in the middle of trying to use:

Read reference more carefully : ) It is all there.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

Read reference more carefully : ) It is all there.

I am a noob when reading programming documentation. I am learning, though haha. Everything looks ambiguous at this point.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×