?
Solved

Time zone conversion

Posted on 2003-02-22
3
Medium Priority
?
1,519 Views
Last Modified: 2010-08-05
Hi,

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.

thanks.
0
Comment
Question by:ajoshi79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Accepted Solution

by:
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.

Alf
0
 
LVL 7

Expert Comment

by:burcarpat
ID: 7999239
check out

    http://www.boost.org/libs/date_time/doc/index.html

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

    http://www.boost.org/libs/date_time/doc/local_time_adjust.html
0
 

Author Comment

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

Featured Post

Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

741 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