Converting to TimeZone

J

Jonathan Wood

I have a DateTime value that represents a UTC time. I also have a UTC offset
value (double) and a Daylight Savings Time value (bool) for a local time.
How best to calculate the local time?

I've been searching the web for a while now. I've found a lot of interesting
new classes and various ways to approach this. But not one is dealing with
the type of data I have.

More Info:

I'm building a website where people can post comments. The date of each
comment is stored in the database as UTC time. My site also needs to track
which region each user is from. So my region table contains time zone
information which I need to apply to the UTC time. I'm opening to storing
different data types, etc. But I don't want to make this much more
complicated.

Thanks.

Jonathan
 
P

Peter Duniho

Jonathan said:
I have a DateTime value that represents a UTC time. I also have a UTC
offset value (double) and a Daylight Savings Time value (bool) for a
local time. How best to calculate the local time?

Minor nit: it's "Daylight Saving Time", not "Daylight Savings Time".

Warning: not every timezone that uses daylight saving has the same rules
for daylight saving. Start and stop dates and times vary, as does the
amount of time the clock is adjusted (at least historically...I don't
know the exact status at this moment, but there have been places where
daylight saving is only a 30 minute shift, for example).
I've been searching the web for a while now. I've found a lot of
interesting new classes and various ways to approach this. But not one
is dealing with the type of data I have.

I don't understand the comment "not one is dealing with the type of data
I have". Are you looking for something that is strictly compatible with
the exact types (System.DateTime, System.Double, and System.Boolean)
you're using? Or is there some more basic data characteristic that
doesn't match?
More Info:

I'm building a website where people can post comments. The date of each
comment is stored in the database as UTC time. My site also needs to
track which region each user is from. So my region table contains time
zone information which I need to apply to the UTC time. I'm opening to
storing different data types, etc. But I don't want to make this much
more complicated.

For what it's worth, one of the more common web site implementations
I've seen pretty much leaves that up to the user. There's a basic "time
zone offset" setting (UTC +/- some value), and a checkbox saying whether
daylight saving is in effect or not. The web site always adjusts for
daylight saving by one hour, regardless of the actual rules for the user
location. It's up to the user to change the daylight saving status as
needed.

Obviously it's some additional work for the user. But it keeps the code
dirt simple.

If you are using .NET 3.5 or later, and you want to do automatic
conversions, the perhaps the System.TimeZoneInfo class would be helpful
to you. If it's not, then your question would be more complete and
understandable if you would explain why TimeZoneInfo doesn't address
your needs.

Pete
 
J

Jonathan Wood

Peter Duniho said:
I don't understand the comment "not one is dealing with the type of data I
have". Are you looking for something that is strictly compatible with the
exact types (System.DateTime, System.Double, and System.Boolean) you're
using? Or is there some more basic data characteristic that doesn't
match?

I just want to convert a datetime to a local time using the data types
mentioned.

If you have code that uses slightly different data types, I'd be happy to
take a look. But if it uses, for example, a TimeZoneInfo, then I need an
efficient way to obtain that because, as explained previously, this is not
the type of data I'm working from.
For what it's worth, one of the more common web site implementations I've
seen pretty much leaves that up to the user. There's a basic "time zone
offset" setting (UTC +/- some value), and a checkbox saying whether
daylight saving is in effect or not. The web site always adjusts for
daylight saving by one hour, regardless of the actual rules for the user
location. It's up to the user to change the daylight saving status as
needed.

Obviously it's some additional work for the user. But it keeps the code
dirt simple.

When I designate a new region, it's a pretty simple matter for me to decide
then if that region is subject to DST. I don't see any advantage to
requiring the user to specify this.
If you are using .NET 3.5 or later, and you want to do automatic
conversions, the perhaps the System.TimeZoneInfo class would be helpful to
you. If it's not, then your question would be more complete and
understandable if you would explain why TimeZoneInfo doesn't address your
needs.

I don't know. Why/how does TimeZoneInfo address my needs. Given the data I
have, I couldn't really see.

Jonathan
 
P

Peter Duniho

Jonathan said:
[...]
When I designate a new region, it's a pretty simple matter for me to
decide then if that region is subject to DST. I don't see any advantage
to requiring the user to specify this.

I doubt it's as simple a matter as you think to decide if a region is
subject to DST.

For each geographical location, there is not just a "is this location
subject to DST or not?" to be answered. You need to know on what dates
_and times_ DST is and is not in effect, taking into account those dates
and times change every year, following a complex and arbitrary set of
rules (after all, they are generally set by organizations like the US
Congress), and if daylight saving is in effect at that location on that
particular date, what the offset is.
I don't know. Why/how does TimeZoneInfo address my needs. Given the data
I have, I couldn't really see.

