Skip to content

Commit 48ae422

Browse files
committed
Improved docs
1 parent 007fd20 commit 48ae422

File tree

8 files changed

+243
-95
lines changed

8 files changed

+243
-95
lines changed

Justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ update:
2626
{{PIP}} install -U \
2727
build \
2828
pytest \
29+
docutils \
2930
pytest-sugar \
3031
pytest-clarity \
3132
freezegun \
@@ -132,3 +133,8 @@ strftime:
132133
f"Format Specifiers:\n{pt.get_string()}\n\n"
133134
"Notes:\n* - Locale-dependent\n+ - C99 extension\n! - when extension"
134135
)
136+
137+
# Preview README.rst
138+
docs:
139+
rst2html5 --output README.html README.rst
140+
open README.html

README.rst

Lines changed: 157 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
=========
12
when 🌐🕐
23
=========
34

@@ -17,64 +18,106 @@ Scenario
1718
--------
1819

1920
Your favorite sporting event, concert, performance, conference, or symposium is happening
20-
in Ulan Bator and all you know is the time of event is relative to the location city or time zone.
21-
But wait! You need to know when that occurs relative to you while traveling to Seoul or Paris.
21+
in Ulan Bator and all you know is the time of event relative to the city or time zone.
22+
So what time is that for you in your local time? What time did it or will it occur at some
23+
other time? What about for your friends in other locations around the world?
24+
25+
Features
26+
--------
27+
28+
``when`` can refer to source and target locations via the ``--source`` and ``--target`` specifiers.
29+
30+
* ``when`` can download a GeoNames_ cities database for referencing locations by city name
31+
* All IANA time zone definitions are available, as well as the most common time zone aliases (i.e.: ``EST`` => ``US/Eastern``)
32+
* Display current lunar state
33+
* List common holidays for a given country and/or year (US or configurable)
34+
* Show dates for full moons
35+
* Extensive configuration options for results
36+
* JSON output
2237

2338
Installation
24-
------------
39+
============
2540

26-
Install from PyPI::
41+
Install from PyPI:
42+
43+
.. code:: bash
2744
2845
$ pip install when
2946
30-
or using pipx_::
47+
or using pipx_:
48+
49+
.. code:: bash
3150
3251
$ pipx install when
3352
34-
or::
53+
or:
54+
55+
.. code:: bash
3556
3657
$ pipx install git+https://github.com/dakrauth/when.git
3758
38-
.. _pipx: https://pypa.github.io/pipx/
59+
.. note::
60+
61+
Once installed, if you wish to utilize ``when``'s full capabilities, you should
62+
install the GeoNames cities database as describe next.
63+
64+
65+
66+
Database installation
67+
---------------------
3968

40-
To access city names, you must install the cities database::
69+
To access city names, you need to install the cities database after installing the ``when`` application:
70+
71+
.. code:: bash
4172
4273
when --db [options]
4374
44-
For ``options``:
75+
Where ``options`` are:
76+
77+
* ``--db-size``: You can specify a database down size by using one of the following:
78+
- ``sm`` - cities with population > 15,000 + country capitals (~2.9M download, ~2M DB)
79+
- ``md`` - **default**, same as ``sm``, plus:
80+
- cities with population > 5,000 (~4.8M download, ~3.1M DB)
81+
- seat of first-order admin division, i.e. US state
82+
- ``lg`` - same as ``md``, plus:
83+
- cities with population > 1,000 (~9.5M download, ~5.8M DB)
84+
- seat of admin division down to third level (counties)
85+
- ``xl`` - same as ``lg``, plus:
86+
- cities with population > 500 (~12.1M download, ~7.2M DB)
87+
- seat of admin division down to fourth order
88+
* ``--db-pop``: Filter non-admin division seats providing a minimum city population size
89+
* ``--db-force``: Force an existing database to be overwritten
4590

46-
You can specify minimum city size by adding ``--size SIZE``, where *SIZE* can be one of:
91+
Database Usage
92+
==============
4793

