Parse Response Data

In the response data of Open Weather Map API, the time is displayed in UNIX timestamp, like this: "dt":1485913525,"

Also, the wind degree need to be converted to readable compass direction, for example: "wind":{"deg":2.50012} to North

  • What's UNIX time stamp

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. It should be point out that this point in time technically does not change no matter where you are located on the globle. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.

  • What happens on January 19, 2038?

On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time.

  • Convert UNIX timestamp to readable format string

    1. Import datetime.
    2. Use datetime.fromtimestamp(timestamp, tz=None) to convert timestamp to datetime object. If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

      >>> from datetime import datetime
      
      >>> dt = datetime.fromtimestamp(1485913525)
      >>> print(dt)
      2017-02-01 14:45:25
      
    3. Python’s datetime.datetime objects have a tzinfo attribute that can be used to store time zone information, represented as an instance of a subclass of datetime.tzinfo. When this attribute is set and describes an offset, a datetime object is aware. Otherwise, it’s naive.

      In this case, I need an aware object, so the user could know the timezone info.

    4. from dateutil import tz, use tz.tzlocal() to get local timezone

      >>> from datetime import datetime
      >>> from dateutil import tz
      
      >>> dt = datetime.fromtimestamp(1485913525, tz.tzlocal())
      >>> print(dt)
      2017-02-01 14:45:25+13:00
      
    5. Use datetime.strftime(format) to convert the datetime object to a readable string in a particular format.

      from datetime import datetime
      from dateutil import tz
      
      dt = datetime.fromtimestamp(1485913525, tz.tzlocal())
      print(dt, type(dt))
      
      fmt = '%Y-%m-%d %H:%M:%S %z'
      dt_string = datetime.strftime(fmt)
      print(dt_string, type(dt_string))
      

      Output is:

      2017-02-01 14:45:25+13:00 <class 'datetime.datetime'>
      2017-02-01 14:45:25 +1300 <class 'str'>
      
  • Convert wind degree to readable compass direction

def deg_to_direction(deg):
'''convert wind degree to compass direction'''
direction = ['N', 'NNE', 'NE', 'ENE',
             'E', 'ESE', 'SE', 'SSE',
             'S', 'SSW', 'SW', 'WSW',
             'W', 'WNW', 'NW', 'NNW']
value = int((deg / 22.5) + 0.5)
return direction[value % 16]

results matching ""

    No results matching ""