That depends on what's causing you problems. So far, you've been vague
about that.

Taken literally, your question appears irresolvable. You don't appear
to have time-zone information, but somehow you've managed to get a
boolean indicating whether daylight saving is in effect. That alone
seems broken, but without time-zone information the best you can do is
assume that when daylight saving time is in effect (your boolean is
"true"), the local time is one hour later than normal.

The obvious solution is to add the offset to your UTC time, and one more
hour if the daylight saving time is set. The DateTime class alone is
sufficient for accomplishing that much. But the result is not
necessarily going to be reliable. It will work fine for most of your
users, but may fail to produce correct results for some.

Since you are asking the question, it seems that the obvious solution
isn't appropriate for your needs. But so far, nothing in your posts
have described any particular reason why that might be. Lacking that
information, it's going to be quite difficult to come up with a precise
and useful answer for you.

Pete
 
J

Jonathan Wood

Peter Duniho said:
For each geographical location, there is not just a "is this location
subject to DST or not?" to be answered. You need to know on what dates
_and times_ DST is and is not in effect, taking into account those dates
and times change every year, following a complex and arbitrary set of
rules (after all, they are generally set by organizations like the US
Congress), and if daylight saving is in effect at that location on that
particular date, what the offset is.

Well, I know one class had a method that indicated if a particular date fell
within DST or not. I don't know exactly how it works but there are time zone
classes that deal with DST. I've yet to see a website that requires me to
specifically specify if DST is currently in effect each time I log on. So,
for my purposes, it seems like it should be good enough.
Since you are asking the question, it seems that the obvious solution
isn't appropriate for your needs. But so far, nothing in your posts have
described any particular reason why that might be. Lacking that
information, it's going to be quite difficult to come up with a precise
and useful answer for you.

I really don't know how to respond to this. Going back to my original post,
I just don't see any way to make it more clear what I'm trying to do. If you
response is that it can't be done, then I think that response is a little
bit suspect. But perhaps someone else can shed some more light on this.

Thanks.

Jonathan
 
P

Peter Duniho

Jonathan said:
Well, I know one class had a method that indicated if a particular date
fell within DST or not.

System.TimeZoneInfo does it too. But you specifically said that class
didn't address your need. (And remember: it's not just the date that's
important...it's a specific date _and time_ that may or may not be
subject to a daylight saving adjustment).
I don't know exactly how it works but there are
time zone classes that deal with DST.

There are lots of implementations. Google can show a large number of
them. They all rely on the same basic strategy: maintain a database of
global time zone and daylight saving rules and apply that information to
specific dates and times to produce the desired results.

That's what TimeZoneInfo does too.
I've yet to see a website that
requires me to specifically specify if DST is currently in effect each
time I log on.

I never said it was something someone needed to specify each time they
log on. Simply that there's a setting in the user profile that the user
is responsible for managing. They need not ever touch it if they don't
want to (in fact, I generally don't when I'm using web sites like
that...all I really need is to see messages in order, and the exact
timestamp on a message is irrelevant to me; they could show all the
times in UTC for all I care).
So, for my purposes, it seems like it should be good enough.

What should be "good enough"? It's a lot more work to properly maintain
time zone and daylight saving information automatically than to have the
user do it. I would think that if anything is "good enough", it would
be the simpler solution.
I really don't know how to respond to this.

You could provide the elaborations I've suggested you provide. You say
that TimeZoneInfo doesn't address your needs, and you say that DateTime
doesn't address your needs. But in neither case have you explained why not.

So far, all you've really asked is "how do I add an offset and
optionally an hour to a given UTC time?" I've answered that (you just
use the DateTime members that allow you to do math on a DateTime), but
it doesn't seem satisfactory to you. If you want an answer to the
question that's really on your mind, you do need to ask that question.
So far, all indications are that you haven't.
Going back to my original
post, I just don't see any way to make it more clear what I'm trying to
do. If you response is that it can't be done,

What I've said "can't be done" is to correctly determine the actual time
zone- and daylight saving time-adjusted time value given only the UTC
time, the UTC offset, and a boolean.

Whether that's what you're asking to do, I don't know. You don't seem
willing to clarify your question. If that is in fact what you're
asking, then yes...it's not possible to do with just the information you
say you have.

If that's not what you're asking, then I haven't said what you're asking
can't be done, because I don't even know what you're asking.
then I think that response
is a little bit suspect. But perhaps someone else can shed some more
light on this.

