C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help! Need to make my own color...
Goto page Previous  1, 2, 3 ... 432, 433, 434 ... 448, 449, 450  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Malcolm McLean
Guest





PostPosted: Thu Jun 28, 2012 11:18 am    Post subject: Re: A question for this group... Reply with quote



בתאריך יום חמישי, 28 ביוני 2012 09:59:51 UTC+1, מאת nick_keigh...@hotmail.com:
Quote:
On Thursday, June 28, 2012 8:19:21 AM UTC+1, io_x wrote:

for me, i prefer a language without UBs

name one. C is actually pretty good.

Java has no UBs. A Java program is guaranteed to produce the same output on any Java Virtual machine. The guarantee has now been weakened to accomodate different floating point processors.
Back to top
Heinrich Wolf
Guest





PostPosted: Thu Jun 28, 2012 11:46 am    Post subject: Re: Converting from UTC to "local" time Reply with quote



"Heinrich Wolf" <invalid (AT) invalid (DOT) invalid> schrieb im Newsbeitrag
news:jshhnv$rog$1 (AT) news (DOT) m-online.net...
Quote:

"Heinrich Wolf" <invalid (AT) invalid (DOT) invalid> schrieb im Newsbeitrag
news:jsheco$igc$1 (AT) news (DOT) m-online.net...
...
For a portable version of timegm(), set the TZ environment variable
to UTC, call mktime(3) and restore the value of TZ.

The man page continues with a sample code for that. I tried it. The sample
code results in the same as timegm(). On my linux, getenv("TZ") results in
NULL. You have to call setenv("TZ", "", 1) and restore TZ with
unsetenv("TZ") in place of NULL.

...
I took the sample code, put a main() around it and did a very quick

successful test.
Here is my code:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
struct tm sZeit1,
sZeit2,
*pZeit;
time_t lZeit;
char *TZ;

if (argc != 3)
printf("%s dd.mm.YYYY hh:mm:ss\n"
"takes the arguments as GMT and prints the local time\n",
argv[0]);
else
{
memset(&sZeit1, 0, sizeof(sZeit1));
if (sscanf(argv[1], "%d.%d.%d", &sZeit1.tm_mday, &sZeit1.tm_mon,
&sZeit1.tm_year) != 3)
printf("%s is not dd.mm.YYYY\n", argv[1]);
else
{
sZeit1.tm_mon --;
sZeit1.tm_year -= 1900;
if (sscanf(argv[2], "%d:%d:%d", &sZeit1.tm_hour, &sZeit1.tm_min,
&sZeit1.tm_sec) != 3)
printf("%s is not hh:mm:ss\n", argv[2]);
else
{
memcpy(&sZeit2, &sZeit1, sizeof(sZeit2));
lZeit = timegm(&sZeit2);
printf("timegm(%s %s) = %ld\n", argv[1], argv[2], lZeit);
pZeit = localtime(&lZeit);
if (! pZeit)
printf("localtime(&%ld) is NULL\n", lZeit);
else
{
printf("localtime(&%ld) is %02d.%02d.%04d %02d:%02d:%02d\n",
lZeit,
pZeit->tm_mday,
pZeit->tm_mon + 1,
pZeit->tm_year + 1900,
pZeit->tm_hour,
pZeit->tm_min,
pZeit->tm_sec);
TZ = getenv("TZ");
if (! TZ)
printf("TZ = NULL\n");
else
printf("TZ = %s\n", TZ);
setenv("TZ", "", 1);
tzset();
lZeit = mktime(&sZeit2);
printf("GMT mktime(%s %s) = %ld\n", argv[1], argv[2], lZeit);
if (TZ)
setenv("TZ", TZ, 1);
else
unsetenv("TZ");
tzset();
}
}
}
}
return 0;
}
Back to top
Heinrich Wolf
Guest





PostPosted: Thu Jun 28, 2012 1:04 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote



