Мне нужно создать время в формате UTC, соответствующее определенному местоположению в любой точке мира, чтобы, когда клиент переводит время локально, оно выдавало ожидаемое время.
/>
Позвольте мне быть более конкретным.
Пользователь находится в Париже, смещение UTC +1.
Мне нужно убедиться, что < strong>время, отображаемое в пользовательском интерфейсе,
Код: Выделить всё
0300But what I send in the JSON payload to be the UTC offset that will produce that.
So, in this case, it needs to be
Код: Выделить всё
0200This 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}.");
}
Код: Выделить всё
NextRunTimeКод: Выделить всё
HourКод: Выделить всё
3Код: Выделить всё
MinuteКод: Выделить всё
0Код: Выделить всё
0300Код: Выделить всё
9Код: Выделить всё
HourКод: Выделить всё
45Код: Выделить всё
MinuteКод: Выделить всё
public class NextRunTime
{
public required int Hour { get; set; }
public required int Minute { get; set; }
public required string Type { get; set; }
}
Источник: https://stackoverflow.com/questions/781 ... local-time
Мобильная версия