|
25 | 25 | from typing import List, Optional |
26 | 26 | from fastapi import Header, HTTPException |
27 | 27 | from Globals import getenv, get_default_agent |
| 28 | +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError |
28 | 29 | from datetime import datetime, timedelta |
29 | 30 | from fastapi import HTTPException |
30 | 31 | from agixtsdk import AGiXTSDK |
@@ -527,9 +528,50 @@ def send_magic_link( |
527 | 528 | raise HTTPException( |
528 | 529 | status_code=401, detail="Invalid MFA token. Please try again." |
529 | 530 | ) |
530 | | - expiration = datetime.now().replace( |
531 | | - hour=0, minute=0, second=0, microsecond=0 |
532 | | - ) + timedelta(days=1) |
| 531 | + |
| 532 | + # --- Start Calculation --- |
| 533 | + |
| 534 | + # 1. Get the timezone name from the environment variable |
| 535 | + tz_name = getenv("TZ") |
| 536 | + if not tz_name: |
| 537 | + print("Warning: TZ environment variable not set. Defaulting to UTC.") |
| 538 | + tz_name = "UTC" # Default to UTC if not set |
| 539 | + |
| 540 | + # 2. Get the timezone object |
| 541 | + try: |
| 542 | + server_tz = ZoneInfo(tz_name) |
| 543 | + except ZoneInfoNotFoundError: |
| 544 | + print(f"Warning: Timezone '{tz_name}' not found. Defaulting to UTC.") |
| 545 | + server_tz = ZoneInfo("UTC") # Default to UTC if invalid |
| 546 | + |
| 547 | + # 3. Get the current time *in the server's timezone* |
| 548 | + now = datetime.now(server_tz) |
| 549 | + |
| 550 | + # 4. Calculate the next month and year based on the current aware time |
| 551 | + current_year = now.year |
| 552 | + current_month = now.month |
| 553 | + |
| 554 | + next_month = current_month + 1 |
| 555 | + next_year = current_year |
| 556 | + if next_month > 12: |
| 557 | + next_month = 1 |
| 558 | + next_year += 1 |
| 559 | + |
| 560 | + # 5. Create the expiration datetime for the first day of the next month |
| 561 | + # at midnight *in the server's timezone*. |
| 562 | + # Crucially, associate the timezone directly during creation. |
| 563 | + expiration = datetime( |
| 564 | + year=next_year, |
| 565 | + month=next_month, |
| 566 | + day=1, |
| 567 | + hour=0, |
| 568 | + minute=0, |
| 569 | + second=0, |
| 570 | + microsecond=0, |
| 571 | + tzinfo=server_tz, # Associate the server's timezone |
| 572 | + ) |
| 573 | + # --- End Calculation --- |
| 574 | + |
533 | 575 | self.token = jwt.encode( |
534 | 576 | { |
535 | 577 | "sub": str(user.id), |
|
0 commit comments