#!/usr/bin/env python
"""Usage: next-solstice [TIMEZONE]"""

# Copyright ©2017-19 Klaus Alexander Seistrup <klaus@seistrup.dk>
# Copying policy: GPLv3+
#
# Requirements: Python 3, ephem, pytz

import sys
from os.path import basename

try:
    import ephem
    from pytz import timezone
    from pytz.exceptions import UnknownTimeZoneError
except ImportError as error:
    print(error, file=sys.stderr)
    sys.exit(1)

DEFAULT_TIMEZONE = 'Europe/Copenhagen'

if len(sys.argv) == 2 and sys.argv[1] in {'-h', '--help', '-help'}:
    ME = basename(sys.argv[0])
    print(f'usage: {ME} [TIMEZONE]  # default: {DEFAULT_TIMEZONE}')
    sys.exit(0)

try:
    LOCAL_TIMEZONE = timezone(
        sys.argv[1] if len(sys.argv) == 2 else DEFAULT_TIMEZONE
    )
except UnknownTimeZoneError as error:
    print(f'Unrecognized timezone {error}', file=sys.stderr)
    sys.exit(1)

SOLSTICE = ephem.next_solstice(ephem.now()).datetime()

SOLSTICE_UTC = SOLSTICE.replace(tzinfo=timezone('UTC'))
SOLSTICE_LOCAL = SOLSTICE_UTC.astimezone(LOCAL_TIMEZONE)

print('{:%F %T %Z}'.format(SOLSTICE_LOCAL))

sys.exit(0)
# eof