Мне нужно создать время в формате UTC, соответствующее определенному местоположению в любой точке мира, чтобы, когда клиент переводит время локально, оно выдавало ожидаемое время.
/>
Позвольте мне быть более конкретным.
Пользователь находится в Париже, смещение UTC +1.
Мне нужно убедиться, что < strong>время, отображаемое в пользовательском интерфейсе,
Код: Выделить всё
0300
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
Код: Выделить всё
0200
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}.");
}
Код: Выделить всё
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