private long getExpectedDelayNanos(RobotsTxt robotsTxt) {
   long delayNanos = millisToNanos(defaultDelay);
   if (isUsingRobotsTxtCrawlDelay(robotsTxt)) {
     delayNanos = TimeUnit.SECONDS.toNanos((long) (robotsTxt.getCrawlDelay()));
   } else {
     for (DelaySchedule schedule : schedules) {
       if (schedule.isCurrentTimeInSchedule()) {
         delayNanos = millisToNanos(schedule.getDelay());
         break;
       }
     }
   }
   return delayNanos;
 }
  @Override
  public void saveToXML(Writer out) throws IOException {
    try {
      EnhancedXMLStreamWriter writer = new EnhancedXMLStreamWriter(out);
      writer.writeStartElement("delay");
      writer.writeAttribute("class", getClass().getCanonicalName());
      writer.writeAttribute("default", Long.toString(defaultDelay));
      writer.writeAttribute("scope", scope);
      writer.writeAttribute("ignoreRobotsCrawlDelay", Boolean.toString(ignoreRobotsCrawlDelay));

      for (DelaySchedule schedule : schedules) {
        writer.writeStartElement("schedule");
        if (schedule.getDayOfWeekRange() != null) {
          writer.writeAttribute(
              "dayOfWeek",
              "from "
                  + schedule.getDayOfWeekRange().getMinimum()
                  + " to "
                  + schedule.getDayOfWeekRange().getMaximum());
        }
        if (schedule.getDayOfMonthRange() != null) {
          writer.writeAttribute(
              "dayOfMonth",
              "from "
                  + schedule.getDayOfMonthRange().getMinimum()
                  + " to "
                  + schedule.getDayOfMonthRange().getMaximum());
        }
        if (schedule.getTimeRange() != null) {
          writer.writeAttribute(
              "time",
              "from "
                  + schedule.getTimeRange().getLeft().toString("HH:mm")
                  + " to "
                  + schedule.getTimeRange().getRight().toString("HH:mm"));
        }
        writer.writeCharacters(Long.toString(schedule.getDelay()));
        writer.writeEndElement();
      }
      writer.writeEndElement();
      writer.flush();
      writer.close();
    } catch (XMLStreamException e) {
      throw new IOException("Cannot save as XML.", e);
    }
  }