Время смещения UTC, которое будет оцениваться как определенное местное время.C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Гость
 Время смещения UTC, которое будет оцениваться как определенное местное время.

Сообщение Гость »


Мне нужно создать время в формате UTC, соответствующее определенному местоположению в любой точке мира, чтобы, когда клиент переводит время локально, оно выдавало ожидаемое время.
/>
Позвольте мне быть более конкретным.
Пользователь находится в Париже, смещение UTC +1.
Мне нужно убедиться, что < strong>время, отображаемое в пользовательском интерфейсе,.
But what I send in the JSON payload to be the UTC offset that will produce that.
So, in this case, it needs to be in the UTC time.
This is what I've got started, but I'm pretty green to date time operations like this, so I'm not quite sure I'm even building the foundation right.

Код: Выделить всё

public static DateTime ToUtcOffsetFromBaseTime(this NextRunTime nextRunTime, Base? staffBase)
{
ArgumentNullException.ThrowIfNull(staffBase, nameof(staffBase));

var instant = DateTime.UtcNow.ToInstant();
var baseOffset = staffBase.OffsetAsOf(instant);

if (baseOffset.HasValue)
{
var baseOffsetInstant = instant.WithOffset(baseOffset.Value);

// perform the time modifications (i.e., set to 0300 if configured as such)

// offset it to the relative UTC time and return
}

throw new Exception($"The base offset for {staffBase.BaseCode} could not be found for the date/time {instant}.");
}
The

Код: Выделить всё

NextRunTime
has the hour and minute that it needs to represent locally. So you might have an of and a of such that it needs to represent local time when the client converts it to local time. Or, you might have something like for the and for the .

Код: Выделить всё

public class NextRunTime
{
public required int Hour { get; set; }

public required int Minute { get; set; }

public required string Type { get; set; }
}
But, I'm not sure where to go from here honestly, and I'm not sure what I've even started with is right.


Источник: https://stackoverflow.com/questions/781 ... local-time
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»