| Chris Skardon 的个人资料Chris Skardon日志列表 | 帮助 |
|
|
2007年4月 Time zones in .NETThe problem: converting a given string representing a date and time to a UTC time. Time zones are funny things, annoyingly complicated and frustrating to program with. Obviously there are solutions and workarounds, mainly working with UTC (Universal Time Code - which is the same as GMT (Greenwich Mean Time) and WET (Western European Time) and I'm sure another 100 or so others!). For those who are taking their first steps into the minefield of time zones in .NET they might be lulled into a false sense of security with the 'TimeZone' class available to developers. It seems promising, hell, a whole class called TimeZone! Easy money - surely something like: TimeZone est = new TimeZone("EST"); //-4/5 hours from UTC Or something like: DateTime estTime = TimeZone.Convert("GMT", "EST", gmtTime); But no, we unfortunately don't have this capability built in. It's a bit annoying, as we know the hosting O/S of the framework (be it *nix or Windows) knows the TimeZone conversion information, we've all played with the clock once or twice in the past and seen the world displayed with all its zones. So, with the framework leaving us high and dry, what can we do? Firstly – looking around the interweb for time zone related information takes us on a tour of many sites dedicated to the world’s time zones, such as ‘The World Clock’, ‘World Time Zone’ and the ‘Time Zone Converter’ sites – generally just information providers – useful for finding out times etc, but not too practical for the programmatical (I’m pretty certain that isn’t a word) approach. Then a bit of delving takes us to the Zoneinfo page (also known as TZ), a public domain database containing the relevant time zone information. Now we’re getting somewhere. It has an impressive amount of implementations, from the GNU C Library, to Mac OS X, in fact, ‘The World Clock’ website (linked above) uses TZ as a backend. Ok, so we’ve got our database, what next? Well – looking down the list of libraries etc using TZ there aren’t any .NET based ones at all – nuts – this means we might have to create the API to the TZ database ourselves, or if we’re lucky – someone has already done this for us. So a quick search for: ‘zoneinfo C#’ brings up a likely candidate – TZ4Net Library. Ahh HA! This is more like it – hopefully with this we can do the conversions we were after! Time now for a quick program just to check this does indeed do what we’re after, and yes! It provides exactly what I wanted. The OlsonTimeZone class will use any ‘DateTime’ passed as an argument as a DateTime from the same zone. Perhaps better explained: If you create two OlsonTimeZone instances: OlsonTimeZone zoneEST = OlsonTimeZone.GetInstance("EST"); And then convert an arbitrary DateTime (in this case 9am) DateTime toConvert = new DateTime(1902, 1, 1, 9, 0, 0); We get the output: Converted: 09:00:00 So, zoneEST read the 09:00:00 as 9am EST time, zonePST as 9am PST. The reason this is important is because it makes no difference what the DateTime class thinks its own TimeZone is, whether it’s UTC or local, OlsonTimeZone will just ignore it. The sample program below goes through a collection of values stored in an array printing out some information about the time zone selected – nothing special at all. using System; class Program string currTimeZoneInfo = (TimeZone.CurrentTimeZone.IsDaylightSavingTime(now)) ? Console.WriteLine("Time: " + now + ", " + currTimeZoneInfo); foreach (string timezone in timeZones) bool isDst = zone.IsDaylightSavingTime(now); Console.WriteLine("UTC: " + utc); Console.WriteLine("Press ENTER to Exit"); All that’s left is to say thanks to Zbigniew Babiej for writing the API! 2007年4月 Into ProductionBusy times at the company at the moment. We've just pushed out the newest version of our system (the one I've spent the last 3 years working on), into a new domain. On a scale of 1-10 (10 being the best) this week has been a 7, we were due to go into production on the Monday, but due to unforseen problems with an external source (always good when the delay isn't due to your code!) we had to delay until Tuesday. Tuesday brought (unfortunately) code issues - and with it, another 24 hour delay. Wednesday though - well - that brought excitement and joy - as it finally went live. Admittedly - there were some teething problems, but nothing a couple of hours of sarcastic jokes and fretful debugging couldn't solve. The system is now back on the shelf - awaiting analysis of the days activity - and a couple more minor bug fixes. But hopefully from next Tuesday onwards - we'll be looking at a fully live situation! I realise this isn't the most interesting post, and by not actually mentioning what the system is, it's mainly pointless. Meh. 2007年4月 Finally finished my first WPF appThat's Windows Presentation Format for those not in the know! It's the new presentation format for .NET developers (due for release with the next version of Visual Studio - "Orcas"), and also buildable using Microsoft 'Expression'. Expression is a new collection of tools aimed more at 'designers' than 'developers' - that basically allows you to design new GUI (Graphical User Interface) elements using the Vista stylings. Well - that's a bit of a simplification, but it's close enough. Anyhews - I love playing around with UI elements on my pet projects, (generally leading to a 'form over function' build arriving much before anything else), but I hadn't really messed around with any of the WPF stuff until this past week or so. I attended a conference in february (DevWeek 2007) and whilst there picked up a new book - 'Foundations of WPF' (by Laurence Moroney). I started working my way through it, and aside from it being a bit out of date now (technology does move fast) I've been finding it good and easy to get to grips with. I've only built one app, and that's using Expression - so I'm looking forward to getting my hands dirty in the Visual Studio Environment - but I have to say how impressed I am with the Expression program (Expression Blend). The ability to create pretty impressive looking stuff in next to no time is phenomenal. Even just setting up a simple reflection in WinForms code would take - well - ages, in Expression - 2 minutes. The plan is to continue one of my other projects (a maze solver) and then wrap it up with a nice WPF front end - only a couple of hundred pages to go! |
|
|