# Path: fastavro/const.py
# MCS_PER_SECOND = 1000000
# 
# MLS_PER_SECOND = 1000
# 
# Path: fastavro/_logical_writers_py.py
# def prepare_timestamp_micros(data, schema):
#     """Converts datetime.datetime to int timestamp with microseconds"""
#     if isinstance(data, datetime.datetime):
#         if data.tzinfo is not None:
#             delta = data - epoch
#             return (
#                 delta.days * 24 * 3600 + delta.seconds
#             ) * MCS_PER_SECOND + delta.microseconds
# 
#         # On Windows, mktime does not support pre-epoch, see e.g.
#         # https://stackoverflow.com/questions/2518706/python-mktime-overflow-error
#         if is_windows:
#             delta = data - epoch_naive
#             return (
#                 delta.days * 24 * 3600 + delta.seconds
#             ) * MCS_PER_SECOND + delta.microseconds
#         else:
#             return (
#                 int(time.mktime(data.timetuple())) * MCS_PER_SECOND + data.microsecond
#             )
#     else:
#         return data
# 
# Path: fastavro/_logical_writers_py.py
# def prepare_timestamp_millis(data, schema):
#     """Converts datetime.datetime object to int timestamp with milliseconds"""
#     if isinstance(data, datetime.datetime):
#         if data.tzinfo is not None:
#             delta = data - epoch
#             return (delta.days * 24 * 3600 + delta.seconds) * MLS_PER_SECOND + int(
#                 delta.microseconds / 1000
#             )
# 
#         # On Windows, mktime does not support pre-epoch, see e.g.
#         # https://stackoverflow.com/questions/2518706/python-mktime-overflow-error
#         if is_windows:
#             delta = data - epoch_naive
#             return (delta.days * 24 * 3600 + delta.seconds) * MLS_PER_SECOND + int(
#                 delta.microseconds / 1000
#             )
#         else:
#             return int(time.mktime(data.timetuple())) * MLS_PER_SECOND + int(
#                 data.microsecond / 1000
#             )
#     else:
#         return data
# 
# Path: tests/conftest.py
# def assert_naive_datetime_equal_to_tz_datetime(naive_datetime, tz_datetime):
#     # mktime appears to ignore microseconds, so do this manually
#     microseconds = int(time.mktime(naive_datetime.timetuple())) * 1000 * 1000
#     microseconds += naive_datetime.microsecond
#     aware_datetime = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(
#         microseconds=microseconds
#     )
#     assert aware_datetime == tz_datetime


import datetime
import fastavro
import pytest
from io import BytesIO
from fastavro.const import MCS_PER_SECOND, MLS_PER_SECOND
from fastavro._logical_writers_py import prepare_timestamp_micros
from fastavro._logical_writers_py import prepare_timestamp_millis
from .conftest import assert_naive_datetime_equal_to_tz_datetime
from datetime import timezone
    assert original.tzinfo is not None
    read = read_data["timestamp-millis"]
    assert read.tzinfo is not None
    assert original == read
    read_in_test_tz = read.astimezone(tst)
    assert original == read_in_test_tz


def test_timestamp_micros_naive_input(timestamp_data_naive, read_data_naive):
    original = timestamp_data_naive["timestamp-micros"]
    assert original.tzinfo is None
    read = read_data_naive["timestamp-micros"]
    assert read.tzinfo is not None
    assert_naive_datetime_equal_to_tz_datetime(original, read)


def test_timestamp_millis_naive_input(timestamp_data_naive, read_data_naive):
    original = timestamp_data_naive["timestamp-millis"]
    assert original.tzinfo is None
    read = read_data_naive["timestamp-millis"]
    assert read.tzinfo is not None
    assert_naive_datetime_equal_to_tz_datetime(original, read)


def test_prepare_timestamp_micros():
    # seconds from epoch == 1234567890
    reference_time = datetime.datetime(2009, 2, 13, 23, 31, 30, tzinfo=timezone.utc)
    mcs_from_epoch = 1234567890 * MCS_PER_SECOND
    assert prepare_timestamp_micros(reference_time, schema) == mcs_from_epoch
    timestamp_tst = reference_time.astimezone(tst)

