2

I have orders that come in it at various times during the week and I have a set of rules for when we can and cannot accept orders. For example, we don't accept orders between 4:45pm and 5:15pm on Wednesday. So if an order came in at 3pm, everything is fine. If an order came in at 5pm we would need to reject it. These rules are based on the day of the week and change daily.

My question, is using joda time, what is the best way to check if the current time is in this time window?

I'm open to other technologies, however I'm currently using joda time through the scala tools time wrapper.

I'm currently working with something like this:

  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

Check out the Span class in JChronic: https://github.com/samtingleff/jchronic/blob/master/src/main/java/com/mdimension/jchronic/utils/Span.java

It would be trivial to create something similar and do a !<> check on the time in millis.

于 2012-12-14T18:27:48.647 に答える
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 に答える