473,395 Members | 2,253 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,395 software developers and data experts.

UTC conversion from different time zones

I have a long list of events. I know where each event took place (globally)
and when (using the event’s local time). I want to do some comparisons
between these date/times. I thought I would convert them all to UTC and thus
get them all on a common time-line. But I can’t see how to do this. I can use
TimeZone.CurrentTimeZone to get the current TimeZone object and use its
ToUniversalTime method to convert dates that occurred in the current time
zone, but I can’t see how to get a TimeZone object for any of the other time
zones?
Jun 30 '06 #1
7 2649
Dick,

I wished it was there, however it is probably impossible to do, timezone is
partialy declared by government, by instance India has its own timezone
which is UTC + 5:30 plus.

But even then it is difficult, beside that we have the Daylight saving time
(USA) or SummerTime (EU) or what ever, those starts and ends in the EU
completely at the same moments for the complete EU, but by instance the USA
which has an act for that, has that many irregulation on that: as well does
it often change..

http://en.wikipedia.org/wiki/Daylight_saving_time

To get universal time from a Net DateTime is however easy that is

DateTime.ToUniversalTime.

I hope this gives an idea,

Cor

"Dick" <Ri***********@nospam.nospam> schreef in bericht
news:E4**********************************@microsof t.com...
I have a long list of events. I know where each event took place (globally)
and when (using the event's local time). I want to do some comparisons
between these date/times. I thought I would convert them all to UTC and
thus
get them all on a common time-line. But I can't see how to do this. I can
use
TimeZone.CurrentTimeZone to get the current TimeZone object and use its
ToUniversalTime method to convert dates that occurred in the current time
zone, but I can't see how to get a TimeZone object for any of the other
time
zones?

Jun 30 '06 #2
Cor Ligthert [MVP] <no************@planet.nlwrote:
I wished it was there, however it is probably impossible to do, timezone is
partialy declared by government, by instance India has its own timezone
which is UTC + 5:30 plus.
It's feasible to do - Java does a reasonable job of it. .NET just has
pitiful support for other time zones. The operating system knows about
the, but there's no way (without using interop) of getting the list of
timezones. I believe there are freeware libraries available to do this,
but I haven't used any.

(Java libraries make dates/times a pain in other ways, but they at
least *allow* you to work with other timezones in a reasonable way.)
But even then it is difficult, beside that we have the Daylight saving time
(USA) or SummerTime (EU) or what ever, those starts and ends in the EU
completely at the same moments for the complete EU, but by instance the USA
which has an act for that, has that many irregulation on that: as well does
it often change..
Again, operating systems have to know about it anyway...
To get universal time from a Net DateTime is however easy that is

DateTime.ToUniversalTime.
That's fine so long as the DateTime represents the time in your local
timezone. It's a shame DateTime has little concept of which timezone
it's representing a date/time in. (.NET 2.0 is a little better than
..NET 1.1 in this, but not a lot.)

As an example of how broken this was in 1.1, try compiling the
following with the 1.1 compiler (so that it uses the 1.1 framework):

using System;

class Test
{
static void Main()
{
DateTime now = DateTime.Now;
for (int i=0; i < 5; i++)
{
Console.WriteLine (now);
now = now.ToUniversalTime();
}
}
}

The results right now on my box:
02/07/2006 14:30:17
02/07/2006 13:30:17
02/07/2006 12:30:17
02/07/2006 11:30:17
02/07/2006 10:30:17

Crazy, huh? (It makes sense when you realise how little idea DateTime
had of what was actually going on, but it still shows an oddity in the
framework.)

It's better in .NET 2.0 - after the first iteration the same time is
always returned, because it then "knows" that it's in UTC.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 2 '06 #3
Jon,

I find it not crazy it is everytime subtracting 1 hour in ticks from the
long in the datetime structure now.

You British have to long been in the lucky situation that GMT was the same
as British Time but you have now Summertime too.

:-)

I find the dateTime pretty good. However you have to know what a datetime
represents

nowInUTC = Now.ToUniversalTime

And than is NowInUTC again a regular date for what the ToUniversalTime will
act on your system settings conform the method.

I am almost sure that you know that because otherwise you would not have
used now (lowercase) so consequent. I think that you thought that I would
not see that.

:-)

About making showable in a sufficient way the TimeZone tables with their
regions do we agree completely.

However I don't see the TimeZone in not any way as a part of the DateTime in
Net. It is additional information to use according the DateTime. This is
another situation with a given date and time from by instance a SQL server
or an webpage. In that is the timezone very much needed. Unlucky enough it
is not given by both (in a standard way). In Net the datetime is only a
temporally value in a program and therefore it is not important where it is
created (as long as you can use it as an UTC time what is very simple
because the ToUniversalTime. In my idea should you never use this to create
another datetime).

Just my thought,

