Scott Hanselman

Back to Basics: Daylight Savings Time bugs strike again with SetLastModified

November 6, '11 Comments [32] Posted in ASP.NET | Back to Basics | Learning .NET
Sponsored By

CC BY-NC 2.0 Creative Commons Clock Photo via Flickr ©Thomas Hawk No matter how well you know a topic, or a codebase, it's never to late (or early) to get nailed by a latest bug a over a half-decade old.

DasBlog, the ASP.NET 2 blog engine that powers this blog, is done. It's not dead, but it's done. It's very stable. We had some commits last year, and I committed a bug fix in February, but it's really well understood and very baked. My blog hasn't been down for traffic spike reasons in literally years as DasBlog scales nicely on a single machine.

It was 10:51pm PDT (that's Pacific Daylight Time) and I was writing a blog post about the clocks in my house, given that PST (that's Pacific Standard Time) was switching over soon. I wrote it up in Windows Live Writer, posted it to my blog, then hit Hanselman.com to check it out.

Bam. 404.

What? 404? Nonsense. Refresh.

404.

*heart in chest* Have I been hacked? What's going on? OK, to the logs!

l2    time    2011-11-06T05:36:31    code    1    message    Error:System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: utcDate
at System.Web.HttpCacnhePolicy.UtcSetLastModified(DateTime utcDate)
at System.Web.HttpCachePolicy.SetLastModified(DateTime date)
at newtelligence.DasBlog.Web.Core.SiteUtilities.GetStatusNotModified(DateTime latest) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SiteUtilities.cs:line 1253
at newtelligence.DasBlog.Web.Core.SharedBasePage.NotModified(EntryCollection entryCollection) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SharedBasePage.cs:line 1182
at newtelligence.DasBlog.Web.Core.SharedBasePage.Page_Load(Object sender, EventArgs e) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SharedBasePage.cs:line 1213
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) while processing http://www.hanselman.com/blog/default.aspx.

What's going on? Out of range? What's out of range. Ok, my site is down for the first time in years. I must have messed it up with my clock post. I'll delete that. OK, delete. Whew.

Refresh.

404.

WHAT?!?

Logs, same error, now the file is a meg and growing, as this messages is happening of hundreds of times a minute. OK, to the code!

UtcSetLastModified is used for setting cache-specific HTTP headers and for controlling the ASP.NET page output cache. It lets me tell HTTP that something hasn't been modified since a certain time. I've got a utility that figures out which post was the last modified or most recently had comments modified, then I tell the home page, then the browser, so everyone can decide if there is fresh content or not.

public DateTime GetLatestModifedEntryDateTime(IBlogDataService dataService, EntryCollection entries)
{
//figure out if send a 304 Not Modified or not...
return latest //the lastTime anything interesting happened.
}

In the BasePage we ask ourselves, can we avoid work and give a 304?

//Can we get away with an "if-not-modified" header?
if (SiteUtilities.GetStatusNotModified(SiteUtilities.GetLatestModifedEntryDateTime(dataService, entryCollection)))
{
//snip
}

However, note that I'm have to call SetLastModified though. Seems that UtcSetLastModified is private. (Why?) When I call SetLastModified it does this:

public void SetLastModified(DateTime date)
{
DateTime utcDate = DateTimeUtil.ConvertToUniversalTime(date);
this.UtcSetLastModified(utcDate);
}

Um, OK. Lame. So that means I have to work in local time. I retrieve dates and convert them ToLocalTime().

At this point, you might say, Oh, I get it, he's called ToLocalTime() too many times and double converted his times. That's what I thought. However, after .NET 2 that is possible.

The value returned by the conversion is a DateTime whose Kind property always returns Local. Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.

But. We originally wrote DasBlog in .NET 1.1 first and MOVED it to .NET 2 some years later. I suspect that I'm actually counting on some incorrect behavior deep in own our (Clemens Vasters and mine) TimeZone and Data Access code that worked with that latent incorrect behavior (overconverting DateTimes to local time) and now that's not happening. And hasn't been happening for four years.

Hopefully you can see where this is going.

It seems a comment came in around 5:36am GMT or 10:36pm PDT which is 1:36am EST. That become the new Last Modified Date. At some point we an hour was added in conversion as PDT wasn't PST yet but EDT was EST.

Your brain exploded yet? Hate Daylight Saving Time? Ya, me too.

Anyway, that DateTime became 2:36am EST rather than 1:36am. Problem is, 2:36am EST is/was the future as 6:46 GMT hadn't happened yet.

A sloppy 5 year old bug that has been happening for an hour each year that was likely always there but counted on 10 year old framework code that was fixed 7 years ago. Got Unit Tests for DST? I don't.

My server is in the future, but actually not as far in the future as it usually is. My server in on the East Coast and it was 1:51am. However, the reasons my posts sometimes look like they are from the future, is I store everything in the neutral UTC/GMT zone, so it was 5:51am the next day on my file system.

Moral of the story?

I need to confirm that my server is on GMT time and that none of my storage code is affected my Daylight Saving Time.

Phrased differently, don't use DateTime.Now for ANY date calculations or to store anything. Use DateTime.UTCNow and be aware that some methods will freak out if you send them future dates, as they should. Avoid doing ANYTHING in local time until that last second when you show the DateTime to the user.

In my case, in the nine minutes it took to debug this, it resolved itself. The future became the present and the future last modified DateTime became valid. Is there a bug? There sure it, at least, there is for an hour, once a year. Now the real question is, do I fix it and possibly break something that works the other 8759 hours in a year. Hm, that IS still four 9's of uptime. (Ya, I know I need to fix it.)

"My code has no bugs, it runs exactly as it was written." - Some famous programmer

Until next year. ;)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Got clocks?

November 6, '11 Comments [34] Posted in Musings
Sponsored By

"Spring forward, fall back"
- The Idiot who invented Daylight Savings Time.

It's time to switch from Daylight Savings Time to Standard Time. I tweeted earlier that I'd gone around the house and updated, by rough count, 18 clocks. Then, I got a bunch of responses like "I've only got one," or "All mine are automatic," or "18 clocks, you're insane."

Turns out the real number is WAY more than 18. I didn't realize.

So, this is for all you folks with your microwave flashing 12:00. I decided to take a moment and really list out all the devices in my house that know the time, separating them by the ones I need to update myself and the ones that automatically update themselves. I realize I could NOT update 50% of them, if not more, and I realize I have a number of gadgets.

Fine, I like clocks. More perhaps than I realized. That said, I really only need the time right on the wristwatch I'm wearing at the time. For you young people, a wrist watch is a non-internet connected device worn often with a leather or metal strap that allowed you to glance as your arm and know the time. It did NOT have Siri, nor did it run Opera Mobile.

Most of these "don't matter" but it was the sheer number of time-keeping devices, both direct and indirect, that surprised me.

binaryclock Clocks I need to change manually

  • Six cheap wall clocks. In bedrooms, bathrooms, playroom.
  • Five clock radios. Bedrooms, guest room.
  • Stove
  • Microwave above stove
  • Insulin Pump
  • Two Blood Glucose Meters
  • Cool Digital Binary Clock on my desk (at right)
  • Two car radios with clocks
  • Zoom H4N Recorder (for my podcast)
  • Casio S95 Digital Camera
  • Canon SLR Digital Camera
  • Cordless Phone Base (3 satellite phones)
  • Nine wrist-watches (I collect watches)
  • Rainbird Water Timer

Clocks that are automatically updated

Of course, many of these are random Craigslist tech, but even if you remove those, you've still got a lot of things keeping time around the house. 

I bet you have more things in your life that care about the time than you think. How did we survive the Year 2000 again? Oh, none of these clocks matter, that's right.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Your images are a virus. They are EVERYWHERE on the Internet

November 4, '11 Comments [13] Posted in Blogging | Musings
Sponsored By

Stuff you love to do, Stuff you're good at, Stuff someone will pay you to do.This is a silly little story of a silly little image. An image whose original conceptual source isn't me. Well, I made the image, I made it in PowerPoint with Smart Art in about 12 seconds over 4 years ago. But per my G+ friend James Saull, I know now that the concept is called "The Hedgehog Principle" or "The Hedgehog Concept" by Jim Collins. You'll find it everywhere but it started in 2001 with the book "Good to Great." I likely absorbed it at some past point and when I got the job I drew three cricles. It's one of those "duh, that's awesome" concepts. It's just a Venn Diagram with three circles with the intersection, the middle bit, being the most awesome ideal part. This isn't about the original of the concept, it's how one image is found everywhere on the internet, spreading like a virus. Once you put an image on the internet, you'll never be able to take it down.