48-
- ``15000`` - cities with population > 15000 or country capitals
49-
- ``5000`` - cities with population > 5000 or seat of first-order admin division, i.e. US state
50-
- ``1000`` - cities with population > 1000 or seat of third order admin division
51-
- ``500`` - cities with population > 500 or seat of fourth-order admin division
94+
* Search: ``--db-search``
5295

53-
Additionally, you can filter non-admin division seats using ``--pop POP``.
96+
Once installed, you can search the database:
5497

55-
The appropriate GeoNames Gazetteer is downloaded and a Sqlite database generated.
98+
.. code:: bash
5699
57-
Usage
58-
-----
100+
$ when --db-search New York
101+
5106292, West New York, West New York, US, New Jersey, America/New_York
102+
5128581, New York City, New York City, US, New York, America/New_York
59103
60-
Once installed, you can search the database::
104+
* Aliases: ``--db-alias``
61105

62-
$ when --db-search New York
63-
5106292, West New York, West New York, US, New Jersey, America/New_York
64-
5128581, New York City, New York City, US, New York, America/New_York
106+
You can add aliases for easier search. In the example directly above, we see that New York City has
107+
a GeoNames ID of 5128581. Pass that to the ``--db-alias`` option along with another name that
108+
you would like to use:
65109

110+
.. code:: bash
66111
67-
Additionally, you can add aliases. In the example directly above, we see that New York City has
68-
a GeoNames ID of 5128581. Pass that to the ``--db-alias`` option along with another name that
69-
you would like to use::
112+
$ when --db-alias 5128581 NYC
113+
$ when --source NYC
114+
2023-07-06 07:58:33-0400 (EDT, America/New_York) 187d27w (New York City, New York, US)[🌕 Full Moon]
70115
71-
$ when --db-alias 5128581 NYC
72-
$ when --source NYC
73-
2023-07-06 07:58:33-0400 (EDT, America/New_York) 187d27w (New York City, New York, US)[🌕 Full Moon]
116+
* Alias listing: ``--db-aliases``
74117

75118

76-
Example
77-
-------
119+
Examples
120+
========
78121

79122
For the sake of clarity, in the following examples I am in Seoul, Korea.
80123

@@ -101,24 +144,100 @@ For the sake of clarity, in the following examples I am in Seoul, Korea.
101144
1945-03-07 22:00:00-0400 (EWT, America/New_York) 066d10w [🌘 Waning Crescent]
102145
103146
104-
Develop
105-
-------
147+
Development
148+
===========
106149

