How do i get the current time of a different time zone? Example, i need to know the current time in Singapore where as my system is set to PT time.
5 Answers
New answer for a very old question.
Given a C++11 or C++14 compiler, and this timezone library, the current time in Singapore is:
#include "tz.h"
#include <iostream>
int
main()
{
using namespace std::chrono;
std::cout << date::make_zoned("Asia/Singapore", system_clock::now()) << '\n';
}
which just output for me:
2015-08-19 05:25:05.453824 SGT
This gives the current local date, time and abbreviation in use. And it is based off of the <chrono>
library and the IANA timezone database.
std::chrono::system_clock::now()
returns a time stamp in the UTC timezone. This program locates the timezone information for "Asia/Singapore", and translates the UTC timestamp into a pair representing the local time and the current timezone for that location.
The above program is independent of the computer's current timezone.
C++20 Update
In C++20 you can now do this (once your vendor ships it):
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
std::cout << zoned_time{"Asia/Singapore", system_clock::now()} << '\n';
}
Output:
2015-08-19 05:25:05.453824 SGT

- 206,506
- 52
- 449
- 577
-
-
1MSVC has shipped it. gcc has implemented it on trunk but not yet shipped it. LLVM has yet to implement it. – Howard Hinnant Jun 08 '23 at 13:54
One implementation would be to use time
to get the current time in UTC and then manipulate the TZ
environment variable to your destination timezone. Then use localtime_r
to convert to that timezone's local time.

- 95,107
- 10
- 109
- 188
-
Warning: It is unsafe to set an environment variable in a multi-threaded program (at least on POSIX). So you should only do this if your program is single-threaded. – Thayne Dec 12 '22 at 07:04
Use UTC (GMT) as much as possible.
If you need to (for example) print a report that's going to another time zone, use something like SystemTimeToTzSpecificLocalTime() to localise it.

- 7,631
- 2
- 24
- 30
You could convert to GMT then convert to whatever time zone you want.

- 49,466
- 12
- 107
- 135
-
GMT is the way to go. (I'm speaking at somebody in the US, so it isn't a matter of it being my time zone.) – David Thornley Jul 27 '10 at 21:10
-
-
You're going to have to provide quite a bit more context if you want a more specific answer. What's your operating system? Platform? Library set? – nmichaels Jul 28 '10 at 11:44
C++20 standart
List of timezones can find here - www.zeitverschiebung.net
String date format like %a %b %e %H:%M:%S %Z %Y
, here - en.cppreference.com
If you need accuracy in seconds, etc, change minutes in std::chrono::floor<std::chrono::minutes>
to the required value.
std::string date = std::format("{:%a %b %e %H:%M:%S %Z %Y}",
std::chrono::zoned_time{ "Asia/Singapore",
std::chrono::floor<std::chrono::minutes>(
std::chrono::system_clock::now())
});
Also you can use std::vformat
std::string epoch2StrTZ(const time_t epoch, const std::string& date_format)
{
std::string date = std::vformat("{:" + date_format + "}", std::make_format_args(
std::chrono::zoned_time{ "Asia/Singapore",
std::chrono::floor<std::chrono::minutes>(
std::chrono::time_point<std::chrono::system_clock>(
std::chrono::seconds(epoch)
)
)
}));
return date;
}

- 1
- 2