"Heinrich Wolf" <invalid (AT) invalid (DOT) invalid> schrieb im Newsbeitrag
news:jshn74$ur8$1 (AT) news (DOT) m-online.net...
....
Quote:
I took the sample code, put a main() around it and did a very quick
successful test.
Here is my code:
....

This code worked on linux. But on Windows with Borland C++Builder 5 tzset()
did not change _timezone and _daylight after putenv(). So the result was off
by 1 hour. Instead of putenv(TZ=\"\"") I had to set both _timezone and
_daylight to 0. Then the results were the expected ones. I did not need to
save the old values of _timezone and _daylight. Calling tzset() restored
them.

Here is my new code (with #ifdef for both linux and C++Builder):
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#ifdef linux
#define _timezone timezone
#define _daylight daylight
#endif

int main(int argc, char *argv[])
{
struct tm sZeit1,
sZeit2,
*pZeit;
time_t lZeit;
char *TZ1,
*TZ2,
buf[80];

if (argc != 3)
printf("%s dd.mm.YYYY hh:mm:ss\n"
"takes the arguments as GMT and prints the local time\n",
argv[0]);
else
{
memset(&sZeit1, 0, sizeof(sZeit1));
if (sscanf(argv[1], "%d.%d.%d",
&sZeit1.tm_mday, &sZeit1.tm_mon, &sZeit1.tm_year) != 3)
printf("%s is not dd.mm.YYYY\n", argv[1]);
else
{
sZeit1.tm_mon --;
sZeit1.tm_year -= 1900;
if (sscanf(argv[2], "%d:%d:%d",
&sZeit1.tm_hour, &sZeit1.tm_min, &sZeit1.tm_sec) != 3)
printf("%s is not hh:mm:ss\n", argv[2]);
else
{
memcpy(&sZeit2, &sZeit1, sizeof(sZeit2));
#ifdef linux
lZeit = timegm(&sZeit2);
printf("timegm(%s %s) = %ld\n", argv[1], argv[2], lZeit);
#endif
TZ1 = getenv("TZ");
if (! TZ1)
puts("TZ = NULL");
else
printf("TZ = %s\n", TZ1);
#ifdef linux
putenv("TZ=");
TZ2 = getenv("TZ");
if (! TZ2)
puts("TZ = NULL");
else
printf("TZ = %s\n", TZ2);
#else
_daylight = 0;
_timezone = 0;
#endif
printf("_daylight = %d\n", _daylight);
printf("_timezone = %d\n", _timezone);
lZeit = mktime(&sZeit2);
printf("GMT mktime(%s %s) = %ld\n", argv[1], argv[2], lZeit);
if (TZ1)
{
strcpy(buf, "TZ=\"");
strncat(buf, TZ1, sizeof(buf) - strlen(buf) - 1);
strncat(buf, "\"", sizeof(buf) - strlen(buf) - 1);
putenv(buf);
}
else
#ifdef linux
unsetenv("TZ");
#else
putenv("TZ=");
#endif
tzset();
printf("_daylight = %d\n", _daylight);
printf("_timezone = %d\n", _timezone);
TZ2 = getenv("TZ");
if (! TZ2)
puts("TZ = NULL");
else
printf("TZ = %s\n", TZ2);
pZeit = localtime(&lZeit);
if (! pZeit)
printf("localtime(&%ld) is NULL\n", lZeit);
else
printf("localtime(&%ld) is %02d.%02d.%04d %02d:%02d:%02d\n",
lZeit,
pZeit->tm_mday,
pZeit->tm_mon + 1,
pZeit->tm_year + 1900,
pZeit->tm_hour,
pZeit->tm_min,
pZeit->tm_sec);
}
}
}
puts("please press ENTER");
fgets(buf, sizeof(buf), stdin);
return 0;
}
Back to top
Heinrich Wolf
Guest





PostPosted: Thu Jun 28, 2012 1:08 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

see also
news:481173 (AT) forums (DOT) embarcadero.com
that is
news:embarcadero.public.cppbuilder.language.cpp
subject "Converting from GMT to local time"
Back to top
io_x
Guest





PostPosted: Thu Jun 28, 2012 2:58 pm    Post subject: Re: A question for this group... Reply with quote

<nick_keighley_nospam (AT) hotmail (DOT) com> ha scritto nel messaggio
news:1df54e49-511e-4dc7-bb7a-ed23cf6a7d25 (AT) googlegroups (DOT) com...
Quote:
On Thursday, June 28, 2012 8:19:21 AM UTC+1, io_x wrote:

"yes UBs are ok...
it is question of master 1000 pages of standard for a language..."

I don't think the C standard is that large. Its a pretty slim and
readable docuemnt (not all language standards are). An most of the
UB is fairly obvious and that that isn't usually has a good reason.

for me, i prefer a language without UBs

name one. C is actually pretty good.

x86 32bit assembly

Quote:
or if it is possible in a set
more little it is possible...

don't understand that.

less UBs there are, better it is...
Back to top
Malcolm McLean
Guest





PostPosted: Thu Jun 28, 2012 8:07 pm    Post subject: Re: A question for this group... Reply with quote

בתאריך יום חמישי, 28 ביוני 2012 13:14:02 UTC+1, מאת Heikki Kallasjoki:
Quote:
On 2012-06-28, Malcolm McLean <malcolm.mclean5 (AT) btinternet (DOT) com> wrote:
For example,

System.out.println(java.io.File.separator);

is definitely not guaranteed to produce the same output on all Java
systems.

Presumably that's regarded as a class which is part of the program, rather than the program itself. If I write two methods called tan(), one of which performs the mathematical operation whilst another switches on a machine in a sun parlour, we wouldn't expect linking the two libraries to have idential results. That's true of almost every programming language.
Back to top
Andrew Cooper
Guest





PostPosted: Thu Jun 28, 2012 10:11 pm    Post subject: Re: A question for this group... Reply with quote

On 28/06/2012 17:58, io_x wrote:
Quote:
nick_keighley_nospam (AT) hotmail (DOT) com> ha scritto nel messaggio
news:1df54e49-511e-4dc7-bb7a-ed23cf6a7d25 (AT) googlegroups (DOT) com...
On Thursday, June 28, 2012 8:19:21 AM UTC+1, io_x wrote:

"yes UBs are ok...
it is question of master 1000 pages of standard for a language..."

I don't think the C standard is that large. Its a pretty slim and
readable docuemnt (not all language standards are). An most of the
UB is fairly obvious and that that isn't usually has a good reason.

for me, i prefer a language without UBs

name one. C is actually pretty good.

x86 32bit assembly

Pure 32bit assembly perhaps, but as soon as you introduce more than 1
processor, there is plenty of undefined and unpredictable behaviour to
be had.

~Andrew

Quote:

or if it is possible in a set
more little it is possible...

don't understand that.

less UBs there are, better it is...


Back to top
Gordon Burditt
Guest





PostPosted: Fri Jun 29, 2012 2:05 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

Quote:
I was trying to find a generic way to do this. Under Windows I had
this until a few days ago:

struct tm local_time = { 0 };
struct tm t; // t has time in UTC
time_t tt = _mkgmtime(&t);
localtime_s(&local_time, &tt);

This stopped working ever since Daylight savings came into effect last
month. In New York, we are now in EDT when is UTC-4. The code above is
always off by an hour.

I suspect this should work if you add t.tm_isdst = 0; to the code, given
a decent implementation of _mkgmtime(). Microsoft's C code (and compilers)
are generally pretty good as long as security isn't involved.

Quote:

Is there a generic way to write this so that it works no matter where
my code runs?

There are some Windows APIs that will do the same thing but I'd like
to first see if I can get this done via standard CRT functions.

can't you compute the difference between gmtime() and localtime() and use this value to do the conversion? It's a bit of a PITA because you have to manipulate struct tm-s

mktime() will do most of the hard work for you.

The problem comes if the zone correction takes you across a Daylight
Saving Time change, and you aren't sure whether what you got is an
offset with or without DST.

*IF* you could count on midnight, January 1 (or pick any other fixed
day/time of the year) *NEVER* having DST in effect, now or in the future,
and *IF* you could count on the time zone offset never changing for
other odd reasons (e.g. at 13:45 July 4, 2016, North Slobbovia was
annexed by East Slobbovia and changed its offset by 1 hour supposedly
permanently so the two match, and then at 20:03 13 September 2044
it broke free and changed back), you could calculate the difference
between the two at 00:00 1 Jan <relevant year> and use that as the
standard-time offset for adjusting a GMT timestamp.

Actually, January 1 is probably a bad choice because of leap seconds.

Take the GMT timestamp, correct tm_hour (we don't care if it goes
out of range: see definition of mktime()) by the standard time
offset we figured out above, set tm_isdst = 0 (that is, pretend
it's a STANDARD TIME local time stamp, and feed it to mktime()
(*NOT* _mkgmtime or timegm). This takes care of the mess propagating
changes, perhaps even into the year, and does Daylight Saving
calculations. Then use the resulting struct tm as local time.


Quote:

if your platform is giving you incorrect localtime then you need to fix *that*

I don't think that's the issue here.

Quote:

rant
DST is an invention of the devil- the sun *should* be at its zenith at noon.

I think DST is brought to you by the same people who wrote leash
laws for dogs, and when the law started causing problems, wrote a
law defining guide dogs for the blind as cats.

Quote:
/rant


toSeconds (const Tm* tm)
{
return tm->tm_sec + 60 * (tm->tm_min + 60 * (tm->tm_hour + 24 * tm->tm_yday));
}

I believe this will malfunction badly if the year in GMT and the
year in local time differ. This happens, for example, in the USA
in the evening of December 31 each year - for how many hours depends
on the specific time zone.

Quote:

// calculate local offset from UTC in seconds
long calcUtcOffset (const Tm* utc, const Tm* local)
{
long utcSecs = toSeconds (utc);
long locSecs = toSeconds (local);
return locSecs - utcSecs;
}
Back to top
Gordon Burditt
Guest





PostPosted: Fri Jun 29, 2012 3:13 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

Quote:
you need not bother with timezones and bias values at all to do this job.

You cannot do the job with the pieces you have indicated. "The job" here
is to take a struct tm filled in with a GMT time stamp and convert it
to a struct tm filled in with a local time stamp.

Quote:
You need only a few very useful functions from time.h .

time_t time(time_t *) gives the current time in seconds since Jan 1 1970
0:00:00 UTC

This is not useful for the required function. The GMT time stamp
may be taken from, say, a log file. It need not be the current
time.

Quote:
struct tm *localtime(time_t *) calculates the local time in a struct
(tm_year, tm_mon, tm_mday, ... tm_sec).
struct tm *gmtime(time_t *) calculates UTC in the same struct type.

We are given a struct tm filled in with GMT; we are not trying to generate one.

Quote:
Fill struct tm with a local time, set tm_isdst = -1 (unknown) and call
time_t mktime(struct tm *) to get the time_t.

I just have no idea, how to get time_t from a struct tm filled with UTC.

It would appear that you are proposing pieces of a solution to a
problem you didn't read carefully.

This is comp.lang.c. The C standard does not guarantee that:

A time_t is a "number of <anything> since <anything>" (although POSIX does).
A lot of implementations use this.

The functions _mkgmtime or timegm or any equivalent function exist.
Many implementations have at least 1 of these.

The external variables daylight, timezone, tzname, _daylight, or
_timezone exist (with time-related definitions). Some implementations
provide other alternatives, such as extra struct tm members.

The struct tm members tm_gmtoff and tm_zone exist.
Back to top
Heinrich Wolf
Guest





PostPosted: Fri Jun 29, 2012 3:30 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

"Gordon Burditt" <gordonb.ue3em (AT) burditt (DOT) org> schrieb im Newsbeitrag
news:LYidnRULJpPVf3DSnZ2dnUVZ_vadnZ2d (AT) posted (DOT) internetamerica...
Quote:
you need not bother with timezones and bias values at all to do this job.

You cannot do the job with the pieces you have indicated. "The job" here
is to take a struct tm filled in with a GMT time stamp and convert it
to a struct tm filled in with a local time stamp.

I'm, sorry. It took a few readings until I realized your problem.

....
Quote:
We are given a struct tm filled in with GMT; we are not trying to generate
one.

Fill struct tm with a local time, set tm_isdst = -1 (unknown) and call
time_t mktime(struct tm *) to get the time_t.

I just have no idea, how to get time_t from a struct tm filled with UTC.

It would appear that you are proposing pieces of a solution to a
problem you didn't read carefully.

You are right. My fault.

Quote:
This is comp.lang.c. The C standard does not guarantee that:

A time_t is a "number of <anything> since <anything>" (although POSIX
does).
A lot of implementations use this.

The functions _mkgmtime or timegm or any equivalent function exist.
Many implementations have at least 1 of these.

The external variables daylight, timezone, tzname, _daylight, or
_timezone exist (with time-related definitions). Some implementations
provide other alternatives, such as extra struct tm members.

The struct tm members tm_gmtoff and tm_zone exist.

I did not find tm_gmtoff in the compilers I use. These are Borland
C++Builder 5 and good old Turbo C 2.0 on Windows and gcc on fedora Linux.

Did you read, what I posted later? On Linux I found timegm() and a short
test revealed no problem. It also recommended not to use it. It suggested
and I tried using mktime() with a struct tm filled as GMT and having set the
timezone to GMT temporarily. There need to be some differences of the
implementation between gcc and Borland C++Builder 5, but I tested it
successfully.
Back to top
Gordon Burditt
Guest





PostPosted: Fri Jun 29, 2012 4:10 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

Quote:
This is comp.lang.c. The C standard does not guarantee that:

A time_t is a "number of <anything> since <anything>" (although POSIX
does).
A lot of implementations use this.

The functions _mkgmtime or timegm or any equivalent function exist.
Many implementations have at least 1 of these.

The external variables daylight, timezone, tzname, _daylight, or
_timezone exist (with time-related definitions). Some implementations
provide other alternatives, such as extra struct tm members.

The struct tm members tm_gmtoff and tm_zone exist.

I did not find tm_gmtoff in the compilers I use. These are Borland
C++Builder 5 and good old Turbo C 2.0 on Windows and gcc on fedora Linux.

As I said, tm_gmtoff need not exist.
Quote:

Did you read, what I posted later? On Linux I found timegm() and a short
test revealed no problem.

I tried that in the first post in this thread I made. I suggest
that the tm_isdst member was uninitialized in the OP's code (which
used _mkgmtime), causing the result to be off by an hour during
daylight saving time periods.

Quote:
It also recommended not to use it. It suggested

It's not standard and at the very least, exists under at least two
different names. Then again, no portable solution to the problem exists.

Quote:
and I tried using mktime() with a struct tm filled as GMT and having set the
timezone to GMT temporarily. There need to be some differences of the
implementation between gcc and Borland C++Builder 5, but I tested it
successfully.

The C standard also does not guarantee that there is a way to change
the current time zone of a running program. (BEWARE if you try
this in PHP: the time stamps in the Apache logs can look really
strange as changing the time zone has rather more wide-ranging
effect than desired).

Someone in this thread tried changing the environment variable TZ
(in C, there is no portable way to do even that, and I believe there
are two different definitions of putenv() with two different numbers
of arguments) and the change *did not take* - you had to call tzset()
or fiddle with other variables like timezone and daylight instead.
I don't think these variables are documented to do anything in
particular if you *write* to them. This is perhaps a good approach
if you need to translate timestamps in timezone X to timestamps in
timezone Y, neither of which has to be local or GMT.

It seems to me that in this situation, trying to change the time zone of
a program introduces much more unportability than it solves.

The most portable way I have found so far:

1. Find the *standard* time offset from GMT. Take the GMT struct
tm, make a copy, set month = January, day = 1, hour = 12, minute =
0, second = 0, isdst = 0, and use the existing year. Use mktime()
to get a time_t. You hope this comes back with isdst = 0. (At
this point you could go unportable and look at timezone, _timezone,
or tm_gmtoff).

Use localtime() and gmtime() to get a struct tm, and subtract the
two struct tm's to get the offset. You have to deal with more than
tm_hour to tell the difference between an offset of +12 and -12
hours. You only need to do this once in a program run.

This is vulnerable to odd things in the future like having Jan 1 on
DST, or random changes in offsets due to political upheval.

2. Add or subtract the offset from the given GMT struct tm (for the
USA you subtract) by adjusting tm_hour and if needed, tm_min. Set
tm_isdst = 0. Then call mktime() to get a time_t. Use localtime() to
get a local struct tm. This uses only portable functions.
Back to top
Heinrich Wolf
Guest





PostPosted: Fri Jun 29, 2012 4:25 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

Thank you very much for telling me things from the top of this thread. I
have resetted my newsreader not long ago. So I have no copy of this thread
before June 27th. As far as Windows is concerned, I mentioned the API call
GetTimeZoneInformation(). I believe, that is better than fiddling around
with Jan 1 and other assumptions about DST.
Back to top
Heinrich Wolf
Guest





PostPosted: Fri Jun 29, 2012 4:37 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

"Gordon Burditt" <gordonb.75ukl (AT) burditt (DOT) org> schrieb im Newsbeitrag
news:oYqdnU_EFrkacnDSnZ2dnUVZ_sudnZ2d (AT) posted (DOT) internetamerica...
....
Quote:
I tried that in the first post in this thread I made. I suggest
that the tm_isdst member was uninitialized in the OP's code (which
used _mkgmtime), causing the result to be off by an hour during
daylight saving time periods.
....

