How to use region timezone with .Net application?

I am working on an asp.net mvc application. Each user has their own time zone.

I am currently using "TimeZoneInfo.GetSystemTimeZones" to create a dropdown so the user can select a time zone and this is what I store in my db
They are:
Morocco Standard Time (00:00:00)
UTC (00:00:00 )
GMT Standard time (00:00:00)
...

I know php uses a different set of timezones, they are the "region timezone" like:
Europe / Paris
Europe / London
...

My question is, is there a way to play with the region timezone (like php) in a .NET application? The only way I can think of is to bind each php scope timezone to .net timezone.

In addition, "TimeZoneInfo.GetSystemTimeZones" displays the entire time zone on the machine. Is the list different between Windows servers, Windows Vista, Windows XP?

Hope this makes sense, thanks guys

+2


a source to share


3 answers


The zone name scheme used by php is often referred to as the Olson database.
You can find the conversion table for Windows zone (id) -> Olson DB at http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html

You might also be interested in noda-time (although not released yet). Jon Skeet talked a bit about this on episode 200 of hanselminutes , and he'll be using Olson DB as well.




If the list / doc at http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml is complete and up to date, you can create a simple transform function for php with

$supplementalData = simplexml_load_file('http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml');

$zones = array();
foreach( $supplementalData->windowsZones->mapTimezones->mapZone as $zi ) {
  $zones[ (string)$zi['other'] ] = (string)$zi['type'];
}

file_put_contents('zoneconvert.php', '<?php function windowszone2olson($winzone) {
  static $zi = '.var_export($zones, true).';
  return isset($zi[$winzone]) ? $zi[$winzone] : false;
}');

      

This will create / overwrite the file zoneconvert.php

that provides the function windowszone2olson($winzone)

. You can do more or less the same on the .net end (which is probably the best place).

+4


a source


It looks like you are after zoneinfo or Olson database names. Basically these are the names that the non-Windows world uses :)

They are also what Noda Time is commonly used, although we will eventually support Windows names as well. Unfortunately, Noda Time is not production ready yet - when it is, that should be the correct answer to your question :)



In the meantime, if you will definitely only be using Windows, I think it would be wise to store the Windows timezone IDs - although you are using a property Id

instead StandardName

. There are translations between Windows IDs and Olson IDs all over the internet, but I don't know how complete (or updated) they are.

+2


a source


I would use this method to initially populate your timezone table with data, rather than retrieving from the collection every time. If something changes, you might accidentally change the IDs that each person uses at some point later along the way. You also don't need to worry about any potential differences between the OS.

+1


a source







All Articles