Get list of time zones for a given offset in minutes using Noda Time

Get list of time zones for a given offset in minutes using Noda Time



I am trying to design the following timezone solution using Noda Time:



A user would use a mobile app or web app to log in to the system. At the time of login a web API would be called with offset from UTC (let's say x minutes) as a parameter.



Now if the offset (x minutes) is different from offset (and timezone) saved in database, then the user would be shown a list of timezones which are x minutes away from UTC, so that they can select one out of them. The selected timezone and corresponding offset (x minutes) would then be saved in the database as the user's latest timezone.



How do I get a list of timezones which are x minutes away from UTC using Noda Time?



For example, if the user is +330 minutes away from UTC, then the user would get this prompt:



We have found that you're 5 hrs 30 minutes ahead of GMT. Please select your current timezone: "Asia/Colombo", "Asia/Kolkata"





I get what you are trying to do, but if you can work out how far from UTC the user is, can't you get their actual position, and therefore know what their current timezone is?
– Neil
Sep 1 at 7:50





@Neil, only common thing between different clients(mobile or web) is to unambiguously determine offset from UTC. There is no common identifier of a timezone across all platforms.
– Dipendu Paul
Sep 1 at 7:59





stackoverflow.com/questions/6939685/…
– Neil
Sep 1 at 8:23





Android: developer.android.com/reference/java/util/TimeZone iOS: developer.apple.com/documentation/foundation/timezone
– Neil
Sep 1 at 8:33





Let us continue this discussion in chat.
– Dipendu Paul
Sep 1 at 8:48




2 Answers
2



You can do something like this:


TimeZoneInfo.GetSystemTimeZones()
.Where(x => x.GetUtcOffset(DateTime.Now).TotalMinutes == 330)



And now you have a collection of the time zones! You can replace DateTime.Now with some other date or a DateTimeOffset depending on your situation.


DateTime.Now


DateTimeOffset



In Noda Time, you can do this:


using NodaTime;
using NodaTime.TimeZones;

TzdbDateTimeZoneSource.Default.GetIds()
.Select(x => TzdbDateTimeZoneSource.Default.ForId(x))
.Where(x =>
x.GetUtcOffset(SystemClock.Instance.GetCurrentInstant()).ToTimeSpan().TotalMinutes == 330)



A slightly alternative approach to Sweeper's code, using a target offset instead of converting each offset to a TimeSpan, using a single computation of "now" (for consistent results) and using the IDateTimeZoneProvider.GetAllZones extension method.


TimeSpan


IDateTimeZoneProvider.GetAllZones


using System;
using System.Linq;
using NodaTime;
using NodaTime.Extensions;

class Test

static void Main()

// No FromMinutes method for some reason...
var target = Offset.FromSeconds(330 * 60);
var now = SystemClock.Instance.GetCurrentInstant();
var zones = DateTimeZoneProviders.Tzdb.GetAllZones()
.Where(zone => zone.GetUtcOffset(now) == target);
foreach (var zone in zones)

Console.WriteLine(zone.Id);





Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?