import datetime
import pytz
import calendar
import time
import numpy
from matplotlib import pylab
 
def get_epochs(timezone):
    """
    Create midnight timestamps for dates in given timezone.
    """
    dates = [20111003, 20111004, 20111005, 20111006, 20111007,
             20111010, 20111011, 20111012, 20111013, 20111014,
             20111017, 20111018, 20111019, 20111020, 20111021,
             20111024, 20111025, 20111026, 20111027, 20111028,
             20111031, 20111101, 20111102, 20111103, 20111104,
             20111107, 20111108, 20111109, 20111110, 20111111,
             20111114, 20111115, 20111116, 20111117, 20111118,
             20111121, 20111122, 20111123, 20111124, 20111125,
             20111128, 20111129, 20111130, 20111201]
    tz = pytz.timezone(timezone)
    epochs = []
    for date in dates:
        strdate = str(date)
        year = int(strdate[:4])
        month = int(strdate[4:6])
        day = int(strdate[6:8])
        dt = tz.localize(datetime.datetime(year, month, day))
        dt_utc = dt.astimezone(pytz.utc).replace(tzinfo=None)
        epoch = calendar.timegm(dt_utc.utctimetuple())
        epochs.append(epoch)
    return epochs

epochs_utc = get_epochs('UTC')
epochs_lon = get_epochs('Europe/London')
num_utc = pylab.epoch2num(epochs_utc)
num_lon = pylab.epoch2num(epochs_lon)
values = numpy.arange(len(epochs_utc))

# Print timestamps
for i in xrange(len(epochs_utc)):
    print "%d, %f (%s);  %d, %f (%s);  value=%.0f" % (
        epochs_utc[i], num_utc[i], time.ctime(epochs_utc[i]),
        epochs_lon[i], num_lon[i], time.ctime(epochs_lon[i]),
        values[i])
    


# =============================================
# Plot daily values
# =============================================

# Plot in UTC (does the right thing)
pylab.figure()
pylab.subplot(1,2,1)
pylab.plot_date(num_utc, values, fmt='o-', tz=pytz.utc)
pylab.grid(True)
pylab.title('UTC, correct')

# Plot in 'Europe/London' (does the wrong thing)
pylab.subplot(1,2,2)
pylab.plot_date(num_lon, values, fmt='o-', tz=pytz.timezone('Europe/London'))
pylab.grid(True)
pylab.title('Europe/London, wrong')

# Make nice date labels
pylab.gcf().autofmt_xdate()



# =============================================
# Plot intraday values,
# .... justifying above usage of tz
# =============================================

# On 20111003, UTC and Europe/London differ by one hour.
hrs = [12, 13, 15, 20]
intraday_epochs = [epochs_lon[0] + hr * 60 * 60 for hr in hrs]
intraday_num = pylab.epoch2num(intraday_epochs)
intraday_values = [1, 2, 1, 2]

# Plot in UTC (will display values shifted)
pylab.figure()
pylab.subplot(1,2,1)
pylab.plot_date(intraday_num, intraday_values, fmt='o-', tz=pytz.utc)
pylab.grid(True)
pylab.title('UTC, wrong times here as expected')

# Plot in 'Europe/London' (displays values as intended)
pylab.subplot(1,2,2)
pylab.plot_date(intraday_num, intraday_values, fmt='o-', tz=pytz.timezone('Europe/London'))
pylab.grid(True)
pylab.title('Europe/London, correct times here')

# Make nice date labels
pylab.gcf().autofmt_xdate()


pylab.show()

