mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Convert from local util.get_local_timezone()
to tzlocal.get_localzone()
one side effect is that a `pytz` object is returned rather than a string, and so conversion to a string must be done explicitly at run time where needed.
This commit is contained in:
parent
b77a195f23
commit
623fc076ad
3 changed files with 5 additions and 21 deletions
|
@ -11,6 +11,7 @@ try:
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
import tzlocal
|
||||||
|
|
||||||
def _parse_args(command):
|
def _parse_args(command):
|
||||||
nargs=[]
|
nargs=[]
|
||||||
|
@ -120,7 +121,7 @@ def check_output(context):
|
||||||
@then('the output should contain "{text}" in the local time')
|
@then('the output should contain "{text}" in the local time')
|
||||||
def check_output_time_inline(context, text):
|
def check_output_time_inline(context, text):
|
||||||
out = context.stdout_capture.getvalue()
|
out = context.stdout_capture.getvalue()
|
||||||
local_tz = pytz.timezone(util.get_local_timezone())
|
local_tz = tzlocal.get_localzone()
|
||||||
utc_time = date_parser.parse(text)
|
utc_time = date_parser.parse(text)
|
||||||
date = utc_time + local_tz._utcoffset
|
date = utc_time + local_tz._utcoffset
|
||||||
local_date = date.strftime("%Y-%m-%d %H:%M")
|
local_date = date.strftime("%Y-%m-%d %H:%M")
|
||||||
|
|
|
@ -23,6 +23,7 @@ import hashlib
|
||||||
import plistlib
|
import plistlib
|
||||||
import pytz
|
import pytz
|
||||||
import uuid
|
import uuid
|
||||||
|
import tzlocal
|
||||||
|
|
||||||
class Journal(object):
|
class Journal(object):
|
||||||
def __init__(self, name='default', **kwargs):
|
def __init__(self, name='default', **kwargs):
|
||||||
|
@ -333,7 +334,7 @@ class DayOne(Journal):
|
||||||
try:
|
try:
|
||||||
timezone = pytz.timezone(dict_entry['Time Zone'])
|
timezone = pytz.timezone(dict_entry['Time Zone'])
|
||||||
except (KeyError, pytz.exceptions.UnknownTimeZoneError):
|
except (KeyError, pytz.exceptions.UnknownTimeZoneError):
|
||||||
timezone = pytz.timezone(util.get_local_timezone())
|
timezone = tzlocal.get_localzone()
|
||||||
date = dict_entry['Creation Date']
|
date = dict_entry['Creation Date']
|
||||||
date = date + timezone.utcoffset(date)
|
date = date + timezone.utcoffset(date)
|
||||||
raw = dict_entry['Entry Text']
|
raw = dict_entry['Entry Text']
|
||||||
|
@ -357,7 +358,7 @@ class DayOne(Journal):
|
||||||
'Creation Date': utc_time,
|
'Creation Date': utc_time,
|
||||||
'Starred': entry.starred if hasattr(entry, 'starred') else False,
|
'Starred': entry.starred if hasattr(entry, 'starred') else False,
|
||||||
'Entry Text': entry.title+"\n"+entry.body,
|
'Entry Text': entry.title+"\n"+entry.body,
|
||||||
'Time Zone': util.get_local_timezone(),
|
'Time Zone': str(tzlocal.get_localzone()),
|
||||||
'UUID': entry.uuid,
|
'UUID': entry.uuid,
|
||||||
'Tags': [tag.strip(self.config['tagsymbols']) for tag in entry.tags]
|
'Tags': [tag.strip(self.config['tagsymbols']) for tag in entry.tags]
|
||||||
}
|
}
|
||||||
|
|
18
jrnl/util.py
18
jrnl/util.py
|
@ -88,24 +88,6 @@ def yesno(prompt, default=True):
|
||||||
raw = py23_input(prompt)
|
raw = py23_input(prompt)
|
||||||
return {'y': True, 'n': False}.get(raw.lower(), default)
|
return {'y': True, 'n': False}.get(raw.lower(), default)
|
||||||
|
|
||||||
def get_local_timezone():
|
|
||||||
"""Returns the Olson identifier of the local timezone.
|
|
||||||
In a happy world, tzlocal.get_localzone would do this, but there's a bug on OS X
|
|
||||||
that prevents that right now: https://github.com/regebro/tzlocal/issues/6"""
|
|
||||||
global __cached_tz
|
|
||||||
if not __cached_tz and "darwin" in sys.platform:
|
|
||||||
__cached_tz = os.popen("systemsetup -gettimezone").read().replace("Time Zone: ", "").strip()
|
|
||||||
if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
|
|
||||||
link = os.readlink("/etc/localtime")
|
|
||||||
# This is something like /usr/share/zoneinfo/America/Los_Angeles.
|
|
||||||
# Find second / from right and take the substring
|
|
||||||
__cached_tz = link[link.rfind('/', 0, link.rfind('/'))+1:]
|
|
||||||
elif not __cached_tz:
|
|
||||||
__cached_tz = str(get_localzone())
|
|
||||||
if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
|
|
||||||
__cached_tz = "UTC"
|
|
||||||
return __cached_tz
|
|
||||||
|
|
||||||
def load_and_fix_json(json_path):
|
def load_and_fix_json(json_path):
|
||||||
"""Tries to load a json object from a file.
|
"""Tries to load a json object from a file.
|
||||||
If that fails, tries to fix common errors (no or extra , at end of the line).
|
If that fails, tries to fix common errors (no or extra , at end of the line).
|
||||||
|
|
Loading…
Add table
Reference in a new issue