Perhaps. In the meantime, consider the fact that your own impression of
the clarity of your question is irrelevant. If someone else says it's
not clear, then it's not clear. If you want an answer, you need to work
to successfully convey whatever information it is you're thinking of in
words that can be understood by the other person.

If someone else stumbles across the correct meaning of your original
question, great. They can answer it for you. But as far as I can see,
so far your question is really basically asking "how do I add two
numbers?" Since the answer to that question is obvious, it doesn't seem
like anyone would ask that question and I have to assume you are asking
some other question. But I can't guess what that question is. I'm not
a good enough mind-reader for that.

Pete
 
J

Jonathan Wood

Peter Duniho said:
System.TimeZoneInfo does it too. But you specifically said that class
didn't address your need.

No, I did not. In fact, I recall specifically stating that I was willing to
look at any other option that could be efficient done from where I was
starting--which has now been described in detail several times.
I never said it was something someone needed to specify each time they log
on.

You said a check box to say if it was DST at that particular time. Since
they could log on at different times, I don't see how this could be of any
value at all unless they have to keep checking it each time.
You could provide the elaborations I've suggested you provide. You say
that TimeZoneInfo doesn't address your needs, and you say that DateTime
doesn't address your needs. But in neither case have you explained why
not.

Where have I said either of those things?
So far, all you've really asked is "how do I add an offset and optionally
an hour to a given UTC time?" I've answered that (you just use the
DateTime members that allow you to do math on a DateTime), but it doesn't
seem satisfactory to you.

I don't recall you saying that, but I don't know how to do that. In fact,
I'm not sure I've seen anything related to DST in DateTime. So if it does
this just fine, then this is simply information you are withholding while
you continue to tell me to provide more details and accuse me of saying this
class won't work.
If you want an answer to the question that's really on your mind, you do
need to ask that question. So far, all indications are that you haven't.

It's not clear to me why you can't accept the question I asked is the one
I'm trying to get answered.

Jonathan
 
P

Peter Duniho

Jonathan said:
No, I did not. In fact, I recall specifically stating that I was willing
to look at any other option that could be efficient done from where I
was starting--which has now been described in detail several times.

From your previous post: "Why/how does TimeZoneInfo address my needs.
Given the data I have, I couldn't really see." In other words,
TimeZoneInfo doesn't (according to you) address your needs.

I am trying to explain to you that you have not yet sufficiently
explained your needs. So we are left trying to figure out exactly what
your needs are. One way of doing that is pointing to a specific
solution and asking why that solution _does not_ address your needs.

You say you've looked at TimeZoneInfo and "couldn't really see" how it
addresses your needs. But that necessarily implies that you have
specific reason to believe it does _not_ address your needs.

It's not like I'm asking why the RichTextBox control doesn't address
your time zone-related calculation needs. That's trivially not
addressing your needs, and we'd get no information from hearing why it
doesn't address your needs.

I'm talking about a class specifically designed to deal with time-zone
issues. Inasmuch as the TimeZoneInfo class does address a number of
time-zone issues, by explaining why none of those features solve your
particular problem you can shed light on what your particular problem _is_.
You said a check box to say if it was DST at that particular time. Since
they could log on at different times, I don't see how this could be of
any value at all unless they have to keep checking it each time.

A check box that indicates whether the times should be adjusted for DST
has the same value that, for example, an edit field that indicates the
user handle that should be displayed with messages, or that allows the
user to enter a new password, or any profile setting that allows the
user to set any other persistent state for that user's profile.

In other words: the user sets the checkbox as desired, and from that
point on until the user changes it, times are displayed to the user
using the DST adjustment indicated (i.e. either adjusted or not).

It may well be that this has nothing to do with your particular problem.
But since we still don't know what your particular problem is, all I
can do is guess and offer information that _might_ help.

If you don't think it's helpful, that's fine. But debating whether the
general feature is useful is pointless. It obviously is, because it's a
common enough feature to find on web sites.
Where have I said either of those things?

See above for my detailed explanation of how you told us that
TimeZoneInfo doesn't address your needs. As for DateTime, you explained
in your first post that you're using the DateTime struct; the mere fact
that you're even asking the question indicates that the DateTime struct
also doesn't address your needs.
I don't recall you saying that,

I wrote: "The obvious solution is to add the offset to your UTC time,
and one more hour if the daylight saving time is set. The DateTime
class alone is sufficient for accomplishing that much".
but I don't know how to do that.

Have you looked at the documentation for the System.DateTime struct? It
provides everything anyone would need to understand how to do math
related to the DateTime struct. Including adding hours to a given
DateTime value.

For your convenience:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx
In fact, I'm not sure I've seen anything related to DST in DateTime.

