473,785 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Curren tTimeZone 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 2683
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.ToUniv ersalTime.

I hope this gives an idea,

Cor

"Dick" <Ri***********@ nospam.nospam> schreef in bericht
news:E4******** *************** ***********@mic rosoft.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.Curren tTimeZone 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.ToUniv ersalTime.
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.WriteLi ne (now);
now = now.ToUniversal Time();
}
}
}

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.co m>
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.ToUniversal Time

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.co mschreef in bericht
news:MP******** *************** *@msnews.micros oft.com...
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.ToUni versalTime.

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.WriteLi ne (now);
now = now.ToUniversal Time();
}
}
}

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.co m>
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.ToUniversal Time

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.co m>
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.co mschreef in bericht
news:MP******** *************** *@msnews.micros oft.com...
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.ToUniversal Time

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.co m>
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.co m>
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
12950
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 here put me straight on the following: I had a look at the time() function came across this: "To clarify, it seems this function returns the time of the computer's clock and does not do any timezone adjustments to return GMT, so you are...
3
4448
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 take into consideration. Thanks JJ
11
4678
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 having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
1
11688
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 field. I need the converted time to show up in a report. I also need the corresponding date to increment if necessary. Any ideas on how to do this would be greatly appreciated.
6
2853
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 across the country. txtRaceTime will always be set to Central Time regardless of where we are in the US. At first I tried ‘Now() – “xx:xx:xx” but that produced an error. Anyway as we approach the east coast the ‘minus’ would need to be a...
0
1745
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wpossetup/htm/microsofttimezoneindexvalues.asp Could anybody advice where could I download time zones list in french, italian, etc? I googled a lot but without any success. Thanks a lot.
1
2480
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 of time zones in different languages (english or french)? The Windows OS is in english. Thanks
10
4026
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 problem. This function does not recognize time zones. How to adjust date to user's time zone? Is converting to timestamp() and then to format readable to visitors the one and only solution (e.g. strtotime() + date() OR DateTime object)? Perhaps,...
5
2123
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 "problematicStamp = ..." is commented I get gmtStamp: 1130634600.0
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.