Files
gogo2/test_timezone_handling.py
Dobromir Popov e2c495d83c cleanup
2025-07-27 18:31:30 +03:00

153 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify timezone handling in data provider
"""
import time
import logging
import pandas as pd
from datetime import datetime, timezone
from core.data_provider import DataProvider
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_timezone_handling():
"""Test timezone handling in data provider"""
logger.info("Testing timezone handling...")
# Initialize data provider
dp = DataProvider()
# Wait for initial data load
logger.info("Waiting for initial data load...")
time.sleep(15)
# Test 1: Check timezone info in cached data
logger.info("\n=== Test 1: Timezone Info in Cached Data ===")
for symbol in ['ETH/USDT', 'BTC/USDT']:
for timeframe in ['1s', '1m', '1h', '1d']:
if symbol in dp.cached_data and timeframe in dp.cached_data[symbol]:
df = dp.cached_data[symbol][timeframe]
if not df.empty:
# Check if index has timezone info
has_tz = df.index.tz is not None
tz_info = df.index.tz if has_tz else "No timezone"
# Get first and last timestamps
first_ts = df.index[0]
last_ts = df.index[-1]
logger.info(f"{symbol} {timeframe}:")
logger.info(f" Timezone: {tz_info}")
logger.info(f" First: {first_ts}")
logger.info(f" Last: {last_ts}")
# Check for gaps (only for timeframes with enough data)
if len(df) > 10:
# Calculate expected time difference
if timeframe == '1s':
expected_diff = pd.Timedelta(seconds=1)
elif timeframe == '1m':
expected_diff = pd.Timedelta(minutes=1)
elif timeframe == '1h':
expected_diff = pd.Timedelta(hours=1)
elif timeframe == '1d':
expected_diff = pd.Timedelta(days=1)
# Check for large gaps
time_diffs = df.index.to_series().diff()
large_gaps = time_diffs[time_diffs > expected_diff * 2]
if not large_gaps.empty:
logger.warning(f" Found {len(large_gaps)} large gaps:")
for gap_time, gap_size in large_gaps.head(3).items():
logger.warning(f" Gap at {gap_time}: {gap_size}")
else:
logger.info(f" No significant gaps found")
else:
logger.info(f"{symbol} {timeframe}: No data")
# Test 2: Compare with current UTC time
logger.info("\n=== Test 2: Compare with Current UTC Time ===")
current_utc = datetime.now(timezone.utc)
logger.info(f"Current UTC time: {current_utc}")
for symbol in ['ETH/USDT', 'BTC/USDT']:
# Get latest 1m data
if symbol in dp.cached_data and '1m' in dp.cached_data[symbol]:
df = dp.cached_data[symbol]['1m']
if not df.empty:
latest_ts = df.index[-1]
# Convert to UTC if it has timezone info
if latest_ts.tz is not None:
latest_utc = latest_ts.tz_convert('UTC')
else:
# Assume it's already UTC if no timezone
latest_utc = latest_ts.replace(tzinfo=timezone.utc)
time_diff = current_utc - latest_utc
logger.info(f"{symbol} latest data:")
logger.info(f" Timestamp: {latest_ts}")
logger.info(f" UTC: {latest_utc}")
logger.info(f" Age: {time_diff}")
# Check if data is reasonably fresh (within 1 hour)
if time_diff.total_seconds() < 3600:
logger.info(f" ✅ Data is fresh")
else:
logger.warning(f" ⚠️ Data is stale (>{time_diff})")
# Test 3: Check data continuity
logger.info("\n=== Test 3: Data Continuity Check ===")
for symbol in ['ETH/USDT', 'BTC/USDT']:
if symbol in dp.cached_data and '1h' in dp.cached_data[symbol]:
df = dp.cached_data[symbol]['1h']
if len(df) > 24: # At least 24 hours of data
# Get last 24 hours
recent_df = df.tail(24)
# Check for 3-hour gaps (the reported issue)
time_diffs = recent_df.index.to_series().diff()
three_hour_gaps = time_diffs[time_diffs >= pd.Timedelta(hours=3)]
logger.info(f"{symbol} 1h data (last 24 candles):")
logger.info(f" Time range: {recent_df.index[0]} to {recent_df.index[-1]}")
if not three_hour_gaps.empty:
logger.warning(f" ❌ Found {len(three_hour_gaps)} gaps >= 3 hours:")
for gap_time, gap_size in three_hour_gaps.items():
logger.warning(f" {gap_time}: {gap_size}")
else:
logger.info(f" ✅ No 3+ hour gaps found")
# Show time differences
logger.info(f" Time differences (last 5):")
for i, (ts, diff) in enumerate(time_diffs.tail(5).items()):
if pd.notna(diff):
logger.info(f" {ts}: {diff}")
# Test 4: Manual timezone conversion test
logger.info("\n=== Test 4: Manual Timezone Conversion Test ===")
# Create test timestamps
utc_now = datetime.now(timezone.utc)
local_now = datetime.now()
logger.info(f"UTC now: {utc_now}")
logger.info(f"Local now: {local_now}")
logger.info(f"Difference: {utc_now - local_now.replace(tzinfo=timezone.utc)}")
# Test pandas timezone handling
test_ts = pd.Timestamp.now(tz='UTC')
logger.info(f"Pandas UTC timestamp: {test_ts}")
# Clean shutdown
logger.info("\n=== Shutting Down ===")
dp.stop_automatic_data_maintenance()
logger.info("Timezone handling test completed")
if __name__ == "__main__":
test_timezone_handling()