# Path: fastavro/const.py
# MCS_PER_SECOND = 1000000
# 
# MLS_PER_SECOND = 1000
# 
# Path: fastavro/_logical_writers_py.py
# def prepare_timestamp_micros(data, schema):
#     """Converts datetime.datetime to int timestamp with microseconds"""
#     if isinstance(data, datetime.datetime):
#         if data.tzinfo is not None:
#             delta = data - epoch
#             return (
#                 delta.days * 24 * 3600 + delta.seconds
#             ) * MCS_PER_SECOND + delta.microseconds
# 
#         # On Windows, mktime does not support pre-epoch, see e.g.
#         # https://stackoverflow.com/questions/2518706/python-mktime-overflow-error
#         if is_windows:
#             delta = data - epoch_naive
#             return (
#                 delta.days * 24 * 3600 + delta.seconds
#             ) * MCS_PER_SECOND + delta.microseconds
#         else:
#             return (
#                 int(time.mktime(data.timetuple())) * MCS_PER_SECOND + data.microsecond
#             )
#     else:
#         return data
# 
# Path: fastavro/_logical_writers_py.py
# def prepare_timestamp_millis(data, schema):
#     """Converts datetime.datetime object to int timestamp with milliseconds"""
#     if isinstance(data, datetime.datetime):
#         if data.tzinfo is not None:
#             delta = data - epoch
#             return (delta.days * 24 * 3600 + delta.seconds) * MLS_PER_SECOND + int(
#                 delta.microseconds / 1000
#             )
# 
#         # On Windows, mktime does not support pre-epoch, see e.g.
#         # https://stackoverflow.com/questions/2518706/python-mktime-overflow-error
#         if is_windows:
#             delta = data - epoch_naive
#             return (delta.days * 24 * 3600 + delta.seconds) * MLS_PER_SECOND + int(
#                 delta.microseconds / 1000
#             )
#         else:
#             return int(time.mktime(data.timetuple())) * MLS_PER_SECOND + int(
#                 data.microsecond / 1000
#             )
#     else:
#         return data
# 
# Path: tests/conftest.py
# def assert_naive_datetime_equal_to_tz_datetime(naive_datetime, tz_datetime):
#     # mktime appears to ignore microseconds, so do this manually
#     microseconds = int(time.mktime(naive_datetime.timetuple())) * 1000 * 1000
#     microseconds += naive_datetime.microsecond
#     aware_datetime = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(
#         microseconds=microseconds
#     )
#     assert aware_datetime == tz_datetime


# Path: tests/test_timezone.py
import datetime
import fastavro
import pytest
from io import BytesIO
from fastavro.const import MCS_PER_SECOND, MLS_PER_SECOND
from fastavro._logical_writers_py import prepare_timestamp_micros
from fastavro._logical_writers_py import prepare_timestamp_millis
from .conftest import assert_naive_datetime_equal_to_tz_datetime
from datetime import timezone

    assert original.tzinfo is not None
    read = read_data["timestamp-millis"]
    assert read.tzinfo is not None
    assert original == read
    read_in_test_tz = read.astimezone(tst)
    assert original == read_in_test_tz


def test_timestamp_micros_naive_input(timestamp_data_naive, read_data_naive):
    original = timestamp_data_naive["timestamp-micros"]
    assert original.tzinfo is None
    read = read_data_naive["timestamp-micros"]
    assert read.tzinfo is not None
    assert_naive_datetime_equal_to_tz_datetime(original, read)


def test_timestamp_millis_naive_input(timestamp_data_naive, read_data_naive):
    original = timestamp_data_naive["timestamp-millis"]
    assert original.tzinfo is None
    read = read_data_naive["timestamp-millis"]
    assert read.tzinfo is not None
    assert_naive_datetime_equal_to_tz_datetime(original, read)


def test_prepare_timestamp_micros():
    # seconds from epoch == 1234567890
    reference_time = datetime.datetime(2009, 2, 13, 23, 31, 30, tzinfo=timezone.utc)
    mcs_from_epoch = 1234567890 * MCS_PER_SECOND
    assert prepare_timestamp_micros(reference_time, schema) == mcs_from_epoch
    timestamp_tst = reference_time.astimezone(tst)

    assert prepare_timestamp_micros(reference_time, schema) == prepare_timestamp_micros(