noyruto88
noyruto88

Reputation: 907

How to get UTC time offset in c++ with windows os

My question is pretty simple. How can I get the UTC offset for Philippines which is +8 and for Japan which is +9, and convert that to integer.

Any suggestions is a great help. Thank you.

UPDATE

For further explanation, I have a c++ desktop application, and I need to automatically detect its timezone wherever the application is used. Like if the application will be used in Japan, the timezone offset for japan will be used. Is that possible?

Upvotes: 3

Views: 6514

Answers (2)

joytrick
joytrick

Reputation: 17

If in your os, std::localtime can access time zone info (in most cases it can, by definition), this is my one-line code:

auto offset_epoch = std::localtime(new time_t(0));

where offset_epoch->tm_hour is the offset hour and offset_epoch->tm_min is the offset minute. You can safely ignore offset_epoch->tm_year as it is of value 70.

It works fine for me so far.

Upvotes: 0

Howard Hinnant
Howard Hinnant

Reputation: 218700

If you would rather not dig into the Windows-specific API, here is portable, and C++20-standard C++ code1 to do this using Howard Hinnant's timezone library.

#include "date/tz.h"
#include <iostream>

int
main()
{
    using date::operator<<;
    using std::chrono::system_clock;
    std::cout << date::current_zone()->get_info(system_clock::now()).offset << '\n';
}

date::current_zone() detects the computer's current local time zone. This returns a pointer to a date::time_zone which has a member function called get_info().

You can call time_zone::get_info with either a UTC time_point or a local_time time_point to get all kinds of information about this time zone at the indicated point in time (as the information changes with times according to current politics). Use of system_clock::now() as the time_point will get the current information for the time_zone.

A date::sys_info is returned which is an aggregate, one of which members is called offset. This is the current UTC offset with units of std::chrono::seconds.

This just output for me:

-14400s

Indicating that my current local time zone is 14400 seconds west of UTC.

For typical code, one should not program down at the level of adding UTC offsets directly. There is a higher-level API to do this for you. Use of the higher-level API prevents careless errors such as adding the UTC offset when you should have subtracted (or vice-versa). However this low-level API exists because sometimes low-level access to information like this is what makes your program efficient.

For example, here is a higher-level code that simply prints out the current local time:

std::cout << date::make_zoned(date::current_zone(), system_clock::now()) << '\n';

system_clock::now() is still explicitly called, but now the UTC offset addition, as well as the call to current_zone()->get_info() is encapsulated under this higher-level API.


1 This library is in namespace std::chrono in C++20: http://eel.is/c++draft/time.zone.db.access#8

Upvotes: 9

Related Questions