I made the circles for a blog post when I took the gig at Microsoft in July of 2007. I haven't thought about them much since, although I've used them in some presentations a few times. They are unimpressive and rather pink.

Yesterday at lunch I was on Facebook and commented on a friends photo. Her friend "liked" my comment, and I clicked to see who that friend of a friend was. Then just scrolling down, I saw my circles on this stranger's wall. Cool! What an amazing coincidence.

Dream Job on FB

This image was shared from another page within Facebook. I followed the rabbit. The photo had hundreds of likes and many shares.

Dream Job on FB

I then started wondering how far this thing went. Well, it spread long before this funny Facebook coincidence. Kyaw Zaw suggested on Google+ that I put my original image into Google Image Search.

Googling with a custom date range shows the first instance of my image, on my blog in 2007. You can also search for images with images using TinEye Image Search.

I can't see how to reliably hotlink to Google Image Search results, so go to http://images.google.com/, click the little camera icon and paste in the URL to the image, like http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/2dc61d7e4a66_13443/image.png

Googling for Dream Job with Google Image Search

Widening the search dates to all dates, I can see there are 228 different places this image appears, mostly on career and inspirational/aspirational blogs.

Lots of Dream Job on the second page

Image search systems are a fascinating way to see how your images find their way around the web. If you had some intellectual property embedded within your images, presumably you could watermark your images but I suspect intuitively that heavily watermarked images might not spread as freely. There's just no easy way to "protect" (if you wanted to) your images these days. So they spread!

What images of yours have spread around the internet?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Google+ Ripples brings something interesting to the table

November 4, '11 Comments [14] Posted in Blogging | Musings
Sponsored By

I'm on Twitter. I'm on The Facebook. And, sigh, I'm on the snoozefest that has been Google+. I keep coming back to Twitter though for so many reasons

Twitter, today, is just easier to to use, the "What's happening" text box is always there, sharing is effortless. But the lack of real threading, of discussion, is starting to wear on me. Why do sites like even Storify exist? They are there to try to piece the shatter pieces of your Twitter discussion back together. Seriously, I challenge you to piece together anything two weeks after it happened on Twitter. How many retweets did your awesome Tweet get? 100+. Maybe more. You'll never know.

But, the Google+ mobile apps are a mess, so I've just not found a reason to spend much time on G+, except for the occasional Hangout. Until today.

Ripples. Wow.

Pick a post, like I did here, click the down-chevron and click "View Ripples."

Give the users their data. Give them analytics. Let them see how their data moves around the web and how it happened. Let them understand how the network lives and works with elegant visualizations. While you at it, animate the process. Brilliant.

Your move, Twitter. Now it's getting interesting.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Profanity doesn't work

November 3, '11 Comments [118] Posted in Musings
Sponsored By

Docoumentation is apparently VERY important. Fucking important, in fact. I was perusing the Interwebs yesterday and stumbled on a new article from Zach Holman called Don't Give Your Users Shit Work. I was a little taken aback by the swear word in the title. I clicked around Zach's site, and found his Talks area and clicked on A Documentation Talk and the second slide dropped the F-bomb. Wow, really? I said to myself, is this how to connect with someone who is trying to learn about a technology? I was surprised to find swearing to up front and center on Zach's blog.

Is swearing in technology conference presentations appropriate? When did this start being OK? Swearing has always been a part of popular culture and certainly always been a part of technology and technology people. However, in my experience swearing has been more often an after work bonding activity, if at all. It's hanging with fellow coders in a pub after a long day's debugging. It wasn't a part of presentations and certainly not welcome in the boardroom.

I propose that David Heinemeier Hanson popularized swearing unapologetically, or at least brought it out in the open at large keynotes and presentations. David says:

...I’ve used profanity to great effect is at conferences where you feel you know the audience enough to loosen your tie and want to create a mental dog ear for an idea. Of all the presentations I’ve given, I’ve generally had the most positive feedback from the ones that carried enough passion to warrant profanity and it’s been very effective in making people remember key ideas.

As with any tool, it can certainly be misused and applied to the wrong audience. But you can cut yourself with a great steak knife too. Use profanity with care and in the right context and it can be f***ing amazing.

