Skip to content

Instantly share code, notes, and snippets.

@qhwa
Last active September 11, 2024 16:01
Show Gist options
  • Save qhwa/63a9b6f9f46601828de20a6f15c8aec8 to your computer and use it in GitHub Desktop.
Save qhwa/63a9b6f9f46601828de20a6f15c8aec8 to your computer and use it in GitHub Desktop.
Virtual Delivery Windows

Daily Delivery Window Calculation

Background

In Zubale, each delivery order is assigned a specific time window during which the delivery must be completed. These windows typically span a few hours within the same day, for example, from 2:00 PM to 5:00 PM on 2nd March, 2024. Such windows have defined start and end times.

However, some orders may have delivery windows that span across multiple days. For instance:

start time end time
2024-05-02 09:00:00 2024-05-03 18:00:00

This indicates that the corresponding order can be delivered any time between 9 AM on May 2nd and 6 PM on May 3rd, 2024.

Problem Statement

Our Order Handler system, which dispatches orders to users for fulfillment, only processes orders scheduled for delivery today. It operates within a fixed daily window from 8 AM to 8 PM.

Given this constraint, we need to compute a "virtual delivery window" for orders whose delivery spans multiple days to ensure they align with the Order Handler's operational window.

For example, given an order with the delivery window specified above, the virtual delivery windows would be as follows:

  • On day one (2024-05-02), the virtual delivery window is 2024-05-02 09:00:00Z to 2024-05-02 20:00:00Z.
  • On day two (2024-05-03), the virtual delivery window is 2024-05-03 08:00:00Z to 2024-05-03 18:00:00Z.

Your task is to write a module that calculates and updates the delivery window information for an order based on this requirement.

Specification

Input and output

Your module should define a function that accepts an order in JSON format and returns the updated JSON data with the adjusted delivery windows.

Example input:

{
  "pickingAndDelivery": {
    "deliveryWindow": [
      "2024-05-02 09:00:00Z",
      "2024-05-03 18:00:00Z"
    ]
  }
}

On day 1, the output is expected to be:

{
  "pickingAndDelivery": {
    "deliveryWindow": [
      "2024-05-02 09:00:00Z",
      "2024-05-02 20:00:00Z"
    ]
  }
}

On day 2, the output is expected to be:

{
  "pickingAndDelivery": {
    "deliveryWindow": [
      "2024-05-03 08:00:00Z",
      "2024-05-03 18:00:00Z"
    ]
  }
}

Timezone

All times are in UTC.

Same-day Cases

The change only apply to delivery windows that crosses days. If a delivery window fits inside the same day, it will not be changed, no matter whether they are extending the operation window (8 AM to 8 PM) or not.

Testing

You are welcome to write an integration test for your module to verify its functionality.

Hope you enjoy solving this problem. Don't hessitate to ask any question if in doubt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment