Other Useful Methods and Info¶
Time Zones¶
Source
For a list of timezone strings, see List of tz database time zones
Acceptable Time Zone names are found under the 'TZ database name' column.
time_methods
¶
Useful time conversion methods.
convert_date_to_tick_tick_format(datetime_obj, tz)
¶
Parses ISO 8601 Format to Tick Tick Date Format
It first converts the datetime object to UTC time based off the passed time zone, and then returns a string with the TickTick required date format.
Info
ISO 8601 Format Example: 2020-12-23T01:56:07+00:00
TickTick Required Format: 2020-12-23T01:56:07+0000 -> Where the last colon is removed for timezone
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datetime_obj |
datetime |
Datetime object to be parsed. |
required |
tz |
str |
Time zone string. |
required |
Returns:
Type | Description |
---|---|
str |
The TickTick accepted date string. |
Import Help
from ticktick.helpers.time_methods import convert_iso_to_tick_tick_format
Example
date = datetime(2022, 12, 31, 14, 30, 45)
converted_date = convert_iso_to_tick_tick_format(date, 'US/Pacific')
Result
The proper format for a date string to be used with TickTick dates.
'2022-12-31T22:30:45+0000'
Source code in helpers/time_methods.py
def convert_date_to_tick_tick_format(datetime_obj, tz: str):
"""
Parses ISO 8601 Format to Tick Tick Date Format
It first converts the datetime object to UTC time based off the passed time zone, and then
returns a string with the TickTick required date format.
!!! info Required Format
ISO 8601 Format Example: 2020-12-23T01:56:07+00:00
TickTick Required Format: 2020-12-23T01:56:07+0000 -> Where the last colon is removed for timezone
Arguments:
datetime_obj (datetime): Datetime object to be parsed.
tz: Time zone string.
Returns:
str: The TickTick accepted date string.
??? info "Import Help"
```python
from ticktick.helpers.time_methods import convert_iso_to_tick_tick_format
```
??? example
```python
date = datetime(2022, 12, 31, 14, 30, 45)
converted_date = convert_iso_to_tick_tick_format(date, 'US/Pacific')
```
??? success "Result"
The proper format for a date string to be used with TickTick dates.
```python
'2022-12-31T22:30:45+0000'
```
"""
date = convert_local_time_to_utc(datetime_obj, tz)
date = date.replace(tzinfo=datetime.timezone.utc).isoformat()
date = date[::-1].replace(":", "", 1)[::-1]
return date
convert_local_time_to_utc(original_time, time_zone)
¶
Converts the datetime object to UTC time. Utilizes the time_zone string for proper conversion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_time |
datetime |
Datetime object |
required |
time_zone |
str |
Time zone of |
required |
Returns:
Type | Description |
---|---|
datetime |
Datetime object with the converted UTC time - with no timezone information attached. |
Import Help
from ticktick.helpers.time_methods import convert_local_time_to_utc
Example
pst = datetime(2020, 12, 11, 23, 59)
converted = convert_local_time_to_utc(pst, 'US/Pacific')
Result
A datetime object that is the UTC equivalent of the original date.
datetime(2020, 12, 12, 7, 59)
Source code in helpers/time_methods.py
def convert_local_time_to_utc(original_time, time_zone: str):
"""
Converts the datetime object to UTC time. Utilizes the time_zone string for proper conversion.
Arguments:
original_time (datetime): Datetime object
time_zone: Time zone of `original_time`
Returns:
datetime: Datetime object with the converted UTC time - with no timezone information attached.
??? info "Import Help"
```python
from ticktick.helpers.time_methods import convert_local_time_to_utc
```
??? Example
```python
pst = datetime(2020, 12, 11, 23, 59)
converted = convert_local_time_to_utc(pst, 'US/Pacific')
```
??? success "Result"
A datetime object that is the UTC equivalent of the original date.
```python
datetime(2020, 12, 12, 7, 59)
```
"""
utc = pytz.utc
time_zone = pytz.timezone(time_zone)
original_time = original_time.strftime(DATE_FORMAT)
time_object = datetime.datetime.strptime(original_time, DATE_FORMAT)
time_zone_dt = time_zone.localize(time_object)
return time_zone_dt.astimezone(utc).replace(tzinfo=None)
hex_color
¶
Provides some methods for dealing with hex color code strings.
check_hex_color(color)
¶
Verifies if the passed in color string is a valid hexadecimal color string
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color |
str |
String to check. |
required |
Returns:
Type | Description |
---|---|
bool |
True if the string is a valid hex code, else False. |
Import Help
from ticktick.helpers.hex_color import check_hex_color
Source code in helpers/hex_color.py
def check_hex_color(color: str) -> bool:
"""
Verifies if the passed in color string is a valid hexadecimal color string
Arguments:
color: String to check.
Returns:
True if the string is a valid hex code, else False.
??? info "Import Help"
```python
from ticktick.helpers.hex_color import check_hex_color
```
"""
check_color = re.search(VALID_HEX_VALUES, color)
if not check_color:
return False
else:
return True
generate_hex_color()
¶
Generates a random hexadecimal color string to be used for rgb color schemes.
Returns:
Type | Description |
---|---|
str |
'#' followed by 6 hexadecimal digits. |
Import Help
from ticktick.helpers.hex_color import generate_hex_color
Source code in helpers/hex_color.py
def generate_hex_color() -> str:
"""
Generates a random hexadecimal color string to be used for rgb color schemes.
Returns:
'#' followed by 6 hexadecimal digits.
??? info "Import Help"
```python
from ticktick.helpers.hex_color import generate_hex_color
```
"""
num = random.randint(1118481, 16777215)
hex_num = format(num, 'x')
return '#' + hex_num