Skip to content

Commit 30d7015

Browse files
Exireldgw
andcommitted
docs: best practices to get timezone and time format
Co-authored-by: dgw <[email protected]>
1 parent a3e4495 commit 30d7015

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

docs/source/plugin/time.rst

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ About time
44

55
Your plugin may want to display dates and times in messages. For that, you can
66
always count on the :mod:`datetime` built-in module. However, what if you would
7-
like to respect the date-format for a given user or a given channel? Various
8-
functions of :mod:`Sopel<sopel.tools.time>` can help you with that:
7+
like to respect the date-format for a given user or a given channel? Functions
8+
of :mod:`sopel.tools.time` can help you with that:
99

1010
* :func:`~sopel.tools.time.get_timezone` will fetch the right timezone for you
1111
* :func:`~sopel.tools.time.format_time` will format your aware datetime for you
1212

13-
Here is a full example of that::
13+
Here is a full example of that, adapted from the built-in ``.t`` command::
1414

1515
import datetime
1616

@@ -146,3 +146,66 @@ Then you have arrived at the last step of your journey, thanks to the
146146

147147
And voilà! You now have a string formatted aware datetime that uses the format
148148
defined for a user/channel/the bot, and can now rest and enjoy your own time.
149+
150+
Best practices
151+
==============
152+
153+
So far, you have learned how to get a time formatted with the preferred timezone
154+
and format of a user: this is perfect for a command like ``.time`` that
155+
displays the time for a specific user in mind. However, other commands, like
156+
URL previews, are not related to users, and they should use a different
157+
strategy to figure out the right timezone and format.
158+
159+
User specific time
160+
------------------
161+
162+
A user specific time is when the time is displayed for a specific user: a
163+
direct message, a reminder, the user's time, etc.
164+
165+
In that case, the recommended order to select the appropriate timezone and time
166+
format is:
167+
168+
* user's preferred ones
169+
* channel's preferred ones
170+
* bot's timezone and format (from configuration)
171+
* default timezone and format
172+
173+
This can be done with :func:`~sopel.tools.time.get_timezone` and
174+
:func:`~sopel.tools.time.format_time`::
175+
176+
>>> custom_tz = get_timezone(
177+
... bot.db, bot.settings, zone=None,
178+
... nick=nick_name, channel=channel_name,
179+
... )
180+
>>> display_time = format_time(
181+
... bot.db, bot.settings, zone=custom_tz,
182+
... nick=nick_name, channel=channel_name, time=user_time,
183+
... )
184+
185+
Other times
186+
-----------
187+
188+
When displaying a time that is not for a specific user, it doesn't make sense
189+
to display time with the user's preferred format. For example, a URL preview
190+
plugin is not displaying a user specific time.
191+
192+
In that case, the recommended order to select the appropriate timezone and time
193+
format is:
194+
195+
* channel's preferred ones
196+
* bot's timezone and format (from configuration)
197+
* default timezone and format
198+
199+
This can be done with :func:`~sopel.tools.time.get_timezone` and
200+
:func:`~sopel.tools.time.format_time`::
201+
202+
>>> custom_tz = get_timezone(
203+
... bot.db, bot.settings, zone=None,
204+
... channel=channel_name,
205+
... )
206+
>>> display_time = plugin_defined_format or format_time(
207+
... bot.db, bot.settings, zone=custom_tz,
208+
... channel=channel_name, time=plugin_time,
209+
... )
210+
211+
Note the absence of the ``nick`` parameter in that snippet.

0 commit comments

Comments
 (0)