Friday, October 30, 2009

Tech: Daylight Savings Time in Javascript

This is another one of those computer programming centric posts I'm doing. I spent the last day and a half trying to find a good way to handle this, and I'm somewhat happy with my solution... so I'm sharing it. I'll post code, so I'm basically saying that if you want to use it, go ahead.

Let me start though, with the situation I needed to resolve. The application I'm working on is something that allows a user to schedule recording times... kind of like TiVo, but not for general Television... the source of the video is something else. Not important. The thing to understand is that it lets a user pick a time through a web application in "local time", "server time", or "UTC".

Since the client machine doesn't know what time it is on the server without the server telling it, I pass the offset from UTC... since everyone knows what UTC is. The problem I was dealing with was when the server and client are in different timezones to begin with, and daylight savings time occurs. If the server is in a timezone before the client (for example), what ends up happening is that when I pass the client my UTC offset, and daylight savings switch happens on the server, but not on the client, the client thinks the server is one hour different from what it actually is... until the client goes through the switch too, and then everything is back to normal.

It's a pain the ass. I hate daylight savings time.

So the idea to solve this problem was to figure out whether the offset was being passed as a standard or daylight savings time, and then to figure out if the client is in the same time (daylight savings or not). If they're different, I need to adjust by one hour, but if they're the same, I can just keep doing what I normally do in the code.

On the server side, I get to use C# (C Sharp for the search engines), and there it's easy: "DateTime.Now.IsDaylightSavingTime()". Really easy... and I can just pass that to the client side in a hidden input.

Now the client side. Javascript is pretty awesome for most things... but it is lacking a simple method or property to give me a boolean response about whether the date/time I'm looking at is in daylight savings or not. I found all sorts of methods online to tell me when daylight savings would shift, but zero methods telling me if a specified Date object held a date/time in daylight savings or not. And the problem only exists for that one hour where we set the clocks back... because it means that a given time occurs twice on the same day. We have 01:30 twice... and knowing which 01:30 becomes important if you're deciding whether to start or stop a recording in that time.

