Wednesday, 5 October 2011

Working with dates

Lately I've been doing a good amount of work on a website feature that requires the output of a relative date and time. For example, a entry written on a user's wall (the website has social aspects) may be annoted with "written by John Smith about two hours ago." It is a relatively straight forward task to accomplish this - but I thought I'd write a few words about some pitfalls you may come across.

Even if you have only every written the most trivial of applications you are likely to have used the DateTime structure in .NET (if you've written .NET apps, that is). And you're very likely to have used DateTime.Now; Let's take a closer look at the DateTime structure. According to Microsoft's documentation a DateTime "Represents an instant in time, typically expressed as a date and time of day." And, DateTime.Now "Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time."

Cool. DateTime.Now is really handy for getting a handle on the current time, and we've all used it. But now I'm going to tell you that you shouldn't.

DateTime.Now returns a DateTime object that represents "the current date and time on this computer, expressed as the local time." As convenient as this may be, it's a potential source of trouble. "This computer" is the computer where the code executes. It could be your desktop machine, a development server, a production server, or a mobile device. The time on that machine depends on where the machine is located (which timezone it is in) and whether or not the machine is affected by daylight savings. So why is this a problem?

The problem is that if you cannot guarantee that all the machines in your infrastructure are all in the same timezone and are equally affected (or not affected) by daylight savings, using DateTime.Now in your code will potentially yield different timestamps on different machines even if the call to DateTime.Now was made at exactly the same time on the machines in question. 1pm in Oslo, Norway, on October 5th 2011 is not the same time as 1pm in London, UK, on the same date. "But that's just silly", I hear you say. "All our infrastructure is in the same data centre in one place." OK. Fine. That may very well be the case. But what about your users. Where are they? Are all of them in the same time zone as your servers? And is it likely that you'll never grow beyond having only local users and having only one data centre?

Even if your answer to the above questions is "we'll never scale beyond one data centre and all our users are in the same place and always will be" I think you should keep reading. It might just make your life a little simpler down the road. Just in case.

The problem with DateTime.Now is that it always represents the local time of the machine on which the code executes, and you don't really want to worry about where that machine is, because doing so makes life as a developer painful. What you want to do instead is use DateTime.UtcNow which returns an instance of DateTime representing the Coordinated Universal Time (UTC) of now. UTC is the local time of the server less any timezone differences and less any daylight savings difference. If you only ever store and use UTC DateTimes then none of your DateTime comparisons will ever have to take into consideration any time differences caused by time zones or daylight savings.

The only thing you now have to worry about is the thing you should worry about, which is displaying the correct date to your end users. You'll have to adjust for the timezone they're in because a date for an event at 12pm in London should be rendered as 1pm for a user in Oslo (they're always an hour ahead of London time).

Regardless of whether you use UTC or not, you'll always have to consider time zones and daylight savings when rendering dates for a user. Using UTC, that's all you'll have to worry about. If you use DateTime.Now, however, you'll also have to ensure that you know what the time offset of that DateTime instance is if you're going to compare it to another date, or if you're going to render it to a user. Pain in the arse (PITA).

I reckon that you should always use UTC times in your applications regardless of what your user base might look like. It makes life simpler from the start, and if you ever need to support users across different timezones you'll be a step ahead.

So, as a rule, this is what I do:

  • Always call DateTime.UtcNow and never DateTime.Now. (In fact, I don't use DateTime... I use DomainTime which is a wrapper I've created around DateTime. I'll write more about that in a later post).
  • Always treat DateTime stored in a database as UTC. This means that when I read that DateTime out of the database I specify that it is a UTC DateTime using the DateTime.SpecifyKind() method. This is very important, because the machine will by default treat any DateTime as local time.

    Note that if you're using an ORM such as NHibernate you need to tell the ORM that the date should be treated as UTC. With FluentNHibernate this is really simple:

    Map(x => x.DateCreated).CustomType().Not.Nullable();

  • Lastly, before displaying the date to the user, I apply the time difference between UTC and the user's location. There are several ways of doing this. For example you can have your users tell you which time zone they're in and you can apply the offset. Or, if your users are web users you can use Javascript's Date.getTimezoneOffset() method and apply the difference (in minutes) to your UTC date. Check out this StackOverflow question for some specific pitfalls of that particular method.

I'll follow up on this post with another post or two about displaying relative times and how to use a DomainTime wrapper around your dates.

24 comments:

Nigel Holland said...

Hi Øyvind,

I agree with you about UtcNow.

I find it useful for my date/time wrappers to have the ability to set the value that UtcNow() returns for unit testing purposes.

Nigel

Øyvind Valland said...

Hi Nigel,

Yeah, any wrapper around DateTime should allow you to specify the date/time so that you can use it for testing. That's why I don't use DateTime directly any more, because it makes testing code that depends on time very difficult. I'm going to write a short post about this soon.

Anonymous said...

Anime Nippon~Jin - Kagi Nippon He, INVITE YOU TO TO VISIT THE WEB AND FORUM OFFICER, WHERE YOU WILL BE ABLE TO FIND:

- Dates of Conventions in the whole American Continent of Anime, Sleeve, Comics, Meetings of Cosplay, Video games, Festivals, Concerts (Peru, Chile, Honduras, Brazil, Mexico, EUA, Canada, Argentina, Uruguay, Costa Rica, Colombia, Bolivia, Venezuela, Paraguay, Ecuador, El Salvador, Panama, etc.) Europe (Spain, Italy, England, France, Portugal) and It was Grasping and Oceania (Japan, South Korea, China, Malaysia, Taiwan, etc.)

- A place with topics of conversation and where you can initiate conversation topics.

- Shops of Anime in your locality.

- Cosplay, tutors, photos, Galleries of Events, Videos, and more.

- Video games

- Radio, Movies, Television


AND ALSO YOU CAN FOLLOW US FOR FACEBOOK, TWITTER, GOOGLE, TUMBLR AND WORDPRESS!!

AND HELP US WHY THE WEB CONTINUES IN FOOT DEACTIVATING ADBLOCKER ON OUR PAGE AND TO CLICK IN SOME ANNOUNCEMENT OF OURS SPONSORS.


Official WEB's


http://kaginipponhe.com
http://kaginipponhe-anj.blogspot.mx/
http://animenipponjin-knh.blogspot.mx/
http://animenipponjin.b1.jcink.com/



FACEBOOK
https://www.facebook.com/AnimeNipponJin



TWITTER

https://twitter.com/AnimeNipponJin



GOOGLE

https://plus.google.com/u/0/108899406425756347857/posts/p/pub



TUMBLR



http://anime-nippon-jin.tumblr.com/

WORDPRESS:

https://kaginipponhe.wordpress.com/



Say NO to racism and discrimination in the Cosplay

Di NO al racismo y discriminación en el Cosplay

Dire NO al razzismo e alla discriminazione nel Cosplay

บอกว่าไม่มีการเหยียดสีผิวและการเลือกปฏิบัติในการคอสเพลย์

Zeg nee tegen racisme en discriminatie in de Cosplay

Mengatakan TIDAK untuk perkauman dan diskriminasi dalam cosplay yang

Sagen Sie NEIN zu Rassismus und Diskriminierung in der Cosplay

Скажи НЕТ расизма и дискриминации в косплей

コスプレでの人種差別や差別にノーと言ってやる



http://kaginipponhe.b1.jcink.com/


https://www.facebook.com/pages/Di-NO-al-racismo-y-discriminaci%C3%B3n-en-el-Cosplay/175730419146227

Jackie Co Kad said...


Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

Gabe Co Hadwin said...

Great Article

IEEE Projects for Engineering Students
Final Year Projects for CSE

Anonymous said...

Nice Post....
Python Training in Bangalore
Best AI Training in Bangalore
Machine Learning Training in Bangalore

pooja said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Devops Training courses
python Training in chennai
Devops Training in Bangalore

sasitamil said...


I was recommended this web site by means of my cousin.
I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!

selenium training in Chennai
selenium training in Tambaram
selenium training in Velachery
selenium training in Omr
selenium training in Annanagar

Maria said...

what about your finances not bad but everything is just gorgeous Do you want to share with you how to make money sitting on the Internet come here crackajack play casino slots online I wish you good luck earning head

Yogayogi said...

I prefer to study this kind of material. Nicely written information in this post, the quality of content is fine and the conclusion is lovely. Things are very open and intensely clear explanation of issues
python Training institute in Chennai
python Training institute in Bangalore
python Training in Pune

Madhu Balan said...

I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
Java Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore

Anjudevan said...

Really informative Blog...Thanks for sharing...Waiting for next update...
Wordpress Training in Chennai
Wordpress Training
wordpress course fees
wordpress training in vadapalani
wordpress training in Guindy
Struts Training in Chennai
clinical sas training in chennai
Spring Training in Chennai
Photoshop Classes in Chennai
LoadRunner Training in Chennai

rutmanaish said...

Interesting blog. Got a lotb of information about this technology.
Spoken English Classes in Chennai
English Coaching Classes in Chennai
IELTS Training in Chennai
Japanese Language Course in Chennai
TOEFL Training in Chennai
French Language Classes in Chennai
Spoken English Classes in Porur
Spoken English Classes in Adyar

Riya Raj said...

More impressive blog!!! Thanks for shared with us.... waiting for you upcoming data.
Software Testing Training in Chennai
software testing course in chennai
testing courses in chennai
software testing training institute in chennai
Software testing training in Thiruvanmiyur
Software testing training in Velachery
Python Training in Chennai
Digital marketing course in chennai
Python Training in Chennai
JAVA Training in Chennai

chintu said...

nice blog..valuable information....thanks for sharing...
C C++ Training in Chennai
C Language Training in Chennai
c c++ course fees in chennai
C C++ training in tambaram
C C++ training in porur
javascript training in chennai
core java training in chennai
Html5 Training in Chennai
DOT NET Training in Chennai
QTP Training in Chennai

rutmanaish said...

I really liked and I got some innovative ideas for improving my thoughts from well defined content.
IELTS Coaching in Chennai
IELTS Coaching centre in Chennai
French Classes in Chennai
pearson vue test center in chennai
Informatica Training in Chennai
Data Analytics Courses in Chennai
IELTS Coaching in OMR
IELTS Coaching in Porur

sanjandevan said...

Informative content,thanks for sharing...waiting for next update...
javascript training in chennai
javascript course in chennai
javascript training institute in chennai
javascript training in Thiruvanmiyur
javascript training in Tambaram
javascript training in chennai
core java training in chennai
Photoshop Classes in Chennai
Manual Testing Training in Chennai
Drupal Training in Chennai

Prakash said...

More valuable post!!! Thanks for sharing this great post with us.
JAVA Training in Chennai
Best JAVA Training institute in Chennai
java training center in chennai
Java classes in chennai
java training in Adyar
JAVA Training in Tnagar
Big data training in chennai
Selenium Training in Chennai
Digital Marketing Course in Chennai
Selenium Training in Chennai

subhashini said...

Nice blog! Thanks for sharing this valuable information
IELTS Coaching in Chennai
IELTS Coaching centre in Chennai
IELTS Coaching centre in coimbatore
IELTS Coaching in madurai
IELTS Training in Chennai
Best IELTS Coaching in Chennai
Best IELTS Coaching centres in Chennai
Software Testing Training in Chennai

sasi said...

The blog you shared is very good. I expect more information from you like this blog. Thankyou.
web designing training in chennai
web designing training in bangalore
web design training coimbatore
Web Designing Course in chennai
web designing course in madurai
Web development training in bangalore
Web development training in chennai
Big Data Course in Coimbatore

Lovable vicky said...

Nice information, want to know about Selenium Training In Chennai
Selenium Training In Chennai
Data Science Training In Chennai
Protractor Training in Chennai
jmeter training in chennai
Rpa Training Chennai
Rpa Course Chennai
Selenium Training institute In Chennai
Python Training In Chennai

Lovable vicky said...

Rpa Training in Chennai
Rpa Course in Chennai
Blue prism training in Chennai

Data Science Training In Chennai
Data Science Course In Chennai
Data Science Course In Chennai

Nannie Co Pam said...

Great Article. Thank you for sharing! Really an awesome post for every one.

IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

shreekavi said...


Such a great blog.Thanks for sharing.........
Ethical Hacking Course in Chennai
Ethical hacking course in bangalore
Ethical hacking course in coimbatore
Ethical Hacking Training in Bangalore
Ethical hacking Training institute in bangalore
Ethical Hacking in Bangalore
Hacking Course in Bangalore
Ethical Hacking institute in Bangalore
Selenium Training in Bangalore
Software Testing course in Bangalore