Cor
"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP************************@msnews.microsoft.c om...
Cor Ligthert [MVP] <no************@planet.nlwrote:
>I wished it was there, however it is probably impossible to do, timezone
is
partialy declared by government, by instance India has its own timezone
which is UTC + 5:30 plus.

It's feasible to do - Java does a reasonable job of it. .NET just has
pitiful support for other time zones. The operating system knows about
the, but there's no way (without using interop) of getting the list of
timezones. I believe there are freeware libraries available to do this,
but I haven't used any.

(Java libraries make dates/times a pain in other ways, but they at
least *allow* you to work with other timezones in a reasonable way.)
>But even then it is difficult, beside that we have the Daylight saving
time
(USA) or SummerTime (EU) or what ever, those starts and ends in the EU
completely at the same moments for the complete EU, but by instance the
USA
which has an act for that, has that many irregulation on that: as well
does
it often change..

Again, operating systems have to know about it anyway...
>To get universal time from a Net DateTime is however easy that is

DateTime.ToUniversalTime.

That's fine so long as the DateTime represents the time in your local
timezone. It's a shame DateTime has little concept of which timezone
it's representing a date/time in. (.NET 2.0 is a little better than
.NET 1.1 in this, but not a lot.)

As an example of how broken this was in 1.1, try compiling the
following with the 1.1 compiler (so that it uses the 1.1 framework):

using System;

class Test
{
static void Main()
{
DateTime now = DateTime.Now;
for (int i=0; i < 5; i++)
{
Console.WriteLine (now);
now = now.ToUniversalTime();
}
}
}

The results right now on my box:
02/07/2006 14:30:17
02/07/2006 13:30:17
02/07/2006 12:30:17
02/07/2006 11:30:17
02/07/2006 10:30:17

Crazy, huh? (It makes sense when you realise how little idea DateTime
had of what was actually going on, but it still shows an oddity in the
framework.)

It's better in .NET 2.0 - after the first iteration the same time is
always returned, because it then "knows" that it's in UTC.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 2 '06 #4
Cor Ligthert [MVP] <no************@planet.nlwrote:
I find it not crazy it is everytime subtracting 1 hour in ticks from the
long in the datetime structure now.
But if you take something which is already UTC (because you've called
ToUniversalTime) then "converting" it into UTC should be a no-op -
which it is in .NET 2.0.
You British have to long been in the lucky situation that GMT was the same
as British Time but you have now Summertime too.

:-)

I find the dateTime pretty good. However you have to know what a datetime
represents

nowInUTC = Now.ToUniversalTime

And than is NowInUTC again a regular date for what the ToUniversalTime will
act on your system settings conform the method.

I am almost sure that you know that because otherwise you would not have
used now (lowercase) so consequent. I think that you thought that I would
not see that.
No, I didn't think that at all. The problem is that on .NET 1.1, a
DateTime had no idea whether it was a local time or a universal time -
so calling ToUniversalTime only made sense if the *caller* knew that it
was a local time to start with. It's better in .NET 2.0, in that it
knows the difference between local and UTC - but it only has the
concept of "local" rather than being for a particular TimeZone.
About making showable in a sufficient way the TimeZone tables with their
regions do we agree completely.

However I don't see the TimeZone in not any way as a part of the DateTime in
Net. It is additional information to use according the DateTime.
That's because the .NET libraries have been badly designed to be that
way. In Java there is a Calendar (which knows which TimeZone it's in,
along with things like what type of calendar it is (Gregorian etc)) and
a Date which is just a number of milliseconds since a particular UTC
time. The two are kept quite separate. .NET mixes them up by having
something which is essentially calendar-based, but with no idea of time
zone.
This is
another situation with a given date and time from by instance a SQL server
or an webpage. In that is the timezone very much needed. Unlucky enough it
is not given by both (in a standard way). In Net the datetime is only a
temporally value in a program and therefore it is not important where it is
created (as long as you can use it as an UTC time what is very simple
because the ToUniversalTime. In my idea should you never use this to create
another datetime).
Unfortunately it's very easy to get it wrong, and so silently - if you
happen to try to convert something to universal time twice, in .NET 1.1
you're pretty much bound to create a bug. Now, how many APIs for .NET
actually specify whether any DateTimes that you pass in or return are
in the local time or in UTC? Some of the framework ones do, but I
suspect most 3rd party libraries are much laxer. This is a fundamental
problem with the way DateTime was designed, and now of course it's too
late to fix it properly. There are half-fixes such as the local/UTC one
in 2.0, but it's still basically wrong.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 2 '06 #5
Jon,

I can be wrong, but in my idea is the time in Net not real calendar based.
There is a calendar class which makes it possible to convert.

However the time in net starts at 01-01-01 00:00:00

The British Empire changed from Julian to Gregorian in 1753 (that was then
including the colonies in North America). AFAIK were they as normal the
last from the than existing European countries which did that change. This
means that every date before that time is incorrect. Therefore the startdate
for me in Net is a moment, which is relative in time.

However as I said, just as far as I know.

Cor