107-
Requirements Python 3.10+. Also, [just](https://github.com/casey/just) for convenience.
150+
Requires Python 3.10+ and just_ for convenience.
108151

109152
.. code:: bash
110153
111154
$ git clone git@github.com:dakrauth/when.git
112155
$ cd when
113-
$ just
114-
$ just venv
156+
$ just # or just help
157+
158+
Set up dev env:
159+
160+
.. code:: bash
161+
162+
$ just init
163+
164+
Test, and code coverage:
165+
166+
.. code:: bash
167+
115168
$ just test
169+
$ just cov
170+
171+
Only run a test matching matching a given substring:
172+
173+
.. code:: bash
174+
175+
$ just test -k test_sometest
176+
177+
Interactive development:
178+
179+
.. code:: bash
180+
116181
$ . ./venv/bin/activate
117182
$ when --help
118183
$ when --db
119184
120185
Further Reading
121-
---------------
186+
===============
187+
188+
`Time Zones Aren’t Offsets – Offsets Aren’t Time Zones`_
189+
190+
.. _pipx: https://pypa.github.io/pipx/
191+
.. _just: https://github.com/casey/just
192+
.. _`Time Zones Aren’t Offsets – Offsets Aren’t Time Zones`: https://spin.atomicobject.com/time-zones-offsets/)
193+
.. _GeoNames: https://www.geonames.org/export/
194+
195+
Complete Usage
196+
==============
197+
198+
.. code:: bash
199+
200+
usage: when [--delta {long,short}] [--offset [+-]?(\d+wdhm)+] [-h] [--prefix] [-s SOURCE] [-t TARGET] [-f FORMAT] [-g] [--all]
201+
[--holidays COUNTRY_CODE] [-v] [-V] [--json] [--config] [--db] [--db-force] [--db-search] [--exact] [--db-alias DB_ALIAS]
202+
[--db-aliases] [--db-size DB_SIZE] [--db-pop DB_POP] [--tz-alias] [--fullmoon]
203+
[timestr ...]
204+
205+
Convert times to and from time zones or cities
206+
207+
positional arguments:
208+
timestr Timestamp to parse, defaults to local time
209+
210+
options:
211+
--delta {long,short} Show the delta to the given timestamp
212+
--offset [+-]?(\d+wdhm)+
213+
Show the difference from a given offset
214+
-h, --help Show helpful usage information
215+
--prefix Show when's directory
216+
-s SOURCE, --source SOURCE
217+
Timezone / city to convert the timestr from, defaulting to local time
218+
-t TARGET, --target TARGET
219+
Timezone / city to convert the timestr to (globbing patterns allowed, can be comma delimited), defaulting to local
220+
time
221+
-f FORMAT, --format FORMAT
222+
Output formatting. Additional formats can be shown using the -v option with -h
223+
-g, --group Group sources together under same target results
224+
--all Show times in all common timezones
225+
--holidays COUNTRY_CODE
226+
Show holidays for given country code.
227+
-v, --verbosity Verbosity (-v, -vv, etc). Use -v to show `when` extension detailed help
228+
-V, --version show program's version number and exit
229+
--json Output results in nicely formatted JSON
230+
--config Toggle config mode. With no other option, dump current configuration settings
231+
--db Create cities database, used with --db-size and --db-pop
232+
--db-force Force an existing database to be overwritten
233+
--db-search Search database for the given city
234+
--exact DB searches must be exact
235+
--db-alias DB_ALIAS Create a new alias from the city id
236+
--db-aliases Show all DB aliases
237+
--db-size DB_SIZE Geonames file size. Can be one of 'xl' ('xlarge'), 'lg' ('large'), 'md' ('medium'), 'sm' ('small').
238+
--db-pop DB_POP City population minimum.
239+
--tz-alias Search for a time zone alias
240+
--fullmoon Show full moon(s) for given year or month. Can be in the format of: 'next' | 'prev' | YYYY[-MM]
241+
242+
Use -v option for details
122243
123-
[Time Zones Aren’t Offsets – Offsets Aren’t Time Zones
124-
](https://spin.atomicobject.com/time-zones-offsets/)

src/when/cli.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#!/usr/bin/env python
22
import argparse
3-
import logging
43
import sys
4+
import logging
55
from pathlib import Path
66

77
from . import __version__, core, db, utils, lunar, exceptions
88
from . import timezones
99
from .config import Settings, __doc__ as FORMAT_HELP
1010

11-
logger = logging.getLogger(__name__)
12-
1311

1412
def get_parser(settings):
1513
class DBSizeAction(argparse.Action):
@@ -227,11 +225,14 @@ def log_config(verbosity, settings):
227225
logging.getLogger("asyncio").setLevel(logging.WARNING)
228226

229227
logging.basicConfig(level=log_level, format=log_format, force=True)
228+
logger = utils.logger()
230229
logger.debug(
231230
"Configuration files read: %s",
232231
", ".join(str(s) for s in settings.read_from) if settings.read_from else "None",
233232
)
234233

234+
return logger
235+
235236

236237
def main(sys_args, when=None, settings=None):
237238
if "--pdb" in sys_args: # pragma: no cover
@@ -258,7 +259,7 @@ def main(sys_args, when=None, settings=None):
258259
parser = get_parser(settings)
259260
args = parser.parse_args(sys_args)
260261

261-
log_config(args.verbosity, settings)
262+
logger = log_config(args.verbosity, settings)
262263
logger.debug(args)
263264
if args.help:
264265
parser.print_help()

src/when/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .db import client
1414
from .lunar import lunar_phase
1515

16-
logger = logging.getLogger(__name__)
16+
logger = utils.logger()
1717

1818

1919
def holidays(settings, co="US", ts=None):

0 commit comments

Comments
 (0)