File size: 1,541 Bytes
2d2c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import datetime
from typing import List, Dict
import pytz

# Timezone-aware scheduler for Asia/Tokyo
TZ = pytz.timezone('Asia/Tokyo')

def _local_today():
    return datetime.datetime.now(TZ)

def get_operator_availability(duration_minutes: int = 15) -> List[datetime.datetime]:
    now = _local_today()
    slots = []
    for d in range(1,6):
        day = now + datetime.timedelta(days=d)
        for hour_min in [(10,0),(11,0),(14,0),(15,30)]:
            slot = TZ.localize(datetime.datetime(day.year, day.month, day.day, hour_min[0], hour_min[1]))
            slots.append(slot)
    return slots


def find_common_slots(customer_windows: List[Dict], duration_minutes: int =15, max_candidates: int =3):
    """Given customer's preferred windows (list of dicts with 'start' and 'end' datetimes),
    return up to max_candidates slots that fit operator availability.
    customer_windows example: [{'start': datetime, 'end': datetime}, ...]
    Both operator slots and customer windows are assumed to be timezone-aware in Asia/Tokyo.
    Returns list of ISO strings in Asia/Tokyo timezone.
    """
    operator_slots = get_operator_availability(duration_minutes)
    candidates = []
    for s in operator_slots:
        for w in customer_windows:
            if w['start'] <= s <= w['end']:
                candidates.append(s)
                break
        if len(candidates) >= max_candidates:
            break
    if not candidates:
        candidates = operator_slots[:max_candidates]
    return [dt.isoformat() for dt in candidates]