简体   繁体   English

一整天(24 小时周期)的关闭/打开的 Noda 时间表示

[英]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属性startend解析为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?

  1. 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 小时时期?

  2. 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,那么从逻辑上讲,这将在一天的一个半小时内为“真”:

  • 00:00-00:30: False 00:00-00:30:错误
  • 00:30-01:30 (first time): True 00:30-01:30(第一次):真
  • 01:30-02:00 (first time): False 01:30-02:00(第一次):假
  • 01:00-01:30 (second time): True 01:00-01:30(第二次):真
  • 01:30-02:00 (second time): False 01:30-02:00(第二次):假
  • 02:00-00:00 (next day): False 02:00-00:00(次日):假

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:我将采用以下流程:

  • Work out detailed requirements, including what you want to happen on days with UTC offset transitions in the specific time zone (and think up tests at this stage)制定详细的要求,包括您希望在特定时区的 UTC 偏移转换的日子里发生什么(并在此阶段考虑测试)
  • Extract the logical values from those requirements in terms of Noda Time types (with the limitation that no, we unfortunately don't support 24:00:00 as a LocalTime )根据 Noda Time 类型从这些要求中提取逻辑值(限制是不,我们很遗憾不支持 24:00:00 作为LocalTime
  • Represent those types in your JSON as closely as possible尽可能接近地在 JSON 中表示这些类型
  • Make your code follow your requirements documentation as closely as possible, in terms of how it handles the data就如何处理数据而言,使您的代码尽可能地遵循您的需求文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM