Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Time zone conversion

Posted on 2003-02-22
Medium Priority
Last Modified: 2010-08-05

I am working on C++/unix environment and my database is informix.i am taking current time as local time using time(0)function. i wanted to know that is there an API or any time function which will convert this current-local time to any specific time zone. say for example my program is running in Central Time Zone and that gives me localtime which is CST, but i need time in EST.
Or is there any function/API which directy gives me time in any specific time zone. say for example irrespective of local time that gives me time in EST.

Question by:ajoshi79
LVL 12

Accepted Solution

Salte earned 200 total points
ID: 7998930
Unfortunately I don't know of any that will do that. It sounds easy enough but to my knowledge the only two time zones you can convert between using standard functions or easily available functions is local time and UTC.

Thus, you can convert from local time to UTC (GMT) and then you could change to a different timezone and change from UTC to that local timezone.

There are two problems here:

1. It is troublesome that you have to switch timezone and so on, it would be much better if the timezone had been a separate data structure.

2. How do you get the timezone for a different timezone? The truth is that your OS probably have tables and databases for all timezones and other info depending on place. However, those tables are used when you install the timezone for your system and thenafter they are more or less dead and not accessible. There is no API to my knowledge to access them. The installation program that does the selection knows their internal format and reads them but does so directly without using any API that would be available to user programs.

What would be nice to have is a function like:

tzptr = get_timezone("Berlin");

and the function would then return some timezone_t data type and you could then use a

struct tm * tmptr = localtime2(& time, tzptr);

function. Unfortunately there is no standard function for that sort of things.

Not even windows new framework .NET has functionality for this although it is a progress compared to old systems, they have a timezone data type but that class has only a static function to get the local timezone. Also, an obvious candidate would be a UTC timezone object and a static function that could do something like:

static timezone_t * TimeZone::GetTimeZone(const char * name);

where name could be strings like "PST" or "Los Angeles" and so on.

It is very unfortunate that it is this way, not even glibc which has done a big improvement in locale_t type functions which is a similar type of problem hasn't provided a solution for timezone problematic.

So, given that you get no support from the system, here is what you can do:

Make your own local time in your program. Just hard code all the info and data about various timezones that you know of. The real problem here is info such as when a specific timezone start daylight savings and when they stop it. Some places has standardized these things and you can algorithmically calculate when they start and stop daylight saving. Problem is that this isn't for all places and it is certainly not for all times. Historically various times where chosen and very often there was no algorithmic or other reason for when they decided to have daylight saving, it was simply a political decision made by some authority and you have to plot it into the table. What is worse: Historically daylight savings has even been different within the same nation from one region to another. US for example have traditionally had that different states changed to daylight savings at different dates. Causing all sorts of troubles in communication between them.

I believe that in recent times they do it all at the same time and the change to/from daylight saving is determined nationally but historically that wasn't how it was done.

So welcome to timezone hell :-)

If you want to make it simple though you can easily enough just plot down various time zone differences. When time is 6pm in new york it is midnight in central europe most of the time - the only exceptions are during daylight savings and in european countries that doesn't follow CET.

Using such a table you can thus convert the times properly at least for now. To make the change to daylight savings correct in the conversion you might discover that the code to handle that will suddenly blow your program up by several 1000 lines of code just to handle all the oddities around daylight savings etc.


Expert Comment

ID: 7999239
check out


for a peer-reviewed date-time library, supported by many c++ standards committee members via boost.org.  the lib is cross-platform and works with many platforms and compilers w/o any problems and it's 100% free ( i.e. free for commercial use, etc. )

the lib does have local time adjustments support, for docs of that part, see


Author Comment

ID: 8092573
Thanks for yor valuable suggestions.
It worked the way you suggested.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question