[英]Noda time representation for close/open that is an entire day (24 hour period)
I am parsing some interestingly formatted data from https://raw.githubusercontent.com/QuantConnect/Lean/master/Data/market-hours/market-hours-database.json我正在解析来自https://raw.githubusercontent.com/QuantConnect/Lean/master/Data/market-hours/market-hours-database.json的一些有趣的格式化数据
It contains a snippet (removing some days) as below:它包含一个片段(删除了一些天),如下所示:
"Future-cme-[*]": {
"dataTimeZone": "UTC",
"exchangeTimeZone": "America/Chicago",
"monday": [
{
"start": "00:00:00",
"end": "1.00:00:00",
"state": "market"
}
],
"friday": [
{
"start": "00:00:00",
"end": "16:00:00",
"state": "market"
}
]
}
I am using a JsonConverter<LocalTime>
to convert the above, and I can parse the friday
properties start
and end
without any issues into a LocalTime
.我正在使用
JsonConverter<LocalTime>
来转换上述内容,并且可以将friday
属性start
和end
解析为LocalTime
,没有任何问题。
However, the dates which this represents as an entire day, ie 1.00:00:00
it throws an error on as it is not an ISO format - this opens up questions on my (incorrect.) use of the structures.但是,这代表一整天的日期,即
1.00:00:00
它会引发错误,因为它不是 ISO 格式 - 这引发了我(不正确)使用结构的问题。
Currently I have the format that uses the LocalTime
as below:目前我有使用
LocalTime
的格式如下:
public class MarketHoursSegment
{
public LocalTime Start { get; init; }
public LocalTime End { get; init; }
public MarketHoursState State { get; init; }
}
And the formatter:和格式化程序:
public class LocalTimeConverter : JsonConverter<LocalTime>
{
public override LocalTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return LocalTimePattern
.GeneralIso
.Parse(reader.GetString())
.GetValueOrThrow();
}
public override void Write(Utf8JsonWriter writer, LocalTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
Is there a preferred way to deal with a LocalTime that represents a 24 hours span?是否有首选方法来处理代表 24 小时跨度的 LocalTime?
Would I detect 1:00:00:00
in the reader.GetString()
of the converter, and if so set to 00:00:00 (midnight) and check if Start == End
then we know it is an entire 24 hour period?我会在转换器的
reader.GetString()
中检测到1:00:00:00
,如果设置为 00:00:00(午夜)并检查Start == End
那么我们知道它是整个 24 小时时期?
Or would it be more correct to have the Start
as a LocalTime
, and a Duration for the hours (ie 24 hours) with End => Start + Duration
?或者将
Start
设置为LocalTime
,并将 Duration 设置为End => Start + Duration
的小时数(即 24 小时)是否更正确?
Is there a preferred way to deal with a LocalTime that represents a 24 hours span?
是否有首选方法来处理代表 24 小时跨度的 LocalTime?
It's worth taking a step back and separating different concepts very carefully and being precise.值得退后一步,非常仔细地区分不同的概念并保持精确。 A
LocalTime
doesn't represent a 24 hour span - it's just a time of day. LocalTime
不代表 24 小时跨度 - 它只是一天中的某个时间。 Two LocalTime
values could effectively represent a 24 hour span without reference to a specific date, yes.两个
LocalTime
值可以有效地表示 24 小时跨度而不参考特定日期,是的。
If you can possibly change your JSON to use 00:00:00
, and then treat a "start==end" situation as being the full day, that's what I'd do.如果您可以将 JSON 更改为使用
00:00:00
,然后将“开始==结束”情况视为一整天,这就是我要做的。 That does mean, however, that you can never represent an empty period.然而,这确实意味着你永远不能代表一个空的时期。
Now, in terms of whether you should use a start and duration... that really depends on what you're trying to model.现在,就您是否应该使用开始和持续时间而言......这实际上取决于您尝试 model 的内容。 Are you trying to model a start time and an end time, or a start time and a duration?
您是在尝试 model 开始时间和结束时间,还是开始时间和持续时间? So far you've referred to the whole day as "a 24 hour span" but that's not always the case, if you're dealing with time zones that have UTC offset transitions (eg due to daylight saving time).
到目前为止,您将一整天称为“24 小时跨度”,但如果您正在处理具有 UTC 偏移转换的时区(例如由于夏令时),情况并非总是如此。
Transitions already cause potential issues with local intervals like this - if you're working on a date where the local time "falls back" from 2am to 1am, and you've got a local time period of (say) 00:30 to 01:30, then logically that will be "true" for an hour and a half of the day:转换已经导致像这样的本地时间间隔的潜在问题 - 如果您正在处理当地时间从凌晨 2 点到凌晨 1 点“回落”的日期,并且您的本地时间段为(例如)00:30 到 01 :30,那么从逻辑上讲,这将在一天的一个半小时内为“真”:
We don't really know what you're doing with the periods, but that's the sort of thing you need to be considering... likewise if you represent something as "00:00 for 24 hours" how does that work on a day which is only 23 hours long, or one that is 25 hours long?我们真的不知道你在做什么,但这是你需要考虑的事情......同样,如果你表示“24小时00:00”这在一天中是如何工作的哪个只有 23 小时,还是 25 小时? It will very much depend on exactly what you do with the data.
这在很大程度上取决于您对数据的处理方式。
I would adopt a process of:我将采用以下流程:
LocalTime
)LocalTime
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.