C# british summer time (BST) timezone abbreviation - c#

I need to display a label with the current time zone abbreviation. My pc's timezone is currently set to "(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London". As a result, I would like to see BST displayed, since LN is currently in british summer time.
It looks like that info (timezone abbrev) is not available. Looking at the GMT TimeZoneInfo, all I can see with regard to names is
Id "GMT Standard Time"
StandardName "GMT Standard Time"
DaylightName "GMT Daylight Time"
Is there any way to get to BST from "GMT Daylight Time" or any other available Windows timezone info?

The TimeZoneInfo class referrers to "British Summer Time" as "GMT Daylight Time", so no it is not possible. If Microsoft were to store it as "BST" it would be in the DaylightName property.
Whilst following the development of the TimeZoneInfo class on the BCL blog many years ago, I saw no explanation behind how they chose the values for DaylightName. If I were to guess it would be because this is for a "Time Zone" and not a particular city.
It appears that the public-domain tzdatabase, which is considered to be more complete than Microsoft's time zone database, does display BST for London (source). This is because Cities are included in this dataset, not just the Time Zones.
There is a Project called Noda Time that brings the tzdatabase to .Net that is now avaialble.

You should know that all the time zones that say "Standard" will properly switch to daylight time in the summer. For example TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time") will be an object that can properly handle both standard and daylight times. The same is true for, say, "Pacific Standard Time".

I think you have two choices: Special case handling for the GMT time zone, or generalized handling for "Time zones for which Microsoft uses an incorrect name."
I wonder, for example, whether the Portuguese use "GMT" for their time zone in the winter. Whether they do or not, I doubt they use "British Summer Time" in the summer!

Related

Get eastern time in c# without converting local time

I know there are bunch of solutions on converting timezone to timezone, but what I'd like to know whether we can get eastern time without making conversion from our local time.
Well yes and no, if you represent using GMT always then you don't need to convert until you need to show locality.
Time is always in GMT and services should run under the GMT Timezone because of this reason among others.
Receiving and Storing a time in anything other than GMT / UTC requires conversions.
Please note the Timezone of the server should also be set to GMT for things to work as I indicated.
When you set things up like this it thus becomes much easier to reference and compare times and dates in different calendar date formats also.
This is the reason why DateTime offset made its way into the framework and sql server. if not careful when the Timezone on a server changes so does all stored and loaded dates in local format.
A date comparison is a date comparison. DateTime just wraps a memory structure which is defined in ticks and makes methods to access commonly used parts of the memory e.g. day or year or Time or TimeOfDay etc.
Furthermore conversion is only possible if you know both the source and destination offsets and then the calculation is always as given is given by -1 * (sourceOffset - destOffset)
Where the part in parenthesis represents the time zone difference.
Where the DateTime you want to convert is in UTC and called theDate
DateTime eastern = TimeZoneInfo
.ConvertTimeFromUtc(
theDate,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
If theDate has a Kind of Local this will throw, only accepting Kind values of Utc or Unspecified. Of course, moving from local to UTC is easy and we could even check and do this conversion when required, but since you say you want this conversion "without making conversion from our local time" I'm assuming you have your times in UTC and hence having the exception would be better as it would warn you that times that should be UTC are being treat as local and a bug could pop up elsewhere.
This will use Eastern Daylight Time when theDate is a time when EDT is in effect, as that is the normal rule for EST. To use EST even when it is the summer you can create your own TimeZoneInfo:
TimeZoneInfo
.ConvertTimeFromUtc(
theDate,
TimeZoneInfo.CreateCustomTimeZone(
"Eastern Standard Time No Daylight Savings",
new TimeSpan(-5, 0, 0),
"Eastern Standard Time No Daylight Savings",
"Eastern Standard Time No Daylight Savings"))

Why do I get a time one hour later using Atlantic Standard Time for St. Martin?

Ok - So right now for me it is 5:16pm Eastern. It's also 5:16pm in St. Martin. St. Martin is in the "Atlantic Standard Time" Zone.
When I use This value as a time zone, I get 6:16pm. I'm using the list linked below - any ideas why this isn't working?
http://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx
Most (but not all) locations in the North American Eastern Time zone are subject to daylight saving time. For example, New York, Miami, or Washington D.C. In these locations, the standard offset is UTC-5, which is used during the winter months, and the daylight offset is UTC-4, which is used during the summer months when daylight saving time is in effect.
Saint Martin, while in the Atlantic Time zone, does not use daylight saving time. It always follows Atlantic Standard Time, which uses an offset of UTC-4. Reference Here.
Note that some of the other locations in the Atlantic time zone, such as Bermuda and Nova Scotia, do use daylight saving time, and would have an offset of UTC-3 when daylight saving time is in effect.
Also, since you said you were using Windows time zones with the TimeZoneInfo class, you should be aware that the time zone having the ID of "Atlantic Standard Time", and having a display name of "(UTC-04:00) Atlantic Time (Canada)", does use daylight saving time.
For Saint Martin, you should instead use "SA Western Standard Time", which has a display name of "(UTC-04:00) Georgetown, La Paz, Manaus, San Juan", and is fixed to UTC-4. This is the correct mapping for Marigot, Saint Martin (America/Marigot), as documented in the Unicode CLDR Windows time zone mappings.
You're rigth it's (GMT-04:00) Atlantic Time (Canada) but you have to disable or enable daylight saving time. That's why you're getting a different value.

difference between "Pacific standard time(mexico)" and "Pacific standard time"

There are many time zones for single value of offset, is there any difference between these time zones for single value of offset.
If yes, then how to differntiate between these.
e.g. In my case I need "Pacific standard time" to be returned for offset value of "-08:00"
but I am getting "Pacific standard time(mexico)"
You can not differentiate on the offset alone; you need more information. That sort of thing usually is the result of differing rules for daylight saving time in the different areas.

Convert GMT Diff to .Net timezone?

My database has GMT Diff for every country like GMT+02:00 and GMT-04:00
Using C# .net is there a simple way to convert that to an actual an .Net timezone?
e.g. "Eastern Standard Time" or "E. Australia Standard Time"
thanks
You can find a timezone, just not that likely the one you want. The UTC offset is ambiguous. GMT+02:00 is the offset for
Jordan Standard Time
GTB Standard Time
Middle East Standard Time
Egypt Standard Time
Syria Standard Time
South Africa Standard Time
FLE Standard Time
Israel Standard Time
E. Europe Standard Time
Any you like in particular? You'd get them with an expression like this:
public static TimeZoneInfo[] GetTimeZones(TimeSpan offset) {
return TimeZoneInfo.GetSystemTimeZones().Where(z => z.BaseUtcOffset == offset).ToArray();
}
I had to look some of these up. FLE = Finland, Lithuania, Estonia. GTB is tougher, I'm guessing at Greece, Turkey, Bulgaria.
That's not going to be possible because different time zones can share the same offset from GMT (or UTC to be more correct).
If you're expecting "Eastern Standard Time" out of it, you need more information than just the offset from GMT. For example, GMT-5 is Eastern Standard Time, Central Daylight Time, Eastern Standard Time (Indiana), and various South American time zones, both with and without Daylight Saving, and on a different DST schedule than the US.
If you are ok with just the first available TimeZone which matches that GMT offset, that should be easy.
This is off my head but...
TimeZoneInfo getTimeZone(string gmtstring)
{
foreach(TimeZoneInfo ti in TimeZoneInfo.GetSystemTimeZones())
{
TimeSpan tsp = ti.BaseUtcOffset;
if(tsp.ToString((tsp.TotalMinutes < 0 ? "-" : "+")+"hh:mm") == gmtstring.Substring(3))
return ti;
}
return null;
}
Otherwise there's no direct relation between timezones and GMT offsets (there are many timezones on the same GMT offset, as the others said)
PS: gmtstring should be in the "GMT(+|-)HH:SS" format

How to convert time between timezones (UTC to EDT)?

I need to have a common function to convert UTC time to EDT. I have a server in India. An application in it needs to use EDT time for all time purposes.
I am using .NET 3.5.
I found this on some other forum.
DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
DateTime.UtcNow, "Eastern Standard Time");
When i tried with "Easten Daylight Time" I got an error.
"The time zone ID 'Eastern Daylight Time' was not found on the local computer".
Please help with this or any other solution.
Eastern Daylight Time isn't the name of a "full" time zone - it's "half" a time zone, effectively, always 4 hours behind UTC. (There may be proper terminology for this, but I'm not aware of it.)
Why would you want to use EDT for times which don't have daylight savings applied? If you want a custom time zone that always has the same offset to UTC, use TimeZoneInfo.CreateCustomTimeZone.
Note that if you use get the Eastern Standard timezone (TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")) then that will still have daylight saving time applied appropriately (i.e. during summer).
For example:
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
// Prints True
Console.WriteLine(tzi.IsDaylightSavingTime(new DateTime(2009, 6, 1)));
// Prints False
Console.WriteLine(tzi.IsDaylightSavingTime(new DateTime(2009, 1, 1)));
I would have said that you should use UTC for calculations of time periods, so that you avoid issues of daylight saving time and then use LocalTime for display only.
DateTime.ToLocalTime for UTC to whatever the local time zone is and then DateTime.ToUniversalTime to convert from local time to UTC.
Edit after comment 1
Do I take it then that you're after displaying a different timezone to that of the server?
If you're using web pages to access your server then use
HttpRequest.UserLanguages to help create a CultureInfo object and use that to parse your DateTime object.
Look here for a full explanation:Microsoft link on displaying local user time for web pages.
If you're using client-server architecture then if the LocalTime call is on the client side it will display the LocalTime for the client. You then convert it to UTC to send back to your server.
Either way your server doesn't need to know where the client is so if you have multiple clients in multiple timezones then all calculations will match. It will also allow you to show the times in any timezone that you wish by use of different Culture objects.
Edit 2 copied my second comment
You can get time data in UTC format from the server. Then you can convert it using DateTime.ToLocalTime or DateTime.ToUniversalTime as requried. If you're including dates as well and need to cope with say US MM/dd/yyyy and european dd/MM/yyyy formats the you can use CultureInfo class to parse the DateTime value accordingly. It sounds like more work than what you have at the moment, but it would mean that if you move your server again then you don't need to recode the DateTime handling.
A new point
Another point to look at is clock synchronisation between the server and the clients using NTP (Network Time Protocol) or SNTP (Simple Network Time Protocol) if it is accurate enough. I don't know what OS you are using but this is used by Windows Server time services to synchronise networks.
The cowboy method is to take the UTC time, subtract four hours' worth of seconds from it (the timezone offset), format it using a UTC formatting function, and slap a "EDT" label on it.
If you need to use Daylight Time sometimes and Standard Time other times, either make a lookup table of switchover dates, or use some calendar function.
TimeZoneInfo.ConvertTimeFromUtc will have correct offset depending on the DateTime you give it. For example:
3AM UTC/11PM ET (4 hour offset):
DateTime timeSummerET = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime("08/01/2019 03:00:00"), zoneET);
3AM UTC/10PM ET (5 hour offset):
DateTime timeWinterET = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime("12/01/2019 03:00:00"), zoneET);

Categories