What's the equivalent in C# of TimeZone.getDefault().getID() in Java? - c#

For one lives in Republic of Molossia, the following code in Java:
String TimeZoneId=TimeZone.getDefault().getID();
System.out.println(TimeZoneId);
will print
America/Regina
I tried TimeZoneInfo.GetSystemTimeZones() but the return value doesn't seem a tzdb id. Is there an equivalent method in C#?

.Net does not natievely support the TZDB, because Microsoft has it's own time zone database. See the TimeZone tag wiki for details.
If you want to work with TZDB zones, use the NodaTime library. The exact translation of what you asked is:
using NodaTime;
...
var timeZoneId = DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
Console.WriteLine(timeZoneId);

Typically this can be done using the TimeZone class. This is the closest that you will get with the native libraries from my understanding. Why do you need that specific result?
Take a look at Microsoft Time Zones and the Conversion Table* for the Windows Equivalent as Windows does not fully support the TZID, take a look at the stack overflow question below for more information. I hope this is more useful as I misread your previous question!!! - sorry!
//Get A reference to our timezone object
TimeZone t = TimeZone.CurrentTimeZone;
//Print out the display name
Console.WriteLine(t.StandardName);
MSDN REFERENCE
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html
Please refer this previous stackoverflow question
.NET TimeZoneInfo from Olson time zone

Similar value is returned by
Calendar.GetTimeZone
for more information: https://learn.microsoft.com/en-us/uwp/api/windows.globalization.calendar.gettimezone?view=winrt-20348

Related

Is it possible to convert short time zone strings as EST CET PST to TimeZoneInfo in C#?

The short time zone string I'm getting it as input. I'm trying to avoid to hardcode myself a huge mapping.
You can't do it. Because for some abbreviations we have several full names:
"AMT" means UTC+4 (Armenia Time) also "AMT" means UTC-4 (Amazon Time). That's why Microsoft didn't add this in his TimeZoneInfo class. I think it's much better to talk with the customer and decide which dictionary you will use.
You can find the list of abbreviations here: https://www.timeanddate.com/time/zones/
Sorry for the bad news.
Unfortunately it doesn't look like there's a built-in way of doing it. You might find the answer given here to be the simplest solution

ASP.NET Core DateTime - ToLocalTime vs ConvertTime

What is the correct way of converting UTC DateTime to local time (CET)? Should I use System.DateTime.ToLocalTime() or TimeZoneInfo.ConvertTime()? Are there any differences? Or are they just two methods internally calling each other?
Both methods should work just fine, I don't think either one is more correct than the other.
The most obvious difference in their standard usage is that System.DateTime.ToLocalTime() uses a local timezone provided by the system, while TimeZoneInfo.ConvertTime() uses whatever timezone you give it (e.g. you hardcode CET).
In both cases you should pay attention to the Kind property, which can sometimes ruin your day.
Anyway, you might want to check this question and of course the MSDN documentation of both methods, which sums up their behavior quite well.

IANA/Olson timezone support in C#

C# supports different timezone id's across the globe. Please find list of time zones that are being supported by C# in below link:
https://msdn.microsoft.com/en-us/library/gg154758.aspx
The timezone id's are used in C# library functions to convert times across the timezones.
[e.g. TimeZoneInfo.ConvertTimeBySystemTimeZoneId("Hawaiian Standard Time")]
Similarly I want support for AMERICA/MIQUELON, which is not present in the msdn list provided in above link.
Can somebody please provide workaround for this specific timezone?
Time zone identifiers like "America/Miquelon" and the others you listed (before editing your question) are from the IANA time zone database. You can read more in the timezone tag wiki and on Wikipedia.
Note that they are usually presented in mixed case form, rather than in all capital letters.
The easiest and best way to work with these in .NET is via the Noda Time library.
For example:
DateTimeZone tz = DateTimeZoneProviders.Tzdb["America/Miquelon"];
Instant now = SystemClock.Instance.Now;
ZonedDateTime converted = now.InZone(tz);

How to find Current Time of different culture i.e different timezone in c# asp.net?

Like DateTime.Now gives us current date and time with respect to current system.
How can we find Current Time of different culture i.e different timezone using c# asp.net???
I find SystemTimeToTzSpecificLocalTime Function but how can i used this one???
If you're using .NET 3.5 or later, you can use TimeZoneInfo:
TimeZoneInfo otherZone = ...;
DateTime otherZoneTimeNow = TimeZoneInfo.ConvertTime(DateTime.UtcNow, otherZone);
You need to be somewhat careful using TimeZoneInfo - different DateTime "kinds" do different things - you should read the docs for any call you make carefully. (I recently blogged about the problems with DateTime... TimeZoneInfo basically has to handle the ambiguity.)
You can use a service, like the Yahoo! Web Services API, to pull this information and more. You can get time zone, local time, population, etc. with the Yahoo! Web Services API. The biggest drawback to that approach is the maximum daily hit count.
You can check out this link for more details:
http://developer.yahoo.com/flash/maps/classreference/com/yahoo/maps/webservices/geocoder/GeocoderResult.html

How to convert UTC time to Time in any other time zone in C#

I am working in C#.net - .Net fx is 2.0 which doesnot support converting between different time zones. I have wrote a scheduler based on UTC but it is giving errors of 1 hour in the DTS periods for London. I need some solution so that I can gat the correct time in any timezone relative to UTC with correct DST adjustments.
Is changing to .NET 3.5 absolutely out of the question? It would make your life much, much easier. Otherwise, you're stuck with the plain TimeZone and DaylightSavings classes, as well as having to fetch the known timezones using P/Invoke.
William Stacey has a blog post with some code to do this - but I haven't tried it, so can't vouch for its accuracy. (In my experience he's usually pretty good though :) There are no doubt similar bits of code around if that one doesn't help you.
I believe that the API he's using doesn't have access to historical data, btw. In other words, it will assume that DST always kicks in on the first Sunday of October (or whatever the rule is) rather than knowing that the rule has changed over time. TimeZoneInfo in .NET 3.5 supports historical data where the OS does.

Categories

Resources