The New DateTimeOffset Structure in .net 3.5

Hi my blog,

I always wonder about almost the Integer.MaxValue like Types present in .net framework. That number kept on increasing from .net 1 to 2 to 3 to 3.5 and they have no plan to stop it. Are these guys still alive? I wonder!. I have a message for them in the form of good old MAD(TM) one liner "Get a Life!”. I again wonder at what time these people wrote these codes, in the daytime or at night.  At whatever time they did their coding there is good chance that my time will be different from theirs. As you may be aware earth does not have the capacity to bear all the people at a time and the poor earth made several time zones so that half of them will be asleep at any point of time, What an Idea! Because of this the human creatures devised several methods to represent one's own time. My teachers, from time immemorial, tried to teach this timescale stuff to me and they utterly failed and retired both from the black art of teaching me and from the art of teaching itself. Now I have to study all these time things myself. What a Pity.

 

In the .net world, as much as I know (in nanograms), there is two easy ways to get one’s time. These are Date.Now() and Date.UtcNow(). Not surprisingly the first one gets the local time and the next one UTC time. Now I have two times which are not the same and I call their difference as a geeky variable name called offset. I here also makes an educated guess,  which by the way most of the time will be wrong, that some one who resides in London will not have the pleasure of getting two times because Greenwich line passes over these poor guys. In other words their offset will be zero.

 

After three years of vigorous research and experimentation on wild animals including me (goofs...) I found, only for your understanding my Blog that DateTime values do not adjust for the differences in time offsets when returning a result. Before you came there was (and still) a good companion for me called Console from System. I will introduce him and he will demonstrate the research work.

 

Dim someOneDiedAtLocalTime, someOneDiedAtUtcTime As Date
someOneDiedAtLocalTime = Date.Now
someOneDiedAtUtcTime = Date.UtcNow

Dim hisCremation As Date = someOneDiedAtLocalTime.AddHours(2)
Dim timeRemainForCremation As TimeSpan

timeRemainForCremation = hisCremation - someOneDiedAtUtcTime Console.WriteLine(timeRemainForCremation)

 

You didn’t get it? Don’t worry blog neither do I  I will tell a story, blog. If some one dies it happens in a point of time and it does not matter whether he died in UTC or local time and for the sake of simplicity or complexity we may call that point Exact Point of Time. If a program like above is used for that one’s cremation, he certainly is going to have a bad time in coffin.   Date structure is not intelligent enough to figure that point and hence it also considers the time zone offset in to calculation.  The guys behind .net 3.5 rectified this situation by introducing DateTimeOffset and here is the code for you my dear Blog

 

Dim someOneDiedAtLocalTime, someOneDiedAtUtcTime As DateTimeOffset

someOneDiedAtLocalTime = DateTimeOffset.Now
someOneDiedAtUtcTime = DateTimeOffset.UtcNow

Dim hisCremation As DateTimeOffset = someOneDiedAtLocalTime.AddHours(2)

Dim timeRemainForCremation As TimeSpan
timeRemainForCremation = hisCremation - someOneDiedAtUtcTime

Console.WriteLine(timeRemainForCremation)

 

This will print his cremation time correctly my Blog. That’s all my little blog for today. I will see you soon in  DateTimeOffset hours and I will tell more about it. Right now I have to attend that cremation, Bye

 

 

 

 




 

Published Thursday, January 24, 2008 5:52 PM by suhair

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)