2

我在一周中的不同时间收到订单,并且我有一套规则来确定我们何时可以接受和不能接受订单。例如,我们不接受星期三下午 4:45 到 5:15 之间的订单。因此,如果在下午 3 点收到订单,一切都很好。如果在下午 5 点收到订单,我们将需要拒绝它。这些规则基于星期几,并且每天都在变化。

我的问题是使用 joda 时间,检查当前时间是否在此时间窗口内的最佳方法是什么?

我对其他技术持开放态度,但是我目前正在通过 scala 工具时间包装器使用 joda time。

我目前正在处理这样的事情:

  val now = DateTime.now
  val wednesdayNoTradeInterval:Interval = ??

  now.getDayOfWeek match {
    case WEDNESDAY => wednesdayNoTradeInterval.contains(now)
  }
4

3 回答 3

2

你可以尝试这样的事情:

implicit def dateTimeToLocalTime(dateTime: DateTime) = dateTime.toLocalTime
type Interval[T] = (T, T)
def during(interval: Interval[LocalTime])(time: LocalTime) = interval match {
  case (start, end) => start < time && time < end
}

def day(day: Int)(time: DateTime) = time.dayOfWeek.get == day

val fourish = new LocalTime(16, 45)
val afternoon = during((fourish, (fourish + (30 minutes)) )) _
val wednesday = day(WEDNESDAY) _
val thursday = day(THURSDAY) _
def acceptingOrders = !{
  val now = DateTime.now
  wednesday(now) && afternoon(now)
  thursday(now) && afternoon(now)
}

并像这样使用它:

acceptingOrders  // ?
于 2012-12-14T20:03:58.297 回答
0

For each day of the week, store a list of pairs of LocalTime instances between which orders are rejected.

Then when an order is placed at a given DateTime, get the pairs associated with the dateTime's day of week, transform the DateTime into a LocalTime, and check if this LocalTime is contained between the start and end of at least one pair.

于 2012-12-14T18:30:03.947 回答
0

查看 JChronic 中的 Span 类:https ://github.com/samtingleff/jchronic/blob/master/src/main/java/com/mdimension/jchronic/utils/Span.java

创建类似的东西并执行 !<> 检查以毫秒为单位的时间将是微不足道的。

于 2012-12-14T18:27:48.647 回答