When filling structs for passing them to some library function, I have
acquired the habit of preparing the struct with memset(s, 0, sizeof(s));
Back to top
Keith Thompson
Guest





PostPosted: Fri Jun 29, 2012 5:19 pm    Post subject: Re: Converting from UTC to "local" time Reply with quote

"Heinrich Wolf" <invalid (AT) invalid (DOT) invalid> writes:
Quote:
Thank you very much for telling me things from the top of this thread. I
have resetted my newsreader not long ago. So I have no copy of this thread
before June 27th.
[...]


It appears that something recently happened to the comp.lang.c archives
on news.eternal-september.org; as far as I can tell, older articles are
no longer available there at all. I haven't seen the same effect in
other newsgroups. (Older articles can still be found through
groups.google.com or other servers.)

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to top
Robert Miles
Guest





PostPosted: Mon Jul 02, 2012 1:19 am    Post subject: Re: Programming "only" in an environment of C and it's frien Reply with quote

On 5/28/2012 10:06 AM, BartC wrote:
Quote:


"Malcolm McLean" <malcolm.mclean5 (AT) btinternet (DOT) com> wrote in message
news:d7483cfb-e299-415e-b286-11b9f4d05aa9 (AT) googlegroups (DOT) com...
בתאריך יום שני, 28 במאי 2012 15:25:49 UTC+1, מאת Bart:

Perhaps you need a more elaborate example where there isn't a trivial
way of
doing it in C...

Write a function which takes a function foo() as a parameter, and as
an output returns a function which is identical to foo(), except where
foo() produces nan on divide by zero, creates a very high value instead.

That sounds near impossible, in whatever language. Assuming you aren't
talking about a foo() merely returning a different floating point
result, but one that may or may not include floating point operations,
and if it does, then to alter the nature of all floating point
operations executed while in the lexical scope of foo()'s replacement.

Why not write a foo() wrapper, which calls foo(), then checks if it
returned a nan, and if so, change the result to a very high value?
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page Previous  1, 2, 3 ... 432, 433, 434 ... 448, 449, 450  Next
Page 433 of 450

 
 


Powered by phpBB © 2001, 2006 phpBB Group