Skip to content

Commit 43db1f7

Browse files
Update code-snippets.md
1 parent 4d93414 commit 43db1f7

File tree

1 file changed

+12
-34
lines changed

1 file changed

+12
-34
lines changed

docs/code-snippets.md

+12-34
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,6 @@ Below is how to a reply chain aka threaded tweets. This will make an initial twe
866866

867867
!> **Untested code** - it might be better to reply to the initial ID only.
868868

869-
870869
```python
871870
screen_name = api.me().screen_name
872871

@@ -879,7 +878,7 @@ target_id = None
879878

880879
for message in messages:
881880
if target_id is None:
882-
print("Initital tweet!")
881+
print("Initial tweet!")
883882
else:
884883
print(f"Replying to tweet ID: {target_id}")
885884
message = f"@{screen_name} {message}"
@@ -895,50 +894,29 @@ for message in messages:
895894
## Handle time values
896895
> Tips on dealing with time values from the Twitter API
897896

898-
### Date and time
899-
900-
The Twitter API often provides a datetime value in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format and Tweepy returns this to you as a string still.
901-
902-
e.g. `"2020-05-03T18:01:41+00:00"`.
903-
904-
This section covers how to parse a datetime string to a timezone-aware datetime object, to make it more useful for calculations and representations.
905-
906-
```python
907-
import datetime
908-
897+
See also my [Time handling](https://michaelcurrin.github.io/dev-cheatsheets/cheatsheets/python/time-handling.html) Python cheatsheet.
909898

910-
TIME_FORMAT_IN = r"%Y-%m-%dT%H:%M%z"
911-
912-
913-
def parse_datetime(value):
914-
"""
915-
Convert from Twitter datetime string to a datetime object.
916-
917-
>>> parse_datetime("2020-01-24T08:37:37+00:00")
918-
datetime.datetime(2020, 1, 24, 8, 37, tzinfo=datetime.timezone.utc)
919-
"""
920-
dt = ":".join(value.split(":", 2)[:2])
921-
tz = value[-6:]
922-
clean_value = f"{dt}{tz}"
899+
### Date and time
923900

924-
return datetime.datetime.strptime(clean_value, TIME_FORMAT_IN)
925-
```
901+
The Twitter API often provides a datetime value in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format and Tweepy returns this to you as a string.
926902

927-
?> When splitting, we don't need seconds and any decimals values. Plus, these have changed style before between API versions so are unreliable. So we just ignore after the 2nd colon (minutes) and pick up the timezone from the last 6 characters.
903+
e.g. `"2020-01-24T12:34:56+00:00"`.
928904

929905
?> The datetime value from Twitter will be always be UTC zone (GMT+00:00), regardless of your location or profile settings. Lookup the datetime docs for more info.
930906

931-
Example usage:
907+
Convert to a timezone-aware datetime object, to make it more useful for calculations and representations:
932908

933909
```python
934-
>>> dt = parse_datetime(tweet.created_at)
935-
>>> print(dt.year)
936-
2020
910+
dt = datetime.datetime.fromisoformat(tweet.created_at)
911+
# datetime.datetime(2020, 1, 24, 12, 34, 56, tzinfo=datetime.timezone.utc)
912+
913+
dt.year
914+
# 2020
937915
```
938916

939917
### Timestamp
940918

941-
If you get any numbers which are timestamps such as from the Rate Limit endpoint, you can convert them to datetime objects.
919+
If you get any numbers which are numeric timestamp, such as from the _Rate Limit_ endpoint, you can convert them to datetime objects.
942920

943921
```python
944922
import datetime

0 commit comments

Comments
 (0)