He rightfully notes that it's a tool used with care and isn't appropriate for all instances, but from what I've seen of DHHs talks as well as in pursuing Zach's (who is a lovely chap, by the way), it appears they believe it's a good tool more often than not.

Perhaps it's generational or cultural, but more and more a lot of new under-30 web techies drop the F-bomb and swear liberally in their presentations and slides. Is this the way young web technologists do business now?

I believe that having S*** and F*** in your conference slides or titles doesn't make you cool or professional, or a better coder. It makes you look crass. When is it appropriate and why is it appropriate when other things aren't?

A few years back there was a controversy when some sexually suggestive pictures were used at a popular technology conference in a database presentation. From Martin Fowler:

The main lines of the debate are familiar. Various people, not all women, lay the charge that the images and general tone was offensive. Such material makes women feel degraded and alienated. This kind of presentation would not be tolerated at most professional events.

Defenders of the presenter point out that the slides were humorous and no offense was intended.

Clearly everyone agrees that sexism has no place in technology presentations. They agreed before this incident and many re-declared their support for sexism-free presentations after.

However, many top presenters don't agree that words that are evocative of sex and feces are in fact not appropriate. They would argue these two words have transcended their original meaning and are now well-used as punctuation or that the F-word is useful as nine different parts of speech. Both of these arguments are demonstrably true, but there's so many other words to use. Is the linguistic usefulness of the F-word too tantalizing to give up? Martin mentions DDH using his own words:

David Heinemeier Hansson is happy to proclaim himself as an R rated individual and is happy to consign "professional" to the same pit to which he cast "enterprise".

Why so mean?I personally don't put the word professional in the same overused category as "enterprise." Professionalism is well understood, in my opinion and usual not up for debate. Perhaps swearing is appropriate on a golf course where the Sales Suits make deals, but it's not appropriate in business meetings, earnings calls, or technology presentations.

There's hundreds of thousands of perfectly cromulent words to use that aren't the Seven Dirty Words. Or even just the two words that evoke scatology or copulation. At least use some colorful metaphors or create a new turn of phrase. Shakespeare managed, thou frothy tickle-brained popinjay. Zounds.

However, I do recognize that swearing, or specifically the choice to swear in a public forum is stylistic. I wouldn't presume to ascribe intelligence or lack thereof based solely on swearing. To DHH and Zach Holman's credit, their swearing in presentations is a conscious and calculated choice.

Zach says, via Twitter:

I love words. And those words evoke a lot of emotion. I want presentations to be emotional. I want a story to be told...it's certainly a stylistic choice I've made (and connected with). I actually am fine with offending or alienating a few. Because I believe it lets me connect deeper with others.

And this last point is where Zach and I differ. While I'm known to swear in person occasionally, I don't swear on this blog or in presentations. In fact, when I did swear in a recent "off the record" podcast, many found it out of character and off-putting.

DHH on being arrogant Swearing in presentations or as a part of your public persona might be attractive to some technologists who admire your "passion" or "zeal" but there's no doubt that many others will find that kind of unnecessary coarseness turn off.

It's worth noting that DHH is Danish and it's been my experience all over the world that it's primarily Americans that are the most easily offended by the use of our own swear words. You'll often hear the F-bomb on even teenage television shows in many European countries and their movies are almost never censored for language.

Swearing in presentations isn't unique to DHH or Zach, and it's not unique to one technology or another. I'm just using them as an example. Both are reasonable and logical guys, so they both realize this is a difference in a opinion and not a personal attach. In fact, Rob Conery and are working on getting both fellows on the show to talk about Swearing, Connecting with your Audience and Professionalism sometime soon.

My question is, do swear words add as much as they subtract? Do they increase your impact while decreasing your potential audience? I believe that swearing decreases your reach and offers little benefit in return. Swearing is guaranteed to reduce the size of your potential audience.

As I've said before:

"Being generally pleasant and helpful isn't sugarcoating, it's being pleasant and helpful."

I appreciate and respect that profanity in presentations is a deliberate choice. You're cultivating a personal brand.

However, you take no chances of offending by not swearing, but you guarantee to offend someone if you do.

Better if it's a focused style, a conscious choice and all part of your master plan but it's not for me. I choose to blog, speak and teach without swearing. My message is clearer without these words.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.