Skip to content

Commit

Permalink
TMP: refactor(JP): replace arrow with datatime library
Browse files Browse the repository at this point in the history
  • Loading branch information
shuuji3 committed Mar 22, 2024
1 parent 83530fc commit ce499ee
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions parsers/JP.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3
from datetime import datetime, timedelta
from logging import Logger, getLogger
from zoneinfo import ZoneInfo

# The arrow library is used to handle datetimes
import arrow
import pandas as pd
from requests import Session

Expand Down Expand Up @@ -36,7 +35,7 @@
"JP-ON": "www.okiden.co.jp/denki/",
}
ZONES_ONLY_LIVE = ["JP-TK", "JP-CB", "JP-SK"]

TIMEZONE = ZoneInfo('Asia/Tokyo')

def get_wind_capacity(datetime: datetime, zone_key, logger: Logger):
ZONE_CONFIG = ZONES_CONFIG[zone_key]
Expand Down Expand Up @@ -156,7 +155,12 @@ def fetch_consumption_df(
"""
if target_datetime is not None and zone_key in ZONES_ONLY_LIVE:
raise NotImplementedError("This parser can only fetch live data")
datestamp = arrow.get(target_datetime).to("Asia/Tokyo").strftime("%Y%m%d")

if target_datetime is None:
datestamp = datetime.now(tz=TIMEZONE).strftime("%Y%m%d")
else:
datestamp = target_datetime.replace(tzinfo=TIMEZONE).strftime("%Y%m%d")

consumption_url = {
"JP-HKD": f"http://denkiyoho.hepco.co.jp/area/data/juyo_01_{datestamp}.csv",
"JP-TH": f"https://setsuden.nw.tohoku-epco.co.jp/common/demand/juyo_02_{datestamp}.csv",
Expand Down Expand Up @@ -210,9 +214,14 @@ def fetch_consumption_forecast(
# Currently past dates not implemented for areas with no date in their demand csv files
if target_datetime and zone_key == "JP-HKD":
raise NotImplementedError("Past dates not yet implemented for selected region")
datestamp = arrow.get(target_datetime).to("Asia/Tokyo").strftime("%Y%m%d")

if target_datetime is None:
datestamp = datetime.now(tz=TIMEZONE).strftime("%Y%m%d")
else:
datestamp = target_datetime.replace(tzinfo=TIMEZONE).strftime("%Y%m%d")

# Forecasts ahead of current date are not available
if datestamp > arrow.get().to("Asia/Tokyo").strftime("%Y%m%d"):
if datestamp > datetime.now(tz=TIMEZONE).strftime("%Y%m%d"):
raise NotImplementedError(
"Future dates(local time) not implemented for selected region"
)
Expand Down Expand Up @@ -329,26 +338,21 @@ def fetch_price(
return data


def parse_dt(row):
def parse_dt(row) -> datetime:
"""Parses timestamps from date and time."""
if "AM" in row["Time"] or "PM" in row["Time"]:
timestamp = (
arrow.get(
" ".join([row["Date"], row["Time"]]).replace("/", "-"),
"YYYY-M-D H:mm A",
)
.replace(tzinfo="Asia/Tokyo")
.datetime
)
date = row["Date"]
time = row["Time"]
datetime_str = f"{date} {time}".replace("/", "-")
if "AM" in time or "PM" in time:
return datetime.strptime(
datetime_str,
"%Y-%m-%d %I:%M %p"
).replace(tzinfo=ZoneInfo('Asia/Tokyo'))
else:
timestamp = (
arrow.get(
" ".join([row["Date"], row["Time"]]).replace("/", "-"), "YYYY-M-D H:mm"
)
.replace(tzinfo="Asia/Tokyo")
.datetime
)
return timestamp
return datetime.strptime(
datetime_str,
"%Y-%m-%d %H:%M"
).replace(tzinfo=ZoneInfo('Asia/Tokyo'))


if __name__ == "__main__":
Expand Down

0 comments on commit ce499ee

Please sign in to comment.