Skip to content

Commit

Permalink
fix: parse the dates in a locale-agnostic manner
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimatespirit authored May 1, 2024
1 parent f71dca3 commit 1743f9f
Showing 1 changed file with 2 additions and 27 deletions.
29 changes: 2 additions & 27 deletions tradedangerous/plugins/eddblink_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,18 @@
import certifi
import csv
import datetime
from email.utils import parsedate_to_datetime
import os
import requests
import sqlite3
import ssl
import typing
import locale
import platform


if typing.TYPE_CHECKING:
from typing import Optional
from .. tradeenv import TradeEnv

# Find the first English UTF-8 locale for use in parsing timestamps.
LOCALE = None
if platform.system() == 'Windows':
for lang in locale.windows_locale.values():
if "en" in lang:
LOCALE = lang
break
else:
# for other operating systems
for lang in locale.locale_alias.values():
if "en" in lang and "UTF-8" in lang:
LOCALE = lang
break
if not LOCALE:
raise PluginException(
"Unable to find compatible locale.\n" +
"This plugin needs an English, UTF-8 compatible " +
"locale installed in order to function correctly.\n" +
"Please refer to your OS for instructions installing one."
)
# Constants
BASE_URL = os.environ.get('TD_SERVER') or "https://elite.tromador.com/files/"
CONTEXT=ssl.create_default_context(cafile=certifi.where())
Expand Down Expand Up @@ -188,10 +167,8 @@ def downloadFile(self, path):
self.tdenv.WARN("Problem with download:\n URL: {}\n Error: {}", url, str(e))
return False

locale.setlocale(locale.LC_ALL, LOCALE)
last_modified = response.headers.get("last-modified")
dump_mod_time = datetime.datetime.strptime(last_modified, "%a, %d %b %Y %H:%M:%S %Z").timestamp()
locale.setlocale(locale.LC_ALL, '')
dump_mod_time = parsedate_to_datetime(last_modified).timestamp()

if Path.exists(localPath):
local_mod_time = localPath.stat().st_mtime
Expand Down Expand Up @@ -352,8 +329,6 @@ def importListings(self, listings_file):
self.tdenv.NOTE("Finished processing market data. End time = {}", self.now())

def run(self):
self.tdenv.DEBUG2(f'Using "{LOCALE}" locale for parsing modified timestamps. Please include this information in any error reports.')

self.tdenv.ignoreUnknown = True

# Create the /eddb folder for downloading the source files if it doesn't exist.
Expand Down

0 comments on commit 1743f9f

Please sign in to comment.