There's not anything related to DST in DateTime. But there's nothing in
your post that suggests that "DST" is anything more than "optionally add
an extra hour". The DateTime struct doesn't need to know anything about
DST for you to add an extra hour to it.
So if
it does this just fine, then this is simply information you are
withholding while you continue to tell me to provide more details

I'm not withholding any information at all. And how I could "withhold
information" AND simultaneously "provide more details" is beyond me.
That sounds like a logical impossibility to me.
and accuse me of saying this class won't work.

I haven't "accused" you of anything. I've simply stated facts, readily
evident by the text found in this thread.
It's not clear to me why you can't accept the question I asked is the
one I'm trying to get answered.

Because so far, you haven't asked a question that can be answered.

Pete
 
A

Arne Vajhøj

Jonathan said:
I have a DateTime value that represents a UTC time. I also have a UTC
offset value (double) and a Daylight Savings Time value (bool) for a
local time. How best to calculate the local time?

I've been searching the web for a while now. I've found a lot of
interesting new classes and various ways to approach this. But not one
is dealing with the type of data I have.

Typically you have the time zone in which case you can do:

public static DateTime Convert(DateTime dt, TimeZoneInfo tzi)
{
return TimeZoneInfo.ConvertTimeFromUtc(dt, tzi);
}

But if you for whatever reason has standard offset + know whether in
DST or not + know DST offset (usually 1 hour, but you need to knw), then
you can use:

public static DateTime Convert(DateTime dt, int stdoffset, bool
indst, int dstoffset)
{
return dt.AddSeconds(stdoffset + (indst ? dstoffset : 0));
}

Arne
 
J

Jonathan Wood

Peter Duniho said:
You say you've looked at TimeZoneInfo and "couldn't really see" how it
addresses your needs. But that necessarily implies that you have
specific reason to believe it does _not_ address your needs.

I'm sorry. I think there is a problem with your logic.

Jonathan
 
J

Jonathan Wood

Arne Vajhøj said:
Typically you have the time zone in which case you can do:

public static DateTime Convert(DateTime dt, TimeZoneInfo tzi)
{
return TimeZoneInfo.ConvertTimeFromUtc(dt, tzi);
}

Well, I sort of have the time zone, just not in the form of a TimeZoneInfo
object.
But if you for whatever reason has standard offset + know whether in
DST or not + know DST offset (usually 1 hour, but you need to knw), then
you can use:

public static DateTime Convert(DateTime dt, int stdoffset, bool
indst, int dstoffset)
{
return dt.AddSeconds(stdoffset + (indst ? dstoffset : 0));
}

Don't you mean AddHours? But, no, I don't readily know whether or not DST
should be applied.

I have a few ideas on how I might approach the issue and I could always
research all the different time zones and when/how they apply DST. It just
seemed like support for this was already in .NET.

Thanks.

Jonathan
 
A

Arne Vajhøj

Jonathan said:
Well, I sort of have the time zone, just not in the form of a
TimeZoneInfo object.

Can what you have be used to identify the timezone ?
Don't you mean AddHours?

No. Because I were using seconds not hours.
But, no, I don't readily know whether or not
DST should be applied.

According to your original post you had a:
Daylight Savings Time value (bool)
I have a few ideas on how I might approach the issue and I could always
research all the different time zones and when/how they apply DST.

It does not work that way.

All timezones with a given offset from UTC does not change to and from
DST at the same time.
It
just seemed like support for this was already in .NET.

I think there is a very good chance that there are support in .NET.

But you need to tell us what information you have.

Arne
 
P

Peter Duniho

Arne said:
[...]
But, no, I don't readily know whether or
not DST should be applied.

According to your original post you had a:
Daylight Savings Time value (bool)

It's pretty clear he doeesn't really know what he has. He can't even
explain in a direct, specific way what his inputs and outputs are.
It does not work that way.

All timezones with a given offset from UTC does not change to and from
DST at the same time.

True. And I already explained that to him. He doesn't seem to care.
I think there is a very good chance that there are support in .NET.

But you need to tell us what information you have.

Been there, done that. He's not going to.

Personally, I believe that the TimeZoneInfo class is _the_ .NET class
for dealing with time zones and DST and the one Jonathan ought to be
using. It's the one object in the framework with access to the time
zone/DST database in Windows that would allow for trivial enumeration of
time zones (so a user can select their appropriate one) and conversion
to and from UTC (so the correct local time can be displayed to the user).

But Jonathan claims to have already looked at TimeZoneInfo and claims
that it doesn't suit his needs. Your reply simply enumerated in code
the things I'd already told him, and he's now rejected those suggestions
twice (once in each form).

It's hard to help someone who refuses to even help themselves.

