Use parse_datetime API

This commit is contained in:
Ivan Kravets
2022-03-28 18:18:51 +03:00
parent 927c5c5e36
commit 1e2afafbc4
3 changed files with 19 additions and 25 deletions

View File

@ -14,13 +14,13 @@
# pylint: disable=unused-argument # pylint: disable=unused-argument
import datetime
import json import json
import re import re
import click import click
from tabulate import tabulate from tabulate import tabulate
from platformio import util
from platformio.clients.account import AccountClient, AccountNotAuthorized from platformio.clients.account import AccountClient, AccountNotAuthorized
@ -244,12 +244,9 @@ def print_packages(packages):
data = [] data = []
expire = "-" expire = "-"
if "subscription" in package: if "subscription" in package:
expire = datetime.datetime.strptime( expire = util.parse_datetime(
( package["subscription"].get("end_at")
package["subscription"].get("end_at") or package["subscription"].get("next_bill_at")
or package["subscription"].get("next_bill_at")
),
"%Y-%m-%dT%H:%M:%SZ",
).strftime("%Y-%m-%d") ).strftime("%Y-%m-%d")
data.append(("Expire:", expire)) data.append(("Expire:", expire))
services = [] services = []
@ -274,21 +271,17 @@ def print_subscriptions(subscriptions):
click.secho(subscription.get("product_name"), bold=True) click.secho(subscription.get("product_name"), bold=True)
click.echo("-" * len(subscription.get("product_name"))) click.echo("-" * len(subscription.get("product_name")))
data = [("State:", subscription.get("status"))] data = [("State:", subscription.get("status"))]
begin_at = datetime.datetime.strptime( begin_at = util.parse_datetime(subscription.get("begin_at")).strftime("%c")
subscription.get("begin_at"), "%Y-%m-%dT%H:%M:%SZ"
).strftime("%Y-%m-%d %H:%M:%S")
data.append(("Start date:", begin_at or "-")) data.append(("Start date:", begin_at or "-"))
end_at = subscription.get("end_at") end_at = subscription.get("end_at")
if end_at: if end_at:
end_at = datetime.datetime.strptime( end_at = util.parse_datetime(subscription.get("end_at")).strftime("%c")
subscription.get("end_at"), "%Y-%m-%dT%H:%M:%SZ"
).strftime("%Y-%m-%d %H:%M:%S")
data.append(("End date:", end_at or "-")) data.append(("End date:", end_at or "-"))
next_bill_at = subscription.get("next_bill_at") next_bill_at = subscription.get("next_bill_at")
if next_bill_at: if next_bill_at:
next_bill_at = datetime.datetime.strptime( next_bill_at = util.parse_datetime(
subscription.get("next_bill_at"), "%Y-%m-%dT%H:%M:%SZ" subscription.get("next_bill_at")
).strftime("%Y-%m-%d %H:%M:%S") ).strftime("%c")
data.append(("Next payment:", next_bill_at or "-")) data.append(("Next payment:", next_bill_at or "-"))
data.append( data.append(
("Edit:", click.style(subscription.get("update_url"), fg="blue") or "-") ("Edit:", click.style(subscription.get("update_url"), fg="blue") or "-")

View File

@ -462,7 +462,7 @@ def lib_show(library, json_output):
"Version: %s, released %s" "Version: %s, released %s"
% ( % (
lib["version"]["name"], lib["version"]["name"],
time.strftime("%c", util.parse_date(lib["version"]["released"])), util.parse_datetime(lib["version"]["released"]).strftime("%c"),
) )
) )
click.echo("Manifest: %s" % lib["confurl"]) click.echo("Manifest: %s" % lib["confurl"])
@ -504,7 +504,7 @@ def lib_show(library, json_output):
"Versions", "Versions",
[ [
"%s, released %s" "%s, released %s"
% (v["name"], time.strftime("%c", util.parse_date(v["released"]))) % (v["name"], util.parse_datetime(v["released"]).strftime("%c"))
for v in lib["versions"] for v in lib["versions"]
], ],
) )
@ -551,7 +551,7 @@ def lib_stats(json_output):
tabular_data = [ tabular_data = [
( (
click.style(item["name"], fg="cyan"), click.style(item["name"], fg="cyan"),
time.strftime("%c", util.parse_date(item["date"])), util.parse_datetime(item["date"]).strftime("%c"),
"https://platformio.org/lib/show/%s/%s" "https://platformio.org/lib/show/%s/%s"
% (item["id"], quote(item["name"])), % (item["id"], quote(item["name"])),
) )

View File

@ -14,6 +14,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import functools
import json import json
import math import math
import os import os
@ -21,7 +22,7 @@ import platform
import re import re
import shutil import shutil
import time import time
from functools import wraps from datetime import datetime
from glob import glob from glob import glob
import click import click
@ -44,7 +45,7 @@ class memoized(object):
self.cache = {} self.cache = {}
def __call__(self, func): def __call__(self, func):
@wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
key = str(args) + str(kwargs) key = str(args) + str(kwargs)
if key not in self.cache or ( if key not in self.cache or (
@ -66,7 +67,7 @@ class throttle(object):
self.last = 0 self.last = 0
def __call__(self, func): def __call__(self, func):
@wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
diff = int(round((time.time() - self.last) * 1000)) diff = int(round((time.time() - self.last) * 1000))
if diff < self.threshhold: if diff < self.threshhold:
@ -252,10 +253,10 @@ def items_in_list(needle, haystack):
return set(needle) & set(haystack) return set(needle) & set(haystack)
def parse_date(datestr): def parse_datetime(datestr):
if "T" in datestr and "Z" in datestr: if "T" in datestr and "Z" in datestr:
return time.strptime(datestr, "%Y-%m-%dT%H:%M:%SZ") return datetime.strptime(datestr, "%Y-%m-%dT%H:%M:%SZ")
return time.strptime(datestr) return datetime.strptime(datestr)
def merge_dicts(d1, d2, path=None): def merge_dicts(d1, d2, path=None):