# Copyright (C) 2010 KenTyde BV
# All rights reserved.
#
# This software is licensed as described in the file LICENSE,
# which you should have received as part of this distribution.

from datetime import datetime, date

import fix42
import copy

def ddecode(s):
	return datetime.strptime(s, '%Y-%m-%d %H:%M:%S').date()

def dtdecode(s):
	return datetime.strptime(s, '%Y-%m-%d %H:%M:%S')

TYPES = {
	date: (str, ddecode),
	datetime: (lambda s: str(s)[:19], dtdecode),
}

def json_encode(msg):
	msg = copy.copy(msg)
	for k, v in msg.iteritems():
		
		if k in fix42.REPEAT:
			msg[k] = [json_encode(i) for i in v]
		else:
			codec = TYPES.get(fix42.WTAGS[k][1])
			msg[k] = v if codec is None else codec[0](v)
		
	return msg

def json_decode(msg):
	new = {}
	for k, v in msg.iteritems():
		
		if k.startswith('_'):
			continue
		
		if k in fix42.REPEAT:
			v = [json_decode(i) for i in v]
		else:
			codec = TYPES.get(fix42.WTAGS[k][1])
			v = v if codec is None else codec[1](v)
		
		new[k] = v
	
	return new

@@ -247,32 +247,50 @@
 def ddecode(s):%0A
+%09if len(s) %3E 10:%0A%09
 %09return datetime
@@ -329,16 +329,64 @@
 ).date()
+%0A%09return datetime.strptime(s, '%25Y-%25m-%25d').date()
 %0A%0Adef dt

Support date only strings.