Pete
 
J

Jonathan Wood

Peter Duniho said:
It's hard to help someone who refuses to even help themselves.

Although I haven't been using .NET all that many years, I've been a
developer for about 23 years. You can rest assured that this will be
accomplished whether you understand my approach or note.

Your initial unhelpful response was one thing, but when you then decided
that me saying I couldn't really see how a particular class could help me
necessarily meant that I was saying it couldn't. There a lot of things in
the Universe that I can't really see how they do what they do, that's a long
way from me saying they can't do it. But apparently that logic is beyond
your grasp. So fine, I can't communicate with you, so I'll move on. But
that's not enough for you. Now you want to interject yourself in other
people's posts to me.

There's something wrong with you. Obviously I have some limits in my
understanding on this particular subject but you have demonstrated quite
satisfactorily that you do not understand my approach or even some basic
statements, you are hardly in a position to judge my approach. So why not
just let it go?

Jonathan
 
J

Jonathan Wood

Arne Vajhøj said:
Can what you have be used to identify the timezone ?

I'm sorry. I thought I had been very clear about what I had. At this point
in time, I have a UTC offset and a DST flag. Does that identify a time zone?
If not, what other information do I need? And if the only way in the
Universe to calculate a local time requires a TimeZoneInfo object, what SQL
Server data type does that correspond to?
According to your original post you had a:
Daylight Savings Time value (bool)

Yes, I said that because that was the truth. But a DST flag only tells me if
the location observes DST. It doesn't tell me if DST is in effect at that
particular date.
It does not work that way.

All timezones with a given offset from UTC does not change to and from
DST at the same time.

Well, if I researched all the different time zones and when/how they applied
DST, then I'd know the times they changed, no?
But you need to tell us what information you have.

Can you clarify what part of a date (DateTime), a UTC offset (double), and a
DST flag (bool) I have been unclear on? Thanks.
 
F

Family Tree Mike

Jonathan said:
Well, if I researched all the different time zones and when/how they
applied DST, then I'd know the times they changed, no?

But then, wouldn't you be recreating the timezoneinfo class? It seems
to me that you don't understand two or more timezoneinfo places may
equate to the same offset and DST boolean flag in your current scheme.
You might need to revisit how Windows asks the user what timezone their
computer resides.
 
J

Jonathan Wood

Family Tree Mike said:
But then, wouldn't you be recreating the timezoneinfo class?

I'm not entirely sure. Either way, it doesn't seem like the best way to
go--I still suspect there is an easier approach.

I've found some examples that I'm looking into. I think I'm getting close.
But a good overview by someone very knowledgeable on the subject is not
something I've really run across yet.
It seems to me that you don't understand two or more timezoneinfo places
may equate to the same offset and DST boolean flag in your current scheme.

I think that may be a little off track. If two timezoneinfo places have the
same UTC offset and DST boolean flag, then I would have thought would simply
produce the same local time for both timezoneinfo places. That would work
just fine for my purposes.

Jonathan
 
P

Peter Duniho

Mark said:
That's something that Peter is totally incapable of doing.

LOL!

Says the guy who felt it necessary to interject himself into a thread he
had absolutely no connection with.

Seems like you're the one looking to "have the last word" here.

As far as whether I can let it go, I have. I see no point in making any
further attempts to help Jonathan, and I'm not going to. Every single
post I've contributed to this thread in reply to his messages has been
with the intent of helping him, and there's no "last word" I've made any
attempt to "have".

My reply to Arne was conversational in nature, simply empathizing with
Arne's own inability to get Jonathan to communicate in a useful way what
he really wants to do.

I see now that Mike is also trying to reiterate all the same points I
already made to Jonathan.

Three people have all tried to provide the same information to Jonathan,
including me. In each case, he isn't accepting the information, nor is
he providing the additional details that would be necessary for him to
get his goal accomplished. Yet, somehow I'm the one with a defect here?

That's rich. You go ahead and grind that axe if it makes you feel
better though.

Pete
 
F

Family Tree Mike

Jonathan Wood said:
I'm not entirely sure. Either way, it doesn't seem like the best way to
go--I still suspect there is an easier approach.

I've found some examples that I'm looking into. I think I'm getting close.
But a good overview by someone very knowledgeable on the subject is not
something I've really run across yet.


I think that may be a little off track. If two timezoneinfo places have the
same UTC offset and DST boolean flag, then I would have thought would simply
produce the same local time for both timezoneinfo places. That would work
just fine for my purposes.

Jonathan


Have a look at
http://en.wikipedia.org/wiki/Daylight_saving_time_around_the_world to see how
complicated it will be.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top