473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

obtaining the time offset from UTC

Hello,
I need to find out the time difference between UTC and local time.
I am doing it the following way

#include <sys/timeb.h>
#include <stdio.h>
int main()
{
struct timeb tp;
ftime(&tp);
printf("%d", tp.timezone);
}

But the problem is that the timezone value is not adjusted for daylight
saving and always returns local time zone offset.
My TZ environment variable is set to MET
Now the DST rules apply and the offset should be +2 hours
When daylight saving is not in effect, then the offset should be +1 hour.

Is there any way to find out the correct offset . i.e. it should return +2
when DST is in effect and +1 otherwise

Regards,
deep
Nov 14 '05 #1
4 24254


__jakal__ wrote:
Hello,
I need to find out the time difference between UTC and local time.
I am doing it the following way

#include <sys/timeb.h>
#include <stdio.h>
int main()
{
struct timeb tp;
ftime(&tp);
printf("%d", tp.timezone);
}

But the problem is that the timezone value is not adjusted for daylight
saving and always returns local time zone offset.
My TZ environment variable is set to MET
Now the DST rules apply and the offset should be +2 hours
When daylight saving is not in effect, then the offset should be +1 hour.

Is there any way to find out the correct offset . i.e. it should return +2
when DST is in effect and +1 otherwise


First, the C Standard makes no guarantees about how
good the system's timekeeping is. Some systems keep track
of time zones and seasonal adjustments, while others do not
and must have their clocks adjusted manually. (A very few
systems have no clocks at all.) From C's point of view,
then, your problem may not be solvable.

However, if you are using a system that keeps track of
such things, you can determine the current UTC offset with
something like (error-checking omitted):

time_t now = time(NULL);
struct tm lcl = *localtime(&now);
struct tm gmt = *gmtime(&now);
printf ("%s\t%d\t%d\n", "year", lcl.tm_year, gmt.tm_year);
printf ("%s\t%d\t%d\n", "yday", lcl.tm_yday, gmt.tm_yday);
printf ("%s\t%d\t%d\n", "hour", lcl.tm_hour, gmt.tm_hour);
printf ("%s\t%d\t%d\n", "min", lcl.tm_min, gmt.tm_min);
printf ("%s\t%d\t%d\n", "sec", lcl.tm_sec, gmt.tm_sec);

Examine the corresponding elements of `lcl' and `gmt' to
determine your UTC offset.

--
Er*********@sun.com

Nov 14 '05 #2
__jakal__ wrote:

I need to find out the time difference between UTC and local time.
I am doing it the following way

#include <sys/timeb.h>
No such standard header.
#include <stdio.h>
int main()
{
struct timeb tp;
No such standard type
ftime(&tp);
No such standard function.
printf("%d", tp.timezone);
}

But the problem is that the timezone value is not adjusted for
daylight saving and always returns local time zone offset.


Which makes your code entirely non-portable. The things available
to you via <time.h> are detailed below, in an excerpt from N869:

B.22 Date and time <time.h>
NULL size_t *time_t
CLOCKS_PER_SEC clock_t struct tm
clock_t clock(void);
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm *timeptr);
time_t time(time_t *timer);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
size_t strftime(char * restrict s,
size_t maxsize,
const char * restrict format,
const struct tm * restrict timeptr);

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #3
CBFalconer wrote:

__jakal__ wrote:

I need to find out the time difference between UTC and local time.
I am doing it the following way

#include <sys/timeb.h>


No such standard header.
#include <stdio.h>
int main()
{
struct timeb tp;


No such standard type
ftime(&tp);


No such standard function.

"return to sender...no such number, no such home" -- Elvis

--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
Nov 14 '05 #4
On Tue, 10 May 2005 10:24:39 -0400, Eric Sosman <er*********@sun.com>
wrote:


__jakal__ wrote:
Hello,
I need to find out the time difference between UTC and local time.
I am [using (POSIX) struct timeb and ftime()]
But the problem is that the timezone value is not adjusted for daylight
saving and always returns local time zone offset.
My TZ environment variable is set to MET
Now the DST rules apply and the offset should be +2 hours
When daylight saving is not in effect, then the offset should be +1 hour.

Is there any way to find out the correct offset . i.e. it should return +2
when DST is in effect and +1 otherwise
First, the C Standard makes no guarantees about how
good the system's timekeeping is. Some systems keep track
of time zones and seasonal adjustments, while others do not
and must have their clocks adjusted manually. (A very few
systems have no clocks at all.) From C's point of view,
then, your problem may not be solvable.

And if it does, standard C doesn't require it to use env var TZ.
POSIX/SUS does, and provides ftime() and struct timeb, which also
includes member dstflag, which is nonzero when (for a time that) DST
is in effect (assuming that the daylight/summer change is always +1hr,
which is very common but probably not universal).
However, if you are using a system that keeps track of
such things, you can determine the current UTC offset with
something like (error-checking omitted):

time_t now = time(NULL);
struct tm lcl = *localtime(&now);
struct tm gmt = *gmtime(&now); <snip> Examine the corresponding elements of `lcl' and `gmt' to
determine your UTC offset.


Or just look at lcl.tm_isdst to determine if there is _any_
daylight/summer in effect and if so assume +1hr.

These are I believe the only fully-standard-C ways. Note that
localtime(), and its difference from gmtime(), can be applied to times
other than the current time, if you wish.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: yashgt | last post by:
In MS SQL 2000, I would like to obtain the timezone offset from UTC for a given date. For today's date, I can do DATEDIFF(ss,GETDATE(),GETUTCDATE()). However, the offset for a future date may not...
8
by: new pip | last post by:
I'm using Windows os. If the current system date time is '28 Jun 2001 14:17:15 +0700', how can I obtain the value '+0700' using python? Thank you
0
by: sags5495 | last post by:
Hello all, For a program I want to fix, I need to find a portable way to get the offset from local time to UTC (coming from the Time Zone plus any daylight savings in effect). At first...
2
by: ZR | last post by:
Hello, I need to convert a GMT time string to local time. I can fill out a "tm" structure with the GMT time string. Are there any standard C (or OS) time functions that will allow me to do this? ...
3
by: Evan Klitzke | last post by:
Although it is not present in ANSI C, the GNU version of stftime supports the conversion character %z, which is a time offset from GMT. The four digit time offset is required in RFC 2822...
4
by: Steve | last post by:
Hi All I need to compare date times from 2 different states in Australia in a Program I have which determines which way to update SQl server 2005 tables based on the lastupdate (datetime field)...
7
by: Damir | last post by:
I have a situation on a DB server where the "current timestamp" command shows the actual system time offset by one hour, which has never been noticed before?!? The DB2 is V9.1 (FP03), running on...
3
by: nonce999 | last post by:
Periodically time server returned from Apache (WinXP SO) to PHP is not Correct. So to restore must stop and restart. In logo, when the time is correct (14:02:12) appear: 127.0.0.1 - - When is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.