I'm ashamed to admit this, but it took me a while to figure out that if you just write out the date object without using any of the Date object's "to" functions, it includes the current timezone. In my case it was showing "EDT" which of course means "Eastern Daylight Time" (On the east coast of the United States. I guess there's another EDT in Australia). I looked into this a little further to see if it might be as simple as contrasting it with "EST" (Eastern Standard Time). I found out that Internet Explorer (at least in IE 8) is not following W3C Standards for how to output that information. It's supposed to spell out "Eastern Daylight Time". The standard would make things easy... because I could just the "toTimeString" function and search for the word "daylight".

So, I had to keep looking for a solution so I could make sure it works in IE. I decided to look up the list of time zone abbreviations... and what I found lead me to believe that if the timezone abbreviation includes the letter "d", it is a daylight savings time date/time. Well... except for what the website calls a military time zone, and one of them is called "delta"... abbreviated "d".

I had my solution. I test for the browser; If it's IE, I parse out the abbreviation, make sure it's longer than one character long (just to be sure to avoid that military time zone), and make sure it contains the letter "d"; If it's any other browser, I just search the toTimeString for "daylight".

Seems kind of simple now, and I'm wondering why I didn't stumble across anything like it in my google searches. Anyway, in an attempt at getting a solution out there on the web... and in hopes that google will find the page and lead other people here, I'll include the code...

First, a simple function for determining browser...

function isIE()
{
return (navigator.userAgent.toLowerCase().indexOf("msie") > -1);
}

Second... the daylight savings time test...

function isDaylightSavingsTime(theDate)
{
var theTime = theDate.toTimeString().toLowerCase();
var isDST = false;
if (isIE()) //do the IE based code for determining whether it's daylight savings time or not...
{
if (theTime.length > 9)
{
theTime = theTime.substr(9); //trim off the time so we just have the time zone
if (theTime.length > 1 && theTime.indexOf("d") > -1)
{
isDST = true;
}
}
}
else //handle W3C compliant browsers
{
if (theTime.indexOf("daylight") > -1)
{
isDST = true;
}
}
return isDST;
}

There might be more efficient ways to do it, but it works, and it's not THAT complicated. I hope this is useful for you. And I apologize but making the indentations right for the code samples is making my headache worse... so I'm leaving them as is. I'm sure you can forgive me that.


Wednesday, October 14, 2009

Did I meet someone? Really?

Today is Wednesday. Last Saturday I met a girl. And I'm crazy about her. I'm not sure I've ever really meant "crazy" literally though. I find it a little scary how much I like her after knowing only a few days, and only a few hours in person. Let me tell you a story. Don't worry... it's only a few days... how long a story can it be?

It starts with a little background. I think I have a sinus infection. The most prominent symptom has been the sinus pressure and therefore the headaches. I missed work two days last week for this, and I have a doctor's appointment to check it out coming up. So I've been dealing with this headache that waxes and wanes for about a week now.

Saturday was no different, except that I needed to be okay so I could go to a wedding for a close friend of mine. Before I even left for the wedding (it was at 10:30 AM) I had my triple dose of "vitamin I" (Ibuprofen for the uninitiated). So as you can probably imagine I wasn't in the most cheery of moods. Part way through the wedding, I took a fourth dose. I was smart and brought a bottle of the ibuprofen with me. Of course, some of you may know that the bottle for ibuprofen tells you to not take more than 4 doses a day. I was at 4 doses by noon. And yes... I had to take a 5th dose before the end of the reception.

Because of my rough state, and my track record for not meeting people even in the most favorable of circumstances, I assumed I wasn't going to meet anyone at this wedding either. But there was one girl there... she caught my eye. Of course, if you don't know me (and are for some reason reading this blog), I am shy. Stupidly so. Even when I manage to overcome it, I tend to falter, and nothing intelligible comes out of my mouth, so I tend to just sit there and do nothing. Which is exactly what I did. But my friends at the wedding had other plans.

When the last dance rolled around, the bride demanded that everyone get up to dance... it was a paired dance... so I was going to ignore her. But she pointed me out and paired me off with this girl I had noticed. So we danced the last dance together. I didn't even know her name at the time. And I was nervous as hell. As a side note: I hate that I get so nervous. It's irrational. You could possibly classify it as a phobia.

Last dance ended, the reception was over, and there was an after party to go to. A good sized group of us (15 or 16 people I think) went into Boston to Dick's Last Resort. The guys all had tuxedo shirts on, the women had their dresses on, and the bride had her wedding dress on. It was pretty awesome how much attention you can get by wandering around Boston in a wedding dress, though I think it would probably work differently if I tried that.

Anyway, it was loud in there, and shouting was the only way to be heard. I have damn good hearing, but I don't pick out words as well as I pick out the simple presence of sound. So I'm not a fan of these loud places where you can't really talk to people. This girl who's name I still didn't know was sitting almost across the table from me. I think we shouted a little... you know... in a nice way... but it wasn't much real communication.

After Dick's Last Resort we went to a place called "The Hong Kong". Since the place is a dive, I'll assume no one knows what it is. It's a bar with a dance floor, and collection of the most frightening guys I've ever seen. They had nice shirts on, styled hair, an air of confidence, and stares that would make me uncomfortable if I had been one of the girls there. These guys did not strike me as the kind of guys I'd enjoy having a conversation with. They were like vultures. They were circling. It was creepy.

But I'm off topic. Anyway... I was dragged onto the dance floor again. I'm not a huge fan of getting on a dance floor. I don't think I'm good at dancing, and I've already mentioned the shyness thing. Put me in front of a computer, and I'm as comfortable as can be. Put me in front of people and make me do something I think I'm not good at... well... I get nervous.

But the girl (who's name I still didn't know)... danced with me. Now the group was all on the dance floor. Everyone seemed to be having a good time, and the ladies were dancing up a storm... they were really impressive, and I mean that in terms of skill level. They really seemed to have a knack for dancing.

Now I have to clarify something else about myself. I'm shy AND oblivious. I don't notice when women show interest in me. At least, this is what my friends tell me. I think they're making it up, but that's another topic. Assuming they're right, and I really am bad at noticing when women are interested, I wouldn't normally notice what was happening on that dance floor. The girl (I'll tell you her name at the point in the story where I learned it) was dancing with me in ways that figuratively knocked my socks off. She was amazing. It stunned me. And I found myself wanting more and more to be close to her. Even through my hazy awareness of social reality, I came to believe she might actually be interested in me. And I was real happy about that.

The night wore on, and eventually it was time to go. We all headed outside into the chilly night, and stood around trying to recover our hearing (it was really loud in that club). And for the first time that night I saw something that hurt my interest in her. She smoked a cigarette. I am not a fan of smoking as you may know if you read that earlier post of mine in this blog about smoking. And normally if I see a girl smoke, it's an immediate deal breaker. I completely lose interest. At that point in the evening, I just walked away and talked to someone else in the group. I didn't want to smell or breathe in the smoke. But somehow... I didn't lose interest. I still can't explain it, but I was still interested in her.

At some point in the evening I found out that she would be leaving the next day to go back home. And it was some place far away. So my hope was hit pretty hard at that piece of information. It meant she'd be far away, and even if we got along great that night, she'd be far away, and there would be no chance for us to spend any more time together. When the evening was over, and everyone was heading home, I figured it would just end up being a memory for me. Something to look back on and wonder about. I was sad, but not surprised considering my luck.

My memory of the rest of the evening is a little hazy. I had had a few drinks over the course of the roughly 12 hours, but I think my memory is hazy because I was just tired at this point. I think the bride and groom approached me after the girl separated from the rest of the group to head to where she was staying. The bride and groom informed me that she had liked me. And this was the moment I found out her name. They told me Tricia was interested, and I remember my response being something along the lines of: "oh so that was her name..."

When I got home an hour later, the groom asked if it would be okay to share my phone number with Tricia. Tricia asked the bride for it. I eagerly said yes, though I have to admit I was wondering if that meant I was getting a phone call at 1AM. That's what time it was. But it wasn't a phone call... it was a text message. And we started texting back and forth... until about 2:30. I think it was then that I found out she has a 9 month old daughter.

She smoked. She lives 600+ miles away. And she has a daughter. I think it was during that text conversation that I found out she's in the military too. She's served two tours, and is going back for another. Don't get me wrong. I highly respect the military. But this would be one more thing that's keeping us from spending time together.

Normally, a guy meeting a girl with a kid already... well... that's a big important thing and it does require some thought. I'll get back to this point later in this hugely long blog.

Even with all the obstacles and the important topics to consider, I knew I really liked her. It was crazy how much. But I was steeling myself for the probability that nothing would come of it. There were just so many obstacles.

The next morning (and I woke up at 6:00AM despite my best effort to go back to sleep) I got a text message from Tricia. She asked me to go see her off. This would be a 40 mile drive, and the time she gave me lead me to think I might not make it... and the bad part about this is that I was rationalizing it in my head. I was convincing myself to not even try to make it. And then it hit me. That's how I live my life. I never take chances, and I live in my comfort zone. Motivating me to leave my comfort zone is a difficult thing. But in this case... I knew I wanted to see her again... and I knew it might be my last chance. I hopped in my car and I drove. I sped. I actually got out of my own way and drove. Maybe it doesn't seem like much to a reader of this post, but to me... it was an important moment in my life. It was a moment where I finally fought through one of my weaknesses... I won. And I was doing it because of this wonderful girl I had met.

And in case I somehow managed to create tension... to make you wonder if I made it or not... She was running late, and needed to get a meal anyway. So we ate at the Cheesecake Factory in Burlington. Unfortunately, since I had gotten up so early, I had already eaten a big meal... so I feel guilty, but I didn't eat much at the CF. I think I still have the leftovers in my fridge from there. Anyway, we got to eat a meal together, and it was probably the first period of time where we could talk that wasn't full of ambient deafening sound. It was really nice, and I was excited to have had the chance to spend more time with her.

I guess I should mention that she and her friend were both there. Tricia was giving her friend a ride to the airport, and then Tricia was going to be driving home... From Massachusetts to Ohio. Just so I'm not being a jerk by leaving her out entirely... her friend is a really good person. Great stories, very friendly, and she made it easy for me to feel comfortable.

So Tricia was facing an 11 hour drive. Alone. Now I'm not a fan of driving at all. I complain about my 30 mile commute to work. And driving for that long (600+ miles) alone is not something I'd be happy about. She called me near the beginning of her drive, and we talked. I'm not even sure I can recall everything we talked about it. Aside from a pause to eat dinner, we talked for her entire trip.

Something else you might need to know about me to appreciate this part of the story: I hate talking on the phone. Hate it. When people call me for a reason other than making plans and that take longer than 2 minutes... I start trying to find excuses to get off the phone. I don't want to converse on the phone. It's a compulsion. And if I can avoid it I won't talk to strangers on the phone. Calling a doctor's office to make an appointment is unpleasant to me. Telemarketers annoy me greatly. There are exceptions. My brother being half way around the world... I'm willing to talk to him on the phone for as long as he likes. Just an example, and as you can have already read, I talked to Tricia for probably 8 or 9 hours that day... and by "that day" I mean last Sunday. "That day" makes it sound like it was years ago.

I'll admit that part of my motivation was because I was worried about her. I didn't want her falling asleep on the road. But the idea that I would be capable of participating in a 9 hour conversation with someone I just met over the phone is mind boggling to me. I must really like this girl.

And now I'm back at work... and we have been keeping in touch with email, texts, and phone. I've talked about buying her a plane ticket to come visit with her daughter so I can meet her too. I've talked with friends about this... wondering what's going on because honestly, I'm a little scared and confused. How can I have such strong feelings for someone I met four days ago? Am I ready to take something like this seriously enough to take on the responsibility of helping to care for a child? How do I be part of a relationship when we're so far apart? What happens when she gets deployed again? Should I try to get her to move in with me? How did I go from being hopelessly single to wondering these things in a weekend?

Here's another tidbit about me. I tend to analyze everything. I think things through so much that it's often detrimental to me. So I've been thinking about her pretty much non-stop since we danced at that little club called the Hong Kong. I'm worried that I might mess something up because I'm trying to apply logic to a feeling.

So that's where I am. I know I like her a lot. Some of the similarities between us are just too good to be true for me. I'd go into some of them now, but this post is getting real long. Just trust that some of the things she's said have surprised me in very good ways. And I'll leave it to you readers to wonder what might happen. It's still really early. And I don't know what might happen either. But I know right now as I type this that I hope she and her daughter will be part of my life. I hope I'm ready, and I hope I don't mess anything up.

Tuesday, October 6, 2009

Tech: ASP.Net annoys me sometimes

I apologize... this post is going to be pretty computer programming centric. For those of you who read this post, and who don't know me personally, I am a software engineer. My job is primarily done with WinForms and C#.Net. But there are projects that lean toward my roots which is web technology. Right now I'm working on a web application that I shouldn't say too much about, but which has become a source of frustration.

How? Well... it's written with ASP.Net.

Prior to ASP.Net, if you wanted to stick with microsoft technology, and you wanted a highly interactive webpage, you used HTML (basis of all web pages), ASP, and Javascript. Each piece handled it's own area of expertise. HTML was the set of tags that told the browser what to show and how to show it. Javascript was the client side script code to handle things like field validation and to update stuff on the page without having to make a request on the network to the server for more info. ASP handled the server side stuff including talking to databases and so forth.

ASP.Net though... they seem to be trying to take responsibility for all the roles the other things used to take. Now, ASP.Net is trying to write all that other code for you. You have drag and drop components that try to do things for you, but in the process they limit your ability to use the other tools effectively. And on top of that, it's not even efficient code. So it does make some things easy, but it murders your web site performance, and limits your ability to do what you need to do in some cases.

An example: ASP.Net provides an "update panel" that is supposed to leverage Ajax technology so you can update sections of your webpage without doing a full page request from the server. The idea is that if you ask the server for small bits of information and just change the page the user already sees, you can improve performance since the computers and network do less. It's a great idea, and the ASP.Net Update Panel does make it easy to implement... you don't have to write any Ajax code or even know what it is. But it's such a crappy chunk of code. It doesn't give you the performance benefits because in the background... it's STILL DOING THE FULL PAGE SUBMIT. You aren't passing just a small chunk of information to the server... It's like using a nuke when all you needed was a bullet, and yes... the nuke in this analogy still destroys everything around it despite only wanted to shoot one target. It's annoying.

Fortunately, you can just write your own Ajax code, and have it work fine.

What I'm dealing today, and probably for the next few days, is figuring out why ASP.Net won't let me use javascript to update some form fields and still get the data to the server side so I can store it in the database. It's as though if you don't use the officially sanctioned ASP.Net method to accomplish something, it won't let you do it. Which is horrendously stupid, and massively offensive that microsoft would code it that way. I can only imagine ASP.Net is aimed at people who don't know much about web code, and aren't looking to do anything complicated... where "complicated" really means: "things microsoft didn't anticipate". And honestly, I can do most of the things ASP.Net controls do, but better.

So why did I use ASP.Net in the first place?

I didn't. I wasn't in charge of this project at its inception. And the guy who was is a back end programmer. Doesn't even like working in winforms UI... Prefers the business logic layer and the database. And he's damn good at it. But he got tricked when he got hired here and got through into a UI intensive web application project. And with ASP.Net, he successfully built the app. The problem is that as the app grows, and since he left the company, ASP.Net is no longer capable of giving us what we need. And I'm stuck on this stupid bug that doesn't seem to have a solution.

So I guess this post is about venting. Maybe it's also a warning. Web programmers using a microsoft base (which is fine. I like ASP... just not ASP.Net), should strongly consider using basic ASP, and writing their own controls. Don't use master pages. Don't use the built in ASP.Net controls (a "label" is just a span... you can write a span can't you?). Don't use repeaters (If I believed in a god I'd say "for the love of God don't use repeaters").

You'll end up with a far better application that is far better capable of dealing with new needs down the line.