C++ will let me get the local time with std::localtime(), but that presents a problem when I’m using an online compiler whose server is in a different continent. How do I get the time in PST instead of “local” time?
Here’s the code I’m using:
<code>#include <ctime>
#include <iostream>
int main()
{
time_t secSinceEpoch = time(0);
tm* timeStruct = std::localtime(&secSinceEpoch);
int hour = timeStruct->tm_hour;
int min = timeStruct->tm_min;
std::cout << "Time in California is " << hour << ":" << min << std::endl;
return 0;
}
</code>
<code>#include <ctime>
#include <iostream>
int main()
{
time_t secSinceEpoch = time(0);
tm* timeStruct = std::localtime(&secSinceEpoch);
int hour = timeStruct->tm_hour;
int min = timeStruct->tm_min;
std::cout << "Time in California is " << hour << ":" << min << std::endl;
return 0;
}
</code>
#include <ctime>
#include <iostream>
int main()
{
time_t secSinceEpoch = time(0);
tm* timeStruct = std::localtime(&secSinceEpoch);
int hour = timeStruct->tm_hour;
int min = timeStruct->tm_min;
std::cout << "Time in California is " << hour << ":" << min << std::endl;
return 0;
}
How to I manipulate localtime() so it returns the time in Los Angeles, rather than in India where the online server is?
New contributor
Samuel van der Veen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.