"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP************************@msnews.microsoft.c om...
Cor Ligthert [MVP] <no************@planet.nlwrote:
>I find it not crazy it is everytime subtracting 1 hour in ticks from the
long in the datetime structure now.

But if you take something which is already UTC (because you've called
ToUniversalTime) then "converting" it into UTC should be a no-op -
which it is in .NET 2.0.
>You British have to long been in the lucky situation that GMT was the
same
as British Time but you have now Summertime too.

:-)

I find the dateTime pretty good. However you have to know what a datetime
represents

nowInUTC = Now.ToUniversalTime

And than is NowInUTC again a regular date for what the ToUniversalTime
will
act on your system settings conform the method.

I am almost sure that you know that because otherwise you would not have
used now (lowercase) so consequent. I think that you thought that I
would
not see that.

No, I didn't think that at all. The problem is that on .NET 1.1, a
DateTime had no idea whether it was a local time or a universal time -
so calling ToUniversalTime only made sense if the *caller* knew that it
was a local time to start with. It's better in .NET 2.0, in that it
knows the difference between local and UTC - but it only has the
concept of "local" rather than being for a particular TimeZone.
>About making showable in a sufficient way the TimeZone tables with their
regions do we agree completely.

However I don't see the TimeZone in not any way as a part of the DateTime
in
Net. It is additional information to use according the DateTime.

That's because the .NET libraries have been badly designed to be that
way. In Java there is a Calendar (which knows which TimeZone it's in,
along with things like what type of calendar it is (Gregorian etc)) and
a Date which is just a number of milliseconds since a particular UTC
time. The two are kept quite separate. .NET mixes them up by having
something which is essentially calendar-based, but with no idea of time
zone.
>This is
another situation with a given date and time from by instance a SQL
server
or an webpage. In that is the timezone very much needed. Unlucky enough
it
is not given by both (in a standard way). In Net the datetime is only a
temporally value in a program and therefore it is not important where it
is
created (as long as you can use it as an UTC time what is very simple
because the ToUniversalTime. In my idea should you never use this to
create
another datetime).

Unfortunately it's very easy to get it wrong, and so silently - if you
happen to try to convert something to universal time twice, in .NET 1.1
you're pretty much bound to create a bug. Now, how many APIs for .NET
actually specify whether any DateTimes that you pass in or return are
in the local time or in UTC? Some of the framework ones do, but I
suspect most 3rd party libraries are much laxer. This is a fundamental
problem with the way DateTime was designed, and now of course it's too
late to fix it properly. There are half-fixes such as the local/UTC one
in 2.0, but it's still basically wrong.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 2 '06 #6
Cor Ligthert [MVP] <no************@planet.nlwrote:
I can be wrong, but in my idea is the time in Net not real calendar based.
Well, it's certainly not "instant in time" based either - because that
would require knowing the timezone. Basically, it's not well specified
exactly what a particular DateTime really means.
There is a calendar class which makes it possible to convert.

However the time in net starts at 01-01-01 00:00:00

The British Empire changed from Julian to Gregorian in 1753 (that was then
including the colonies in North America). AFAIK were they as normal the
last from the than existing European countries which did that change. This
means that every date before that time is incorrect. Therefore the startdate
for me in Net is a moment, which is relative in time.

However as I said, just as far as I know.
But what it's relative to depends on its use, which doesn't make it
terribly useful. I've written quite a few apps which require working
with this kind of information, and it's *always* a pain to work out
exactly what's required, whatever language you're using - but at least
Java has the right principles in its classes. With .NET 1.1, you don't
know what a partiucular DateTime is meant to be - UTC, local, or with
respect to some other timezone. .NET 2.0 is slightly better, but only
slightly IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 2 '06 #7
Hi Richard,

Thanks for your post!

Yes, I see your concern. TimeZone class does not expose an interface for
retrieving a specific timezone object instance.

After performing some search internally, I found that this is a known issue
in this version. Enumerating TimeZone's is actually quite difficult because
of naming issues and areas that use non-hour offsets from UTC. Our product
team is planning on adding this functionality in the next version, such as
adding a method named GetTimeZones(). For now, .Net FCL only support UTC
and the CurrentTimeZone.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 3 '06 #8

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

Similar topics

6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
3
by: JJ | last post by:
Hi, Is there an asp component anywhere which will convert a GMT time in the past to a different time zone e.g. EST? You can't just take the hours off as there is often daylight saving hours to...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system time. This will change with system time as we travel...
0
by: Sabotage | last post by:
Hi all, I am looking to download time zones list for my multilingual software. I found time zones list in english here:...
1
by: AnshuGupta | last post by:
Hello, I have an application coded in .Net c# that has to display the list of available time zones. This application supports both english and french language. Is it possible to display the list...
10
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one...
5
by: Ivan Velev | last post by:
Hello, Minimal example below - it gives me different output if I comment / uncomment the extra time.mktime call - note that this call is not related in any way to main logic flow. When...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.