Skip to content

Commit

Permalink
group common datetime functions
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Apr 12, 2024
1 parent 22fd413 commit 09dbbad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/apps/tiddlywiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
import json

import common
import intermediate_format as imf


Expand All @@ -13,7 +14,7 @@

def tiddlywiki_to_unix(tiddlywiki_time: str) -> int:
"""Format: https://tiddlywiki.com/static/DateFormat.html"""
return int(datetime.strptime(tiddlywiki_time, "%Y%m%d%H%M%S%f").timestamp() * 1000)
return common.datetime_to_ms(datetime.strptime(tiddlywiki_time, "%Y%m%d%H%M%S%f"))


def split_tags(tag_string: str) -> list[str]:
Expand Down
3 changes: 2 additions & 1 deletion src/apps/todoist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
from pathlib import Path

import common
import intermediate_format as imf


Expand Down Expand Up @@ -118,7 +119,7 @@ def convert(file_: Path, parent: imf.Notebook):
"source_application": Path(__file__).stem,
}
if (due_date := parse_date(row["DATE"])) is not None:
note_data["todo_due"] = int(due_date.timestamp() * 1000)
note_data["todo_due"] = common.datetime_to_ms(due_date)

tags_string = labels + [f"todoist-priority-{row['PRIORITY']}"]
joplin_note = imf.Note(
Expand Down
20 changes: 12 additions & 8 deletions src/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Common functions."""

from datetime import datetime
import datetime as dt
import re


Expand All @@ -16,16 +16,20 @@ def get_wikilink_links(text: str):
return WIKILINK_LINK_REGEX.findall(text)


def current_unix_ms():
return int(datetime.now().timestamp() * 1000)
def datetime_to_ms(datetime_: dt.datetime) -> int:
return int(datetime_.timestamp() * 1000)


def date_to_unix_ms(date_):
def current_unix_ms() -> int:
return datetime_to_ms(dt.datetime.now())


def date_to_unix_ms(date_: dt.date) -> int:
# https://stackoverflow.com/a/61886944/7410886
return int(
datetime(year=date_.year, month=date_.month, day=date_.day).timestamp() * 1000
return datetime_to_ms(
dt.datetime(year=date_.year, month=date_.month, day=date_.day)
)


def iso_to_unix_ms(iso_time):
return int(datetime.fromisoformat(iso_time).timestamp() * 1000)
def iso_to_unix_ms(iso_time: str) -> int:
return datetime_to_ms(dt.datetime.fromisoformat(iso_time))

0 comments on commit 09dbbad

Please sign in to comment.