from __future__ import print_function, division
from fontTools.misc.py23 import *
from fontTools.misc import sstruct
from fontTools.misc.textTools import safeEval, num2binary, binary2num
from . import DefaultTable
import time


headFormat = """
		>	# big endian
		tableVersion:       16.16F
		fontRevision:       16.16F
		checkSumAdjustment: I
		magicNumber:        I
		flags:              H
		unitsPerEm:         H
		created:            Q
		modified:           Q
		xMin:               h
		yMin:               h
		xMax:               h
		yMax:               h
		macStyle:           H
		lowestRecPPEM:      H
		fontDirectionHint:  h
		indexToLocFormat:   h
		glyphDataFormat:    h
"""

class table__h_e_a_d(DefaultTable.DefaultTable):
	
	dependencies = ['maxp', 'loca']
	
	def decompile(self, data, ttFont):
		dummy, rest = sstruct.unpack2(headFormat, data, self)
		if rest:
			# this is quite illegal, but there seem to be fonts out there that do this
			assert rest == "\0\0"
	
	def compile(self, ttFont):
		self.modified = int(time.time() - mac_epoch_diff)
		data = sstruct.pack(headFormat, self)
		return data
	
	def toXML(self, writer, ttFont):
		writer.comment("Most of this table will be recalculated by the compiler")
		writer.newline()
		formatstring, names, fixes = sstruct.getformat(headFormat)
		for name in names:
			value = getattr(self, name)
			if name in ("created", "modified"):
				try:
					value = time.asctime(time.gmtime(max(0, value + mac_epoch_diff)))
				except ValueError:
					value = time.asctime(time.gmtime(0))
			if name in ("magicNumber", "checkSumAdjustment"):
				if value < 0:
					value = value + 0x100000000
				value = hex(value)
				if value[-1:] == "L":
					value = value[:-1]
			elif name in ("macStyle", "flags"):
				value = num2binary(value, 16)
			writer.simpletag(name, value=value)
			writer.newline()
	
	def fromXML(self, name, attrs, content, ttFont):
		value = attrs["value"]
		if name in ("created", "modified"):
			value = parse_date(value) - mac_epoch_diff
		elif name in ("macStyle", "flags"):
			value = binary2num(value)
		else:
			value = safeEval(value)
		setattr(self, name, value)


def calc_mac_epoch_diff():
	"""calculate the difference between the original Mac epoch (1904)
	to the epoch on this machine.
	"""
	safe_epoch_t = (1972, 1, 1, 0, 0, 0, 0, 0, 0)
	safe_epoch = time.mktime(safe_epoch_t) - time.timezone
	# This assert fails in certain time zones, with certain daylight settings
	#assert time.gmtime(safe_epoch)[:6] == safe_epoch_t[:6]
	seconds1904to1972 = 60 * 60 * 24 * (365 * (1972-1904) + 17) # thanks, Laurence!
	return int(safe_epoch - seconds1904to1972)

mac_epoch_diff = calc_mac_epoch_diff()


_months = ['   ', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
		'sep', 'oct', 'nov', 'dec']
_weekdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']

def parse_date(datestring):
	datestring = datestring.lower()
	weekday, month, day, tim, year = datestring.split()
	weekday = _weekdays.index(weekday)
	month = _months.index(month)
	year = int(year)
	day = int(day)
	hour, minute, second = [int(item) for item in tim.split(":")]
	t = (year, month, day, hour, minute, second, weekday, 0, 0)
	return int(time.mktime(t) - time.timezone)

@@ -219,16 +219,32 @@
 rt time%0A
+import calendar%0A
 %0A%0AheadFo
@@ -1989,25 +1989,44 @@
 e = 
-parse_dat
+calendar.timegm(time.strptim
 e(value)
  - m
@@ -2021,16 +2021,17 @@
 e(value)
+)
  - mac_e
@@ -2875,387 +2875,4 @@
 n'%5D%0A
-%0Adef parse_date(datestring):%0A%09datestring = datestring.lower()%0A%09weekday, month, day, tim, year = datestring.split()%0A%09weekday = _weekdays.index(weekday)%0A%09month = _months.index(month)%0A%09year = int(year)%0A%09day = int(day)%0A%09hour, minute, second = %5Bint(item) for item in tim.split(%22:%22)%5D%0A%09t = (year, month, day, hour, minute, second, weekday, 0, 0)%0A%09return int(time.mktime(t) - time.timezone)%0A

Simplify date parsing