Ross Bulat
5 min readAug 9, 2018

Using Coinmarketcap Pro and CryptoCompare APIs around manipulating timestamps in Python

You’re building your backend web services with Python and things are going swimmingly. Apart from one thing — every service seems to be using a different timestamp format. Whilst we wish that all services would stick to one format, this is just not the case.

Luckily Python provide the tools to convert timestamps of various formats into a datetime object, where we can then manipulate them as we wish. This article gives you a taster of how to do this.

Checking your system timezone

Before we manipulate time in Python, make sure you know which timezone your system is running:

import timeprint(time.tzname)
print(time.time())
> [UTC, UTC]

time.time() gives us 2 values, the timezone not accounting for daylight savings, and the timezone accounting for daylight savings. What if we don’t want to worry about DST issues? Stick to UTC time.

The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time. — nhc.noaa.gov

This is why UTC stands for Universal Time Coordinated, it makes our lives as programmers easier to use a universally accepted format.

Use the following command if you need to switch your timezone to UTC:

sudo timedatectl set-timezone UTC

Converting a timestamp from a Coinmarketcap Pro API call

Let’s move onto a real world example of fetching an API response where a timestamp is a necessity to store or manipulate. In this case let’s use the new Coinmarketcap Pro API to fetch the latest Bitcoin prices.

Note: Both services used in this article adhere to UTC time by default.

To make the call let’s use Python’s requests module, and format the result as a JSON Object:
(Install requests with pip install requests)

import time, requests, json
import datetime as dt
Ross Bulat

Programmer and Author. @ Parity Technologies, JKRB Investments