Hashtag

Definitions

Hash (noun): The # symbol on a keyboard or telephone.
Tag (noun): a label or identifying mark.
Hashtag (noun): a word or phrase starting with #, used to group relevant topics on the web.
Ex: #imwearingsocks

A brief history of hashtags

  • Originated in Internet chatrooms as a way to label conversation topics. #nerds
  • Introduced to microblogging site Twitter in 2007. #billionairenerds
  • Now used everywhere by everyone, including your mother. #weareallnerds

Django: can’t compare offset-naive and offset-aware datetimes error

I encountered an horrible problem today, and I wants to share the solution with you. So I decided to write this post. The problem was: can’t compare offset-naive and offset-aware datetimes. The origin of this error is a compare between an offset-aware date time, obtained from the database in my case, and an offset-naive date time, this is the default type of python datetime object. Below the line that caused this error:

from datetime import timedelta, datetime

one_hour_ago = datetime.now() - timedelta(hours=1)
if topic.created > one_hour_ago:
    # Do something

So the solution is to make both of compared datetime to either naive or aware datetime object. Fortunately, Django comes with a helper to solve this problem:

>>> from django.utils import timezone
>>> now = timezone.now()
datetime.datetime(2013, 12, 11, 9, 43, 44, 868854, tzinfo=<UTC>)

Now, “now” is an offset-aware datetime object. For non djangonauts developer, you can use pytz python module like that:

>>> from datetime import datetime
>>> import pytz
>>> utc = pytz.UTC
>>> now = utc.localize(datetime.now())
datetime.datetime(2013, 12, 11, 9, 43, 44, 868854, tzinfo=<UTC>)