File: gmDateTimeInput.py

package info (click to toggle)
gnumed-client 0.2.2a-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,448 kB
  • ctags: 3,620
  • sloc: python: 32,178; sh: 231; makefile: 97
file content (894 lines) | stat: -rw-r--r-- 26,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# -*- coding: iso-8859-1 -*-
"""GNUmed date input widget

All GNUmed date input should happen via classes in
this module. Initially this is just a plain text box
but using this throughout GNUmed will allow us to
transparently add features.

@copyright: author(s)
"""
#==============================================================================
# $Source: /sources/gnumed/gnumed/gnumed/client/wxpython/gmDateTimeInput.py,v $
# $Id: gmDateTimeInput.py,v 1.41 2006/07/19 20:29:50 ncq Exp $
__version__ = "$Revision: 1.41 $"
__author__  = "K. Hilbert <Karsten.Hilbert@gmx.net>"
__licence__ = "GPL (details at http://www.gnu.org)"

import re, string, sys, time

import mx.DateTime as mxDT
try:
	import wxversion
	import wx
except ImportError:
	from wxPython import wx

from Gnumed.pycommon import gmLog, gmMatchProvider, gmExceptions, gmI18N, gmFuzzyTimestamp
from Gnumed.wxpython import gmPhraseWheel, gmGuiHelpers

_log = gmLog.gmDefLog

month_length = {
	1: 31,
	2: 28,		# FIXME: make leap year aware
	3: 31,
	4: 30,
	5: 31,
	6: 30,
	7: 31,
	8: 31,
	9: 30,
	10: 31,
	11: 30,
	12: 31
}

#============================================================
class cMatchProvider_FuzzyTimestamp(gmMatchProvider.cMatchProvider):
	def __init__(self):
		self.__allow_past = 1 
		self.__shifting_base = None
		self.__expanders = []
		self.__expanders.append(self.__numbers_only)
		self.__expanders.append(self.__single_char)
		self.__expanders.append(self.__explicit_offset)
		self.__expanders.append(self.__single_slash)
		self.__expanders.append(self.__single_dot)
		self.set_single_character_triggers()
		self.set_offset_chars()
		gmMatchProvider.cMatchProvider.__init__(self)
	#--------------------------------------------------------
	# external API
	#--------------------------------------------------------
	def set_single_character_triggers(self, triggers = 'ndmy'):
		"""Set trigger characters.

		Default is 'ndmy':
			n - now
			d - toDay
			m - toMorrow	Someone please suggest a synonym !
			y - yesterday

		This also defines the significance of the order of the characters.
		"""
		self.__single_char_triggers = triggers[:4].lower()
	#--------------------------------------------------------
	def set_offset_chars(self, offset_chars = 'hdwmy'):
		"""Set offset characters.

		Default is 'hdwm':
			h - hours
			d - days
			w - weeks
			m - months
			y - years

		This also defines the significance of the order of the characters.
		"""
		self.__offset_chars = offset_chars[:5].lower()
	#--------------------------------------------------------
	# base class API
	#--------------------------------------------------------
	# internal matching algorithms
	#
	# if we end up here:
	#	- aFragment will not be "None"
	#   - aFragment will be lower case
	#	- we _do_ deliver matches (whether we find any is a different story)
	#--------------------------------------------------------
	def getMatchesByPhrase(self, aFragment):
		"""Return matches for aFragment at start of phrases."""
		self.__now = mxDT.now()
		aFragment = aFragment.strip()
		matches = []
		for expander in self.__expanders:
			items = expander(aFragment)
			if items is not None:
				matches.extend(items)
		if len(matches) > 0:
			return (True, matches)
		else:
			return (False, [])
	#--------------------------------------------------------
	def getMatchesByWord(self, aFragment):
		"""Return matches for aFragment at start of words inside phrases."""
		return self.getMatchesByPhrase(aFragment)
	#--------------------------------------------------------
	def getMatchesBySubstr(self, aFragment):
		"""Return matches for aFragment as a true substring."""
		return self.getMatchesByPhrase(aFragment)
	#--------------------------------------------------------
	def getAllMatches(self):
		"""Return all items."""
		# FIXME: popup calendar to pick from
		return None
	#--------------------------------------------------------
	def __single_dot(self, aFragment):
		"""Expand fragments containing a single dot.

		Standard colloquial date format in Germany: day.month.year

		"14."
			- 14th current month this year
			- 14th next month this year
		"""
		if not re.match("^\d{1,2}\.{1}$", aFragment):
			return None
		val = int(aFragment.replace('.', ''))

		matches = []

		# day X of this month
		ts = self.__now + mxDT.RelativeDateTime(day = val)
		if val > 0 and val <= month_length[ts.month]:
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = ts, accuracy = gmFuzzyTimestamp.acc_days),
				'label': '%s.%s.%s - a %s this month' % (ts.day, ts.month, ts.year, ts.strftime('%A'))
			})

		# day X of next month
		ts = self.__now + mxDT.RelativeDateTime(day = val, months = +1)
		if val > 0 and val <= month_length[ts.month]:
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = ts, accuracy = gmFuzzyTimestamp.acc_days),
				'label': '%s.%s.%s - a %s next month' % (ts.day, ts.month, ts.year, ts.strftime('%A'))
			})

		# day X of last month
		ts = self.__now + mxDT.RelativeDateTime(day = val, months = -1)
		if val > 0 and val <= month_length[ts.month]:
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = ts, accuracy = gmFuzzyTimestamp.acc_days),
				'label': '%s.%s.%s - a %s last month' % (ts.day, ts.month, ts.year, ts.strftime('%A'))
			})

		return matches
	#--------------------------------------------------------
	# date fragment expanders
	#--------------------------------------------------------
	def __single_slash(self, aFragment):
		"""Expand fragments containing a single slash.

		"5/"
			- 2005/					(2000 - 2025)
			- 1995/					(1990 - 1999)
			- Mai/current year
			- Mai/next year
			- Mai/last year
			- Mai/200x
			- Mai/20xx
			- Mai/199x
			- Mai/198x
			- Mai/197x
			- Mai/19xx
		"""
		if not re.match("^\d{1,2}/{1}$", aFragment):
			return None
		val = int(aFragment.replace('/', ''))

		matches = []

		if val < 100 and val >= 0:
			matches.append ({
				'data': None,
				'label': '%s/' % (val + 1900)
			})

		if val < 26 and val >= 0:
			matches.append ({
				'data': None,
				'label': '%s/' % (val + 2000)
			})

		if val < 10 and val >= 0:
			matches.append ({
				'data': None,
				'label': '%s/' % (val + 1990)
			})

		if val < 13 and val > 0:
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = self.__now, accuracy = gmFuzzyTimestamp.acc_months),
				'label': '%.2d/%s' % (val, self.__now.year)
			})
			ts = self.__now + mxDT.RelativeDateTime(years = 1)
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = ts, accuracy = gmFuzzyTimestamp.acc_months),
				'label': '%.2d/%s' % (val, ts.year)
			})
			ts = self.__now + mxDT.RelativeDateTime(years = -1)
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = ts, accuracy = gmFuzzyTimestamp.acc_months),
				'label': '%.2d/%s' % (val, ts.year)
			})
			matches.append ({
				'data': None,
				'label': '%.2d/200' % val
			})
			matches.append ({
				'data': None,
				'label': '%.2d/20' % val
			})
			matches.append ({
				'data': None,
				'label': '%.2d/199' % val
			})
			matches.append ({
				'data': None,
				'label': '%.2d/198' % val
			})
			matches.append ({
				'data': None,
				'label': '%.2d/197' % val
			})
			matches.append ({
				'data': None,
				'label': '%.2d/19' % val
			})

		return matches
	#--------------------------------------------------------
	def __numbers_only(self, aFragment):
		"""This matches on single numbers.

		Spaces or tabs are discarded.
		"""
		if not re.match("^\d+$", aFragment):
			return None
		val = int(aFragment)

		matches = []

		# day X of this month
		if val <= month_length[self.__now.month]:
			ts = self.__now + mxDT.RelativeDateTime(day = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_days
			)
			tmp = {
				'data': target_date,
				'label': _('%d. of %s (this month) - a %s') % (val, ts.strftime('%B'), ts.strftime('%A'))
			}
			matches.append(tmp)

		# day X of next month
		if val <= month_length[(self.__now + mxDT.RelativeDateTime(months = 1)).month]:
			ts = self.__now + mxDT.RelativeDateTime(months = 1, day = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_days
			)
			tmp = {
				'data': target_date,
				'label': _('%d. of %s (next month) - a %s') % (val, ts.strftime('%B'), ts.strftime('%A'))
			}
			matches.append(tmp)

		# day X of last month
		if val <= month_length[(self.__now + mxDT.RelativeDateTime(months = -1)).month]:
			ts = self.__now + mxDT.RelativeDateTime(months = -1, day = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_days
			)
			tmp = {
				'data': target_date,
				'label': _('%d. of %s (last month) - a %s') % (val, ts.strftime('%B'), ts.strftime('%A'))
			}
			matches.append(tmp)

		# X days from now
		if val <= 400:				# more than a year ahead in days ?? nah !
			ts = self.__now + mxDT.RelativeDateTime(days = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts
			)
			tmp = {
				'data': target_date,
				'label': _('in %d day(s) - %s') % (val, target_date.timestamp.strftime('%A, %x'))
			}
			matches.append(tmp)

		# X weeks from now
		if val <= 50:				# pregnancy takes about 40 weeks :-)
			ts = self.__now + mxDT.RelativeDateTime(weeks = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts
			)
			tmp = {
				'data': target_date,
				'label': _('in %d week(s) - %s') % (val, target_date.timestamp.strftime('%A, %x'))
			}
			matches.append(tmp)

		# month X of ...
		if val < 13:
			# ... this year
			ts = self.__now + mxDT.RelativeDateTime(month = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_months
			)
			tmp = {
				'data': target_date,
				'label': _('%s (%s this year)') % (target_date, ts.strftime('%B'))
			}
			matches.append(tmp)

			# ... next year
			ts = self.__now + mxDT.RelativeDateTime(years = 1, month = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_months
			)
			tmp = {
				'data': target_date,
				'label': _('%s (%s next year)') % (target_date, ts.strftime('%B'))
			}
			matches.append(tmp)

			# ... last year
			ts = self.__now + mxDT.RelativeDateTime(years = -1, month = val)
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_months
			)
			tmp = {
				'data': target_date,
				'label': _('%s (%s last year)') % (target_date, ts.strftime('%B'))
			}
			matches.append(tmp)

			# fragment expansion
			matches.append ({
				'data': None,
				'label': '%s/200' % val
			})
			matches.append ({
				'data': None,
				'label': '%s/199' % val
			})
			matches.append ({
				'data': None,
				'label': '%s/198' % val
			})
			matches.append ({
				'data': None,
				'label': '%s/19' % val
			})

		# day X of ...
		if val < 8:
			# ... this week
			ts = self.__now + mxDT.RelativeDateTime(weekday = (val-1, 0))
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_days
			)
			tmp = {
				'data': target_date,
				'label': _('%s this week (%s of %s)') % (ts.strftime('%A'), ts.day, ts.strftime('%B'))
			}
			matches.append(tmp)

			# ... next week
			ts = self.__now + mxDT.RelativeDateTime(weeks = +1, weekday = (val-1, 0))
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_days
			)
			tmp = {
				'data': target_date,
				'label': _('%s next week (%s of %s)') % (ts.strftime('%A'), ts.day, ts.strftime('%B'))
			}
			matches.append(tmp)

			# ... last week
			ts = self.__now + mxDT.RelativeDateTime(weeks = -1, weekday = (val-1, 0))
			target_date = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = ts,
				accuracy = gmFuzzyTimestamp.acc_days
			)
			tmp = {
				'data': target_date,
				'label': _('%s last week (%s of %s)') % (ts.strftime('%A'), ts.day, ts.strftime('%B'))
			}
			matches.append(tmp)

		if val < 100:
			matches.append ({
				'data': None,
				'label': '%s/' % (1900 + val)
			})

		if val == 200:
			tmp = {
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = self.__now, accuracy = gmFuzzyTimestamp.acc_days),
				'label': '%s' % target_date
			}
			matches.append(tmp)
			matches.append ({
				'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = self.__now, accuracy = gmFuzzyTimestamp.acc_months),
				'label': '%.2d/%s' % (self.__now.month, self.__now.year)
			})
			matches.append ({
				'data': None,
				'label': '%s/' % self.__now.year
			})
			matches.append ({
				'data': None,
				'label': '%s/' % (self.__now.year + 1)
			})
			matches.append ({
				'data': None,
				'label': '%s/' % (self.__now.year - 1)
			})

		if val < 200 and val >= 190:
			for i in range(10):
				matches.append ({
					'data': None,
					'label': '%s%s/' % (val, i)
				})

		return matches
	#--------------------------------------------------------
	def __single_char(self, aFragment):
		"""This matches on single characters.

		Spaces and tabs are discarded."""

		if not re.match('^[%s]$' % self.__single_char_triggers, aFragment):
			return None
		val = aFragment.lower()

		# FIXME: handle uebermorgen/vorgestern ?

		# right now
		if val == self.__single_char_triggers[0]:
			ts = self.__now
			return [{
				'data': gmFuzzyTimestamp.cFuzzyTimestamp (
					timestamp = ts,
					accuracy = gmFuzzyTimestamp.acc_subseconds
				),
				'label': _('right now (%s, %s)') % (ts.strftime('%A'), ts)
			}]

		# today
		if val == self.__single_char_triggers[1]:
			return [{
				'data': gmFuzzyTimestamp.cFuzzyTimestamp (
					timestamp = self.__now,
					accuracy = gmFuzzyTimestamp.acc_days
				),
				'label': _('today (%s)') % self.__now.strftime('%A, %Y-%m-%d')
			}]

		# tomorrow
		if val == self.__single_char_triggers[2]:
			ts = self.__now + mxDT.RelativeDateTime(days = +1)
			return [{
				'data': gmFuzzyTimestamp.cFuzzyTimestamp (
					timestamp = ts,
					accuracy = gmFuzzyTimestamp.acc_days
				),
				'label': _('tomorrow (%s)') % ts.strftime('%A, %Y-%m-%d')
			}]

		# yesterday
		if val == self.__single_char_triggers[3]:
			ts = self.__now + mxDT.RelativeDateTime(days = -1)
			return [{
				'data': gmFuzzyTimestamp.cFuzzyTimestamp (
					timestamp = ts,
					accuracy = gmFuzzyTimestamp.acc_days
				),
				'label': _('yesterday (%s)') % ts.strftime('%A, %Y-%m-%d')
			}]

		return None
	#--------------------------------------------------------
	def __explicit_offset(self, aFragment):
		# "+/-XXd/w/m/t"
		if not re.match("^(\+|-)?(\s|\t)*\d{1,2}(\s|\t)*[%s]$" % self.__offset_chars, aFragment):
			return None
		aFragment = aFragment.lower()

		# allow past ?
		is_future = True
		if string.find(aFragment, '-') > -1:
			is_future = False

		val = int(re.search('\d{1,2}', aFragment).group())

		ts = None
		# hours
		if aFragment[-1] == self.__offset_chars[0]:
			if is_future:
				ts = self.__now + mxDT.RelativeDateTime(hours = val)
				label = _('in %d hour(s) - %s') % (val, ts.strftime('%H:%M'))
			else:
				ts = self.__now - mxDT.RelativeDateTime(hours = val)
				label = _('%d hour(s) ago - %s') % (val, ts.strftime('%H:%M'))
			accuracy = gmFuzzyTimestamp.acc_subseconds
		# days
		elif aFragment[-1] == self.__offset_chars[1]:
			if is_future:
				ts = self.__now + mxDT.RelativeDateTime(days = val)
				label = _('in %d day(s) - %s') % (val, ts.strftime('%A, %x'))
			else:
				ts = self.__now - mxDT.RelativeDateTime(days = val)
				label = _('%d day(s) ago - %s') % (val, ts.strftime('%A, %x'))
			accuracy = gmFuzzyTimestamp.acc_days
		# weeks
		elif aFragment[-1] == self.__offset_chars[2]:
			if is_future:
				ts = self.__now + mxDT.RelativeDateTime(weeks = val)
				label = _('in %d week(s) - %s') % (val, ts.strftime('%A, %x'))
			else:
				ts = self.__now - mxDT.RelativeDateTime(weeks = val)
				label = _('%d week(s) ago - %s)') % (val, ts.strftime('%A, %x'))
			accuracy = gmFuzzyTimestamp.acc_days
		# months
		elif aFragment[-1] == self.__offset_chars[3]:
			if is_future:
				ts = self.__now + mxDT.RelativeDateTime(months = val)
				label = _('in %d month(s) - %s') % (val, ts.strftime('%A, %x'))
			else:
				ts = self.__now - mxDT.RelativeDateTime(months = val)
				label = _('%d month(s) ago - %s') % (val, ts.strftime('%A, %x'))
			accuracy = gmFuzzyTimestamp.acc_days
		# years
		elif aFragment[-1] == self.__offset_chars[4]:
			if is_future:
				ts = self.__now + mxDT.RelativeDateTime(years = val)
				label = _('in %d year(s) - %s') % (val, ts.strftime('%A, %x'))
			else:
				ts = self.__now - mxDT.RelativeDateTime(years = val)
				label = _('%d year(s) ago - %s') % (val, ts.strftime('%A, %x'))
			accuracy = gmFuzzyTimestamp.acc_months

		if ts is None:
			return None

		tmp = {
			'data': gmFuzzyTimestamp.cFuzzyTimestamp(timestamp = ts, accuracy = accuracy),
			'label': label
		}
		return [tmp]
#==================================================
class cFuzzyTimestampInput(gmPhraseWheel.cPhraseWheel):

	def __init__(self, *args, **kwargs):

		# setup custom match provider
		matcher = cMatchProvider_FuzzyTimestamp()
		matcher.setWordSeparators('xxx_do_not_separate_words_xxx')
#		matcher.setIgnoredChars("""[?!."'\\(){}\[\]<>~#*$%^_]+""")
		matcher.setThresholds(aWord = 998, aSubstring = 999)
		kwargs['aMatchProvider'] = matcher

		gmPhraseWheel.cPhraseWheel.__init__(self, *args, **kwargs)
		self.allow_multiple_phrases(False)
		self.selection_only = False

	#--------------------------------------------------------
	# internal helpers
	#--------------------------------------------------------
	def __text2timestamp(self):
		# does have timestamp object associated with it
		if self.data is not None:
			return True

		val = self.GetValue().strip()

		# skip empty value
		if val == '':
			return True

		# xx/yyyy ?
		if re.match("^\d{1,2}/{1}\d{4}$", val):
			parts = val.split('/')
			self.data = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = mxDT.now() + mxDT.RelativeDateTime(year = int(part[1]), month = int(part[0])),
				accuracy = gmFuzzyTimestamp.acc_months
			)
			return True

		# date ?
		try:
			date = mxDT.Parser.DateFromString (
				text = val,
				formats = ('euro', 'iso', 'us', 'altus', 'altiso', 'lit', 'altlit', 'eurlit')
			)
			accuracy = gmFuzzyTimestamp.acc_days
			# time, too ?
			time = mxDT.Parser.TimeFromString(text = val)
			datetime = date + time
			if datetime != date:
				accuracy = gmFuzzyTimestamp.acc_subseconds
			self.data = gmFuzzyTimestamp.cFuzzyTimestamp (
				timestamp = datetime,
				accuracy = accuracy
			)
			return True
		except (ValueError, mxDT.RangeError):
			return False

		# valid date
		return True
	#--------------------------------------------------------
	# phrasewheel internal API
	#--------------------------------------------------------
	def _on_lose_focus(self, event):
		if not self.__text2timestamp():
			msg = _('Cannot parse <%s> into proper timestamp.') % self.GetValue().strip()
			wx.CallAfter(gmGuiHelpers.gm_beep_statustext, msg)
			self.SetBackgroundColour('pink')
			self.Refresh()
		else:
			self.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
			self.Refresh()

		gmPhraseWheel.cPhraseWheel._on_lose_focus(self, event)
	#--------------------------------------------------------
	def _calc_display_string(self):
		data = self._picklist.GetClientData(self._picklist.GetSelection())
		if data is None:
			data = self._picklist.GetStringSelection()
		return '%s' % data
	#--------------------------------------------------------
	# external API
	#--------------------------------------------------------
	def SetValue(self, val, data=None):
		gmPhraseWheel.cPhraseWheel.SetValue(self, val, data=data)
		if self.data is None:
			self.__text2timestamp()
	#--------------------------------------------------------
	def is_valid_timestamp(self):
		self.__text2timestamp()
		if self.data is None:
			return False
		return True
#==================================================
class cTimeInput(wx.TextCtrl):
	def __init__(self, parent, *args, **kwargs):
		if len(args) < 2:
			if not kwargs.has_key('value'):
				kwargs['value'] = _('enter time here')
		wx.TextCtrl.__init__(
			self,
			parent,
			*args,
			**kwargs
		)
	#----------------------------------------------
#==================================================
# main
#--------------------------------------------------
if __name__ == '__main__':
	#----------------------------------------------------
	def clicked (data):
		print "Selected :%s" % data
	#----------------------------------------------------
	class TestApp (wx.App):
		def OnInit (self):

			frame = wx.Frame (
				None,
				-4,
				"date input wheel test for GNUmed",
				size=wx.Size(300, 350),
				style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE
			)

			date_wheel = cFuzzyTimestampInput (
				parent = frame,
				id = -1,
				pos = (50, 50),
				size = (180, 30)
			)
			date_wheel.on_resize (None)

			frame.Show (1)
			return 1

	#--------------------------------------------------------
	def test_cli():
		mp = cMatchProvider_FuzzyTimestamp()
		mp.setWordSeparators('xxx_do_not_separate_words_xxx')
		mp.setThresholds(aWord = 998, aSubstring = 999)
		val = None
		while val != 'exit':
			print "************************************"
			val = raw_input('Enter date fragment: ')
			found, matches = mp.getMatches(aFragment=val)
			for match in matches:
				print match['label']
				print match['data']
				print "---------------"
	#--------------------------------------------------------
	def test_gui():
		gmI18N.activate_locale()
		gmI18N.install_domain()
		app = wx.PyWidgetTester(size = (200, 300))
		app.SetWidget(cFuzzyTimestampInput, id=-1, size=(180,20), pos=(10,20))
		app.MainLoop()
	#--------------------------------------------------------
#	test_cli()
	test_gui()

#==================================================
# - free text input: start string with "
#==================================================
# $Log: gmDateTimeInput.py,v $
# Revision 1.41  2006/07/19 20:29:50  ncq
# - import cleanup
#
# Revision 1.40  2006/07/01 13:12:32  ncq
# - improve test harness
#
# Revision 1.39  2006/06/15 15:35:30  ncq
# - better error handling
#
# Revision 1.38  2006/06/05 21:30:08  ncq
# - add single-dot expander so German 23. expands to 23rd this month this year
# - add is_valid_timestamp() to external API so patient wizard can use it
#
# Revision 1.37  2006/06/04 21:50:32  ncq
# - cleanup
#
# Revision 1.36  2006/06/02 13:17:50  ncq
# - add configurable offset chars for i18n
# - various cleanups and optimizations
# - fix __explicit_offset to use proper fuzzy timestamp
# - __validate() -> __text2timestamp() and smarten up
#
# Revision 1.35  2006/06/02 10:12:09  ncq
# - cleanup
# - add more fragment expanders
#
# Revision 1.34  2006/05/24 10:35:38  ncq
# - better named match provider
#
# Revision 1.33  2006/05/24 10:12:37  ncq
# - cleanup
# - timestamp match provider:
#   - use fuzzy timestamp
#   - i18n()ize single character triggers
#   - improve phrasewheel strings
# - fuzzy timestamp phrasewheel
#   - a lot of cleanup
# - proper test code
#
# Revision 1.32  2006/05/20 18:37:10  ncq
# - cleanup
#
# Revision 1.31  2006/05/12 12:08:51  ncq
# - comment out proposed fix for unicode problems
#
# Revision 1.30  2005/09/28 21:27:30  ncq
# - a lot of wx2.6-ification
#
# Revision 1.29  2005/09/28 15:57:47  ncq
# - a whole bunch of wx.Foo -> wx.Foo
#
# Revision 1.28  2005/09/26 18:01:50  ncq
# - use proper way to import wx26 vs wx2.4
# - note: THIS WILL BREAK RUNNING THE CLIENT IN SOME PLACES
# - time for fixup
#
# Revision 1.27  2005/09/25 01:13:06  ihaywood
# use a nicer way of discovering non-Unicode wxPython builds
# I resisted the temptation to use "if os.environ['USER'] == 'ncq':"
#
# Revision 1.26  2005/09/25 01:00:47  ihaywood
# bugfixes
#
# remember 2.6 uses "import wx" not "from wxPython import wx"
# removed not null constraint on clin_encounter.rfe as has no value on instantiation
# client doesn't try to set clin_encounter.description as it doesn't exist anymore
#
# Revision 1.25  2005/08/22 13:03:46  ncq
# - set bounds on "day of month" calculations
#
# Revision 1.24  2005/07/31 16:22:25  ncq
# - need to import "time"
#
# Revision 1.23  2005/07/31 16:04:19  ncq
# - on some platforms, notably MS Windows mx.DateTime does not support
#   strptime(), hence use time.strptime()
#
# Revision 1.22  2005/07/31 15:32:50  ncq
# - cleanup
#
# Revision 1.21  2005/07/31 15:23:40  ncq
# - fixed long-standing validation logic bug
# - logging is best done using proper syntax, too
#
# Revision 1.20  2005/07/27 15:17:06  ncq
# - properly catch date input error such that we
#   may find the bug on Windows
#
# Revision 1.19  2005/06/08 22:01:42  cfmoro
# Avoid validating when empty date
#
# Revision 1.18  2005/06/08 21:19:50  cfmoro
# Crash fix
#
# Revision 1.17  2005/06/04 09:55:32  ncq
# - also call parent class _on_lose_focus so we don't loose
#   callbacks set by other people
#
# Revision 1.16  2005/06/03 00:54:33  cfmoro
# Validte date in SetValue
#
# Revision 1.15  2005/06/03 00:36:54  cfmoro
# Validate date on setValue
#
# Revision 1.14  2005/06/02 23:28:54  cfmoro
# Date validation
#
# Revision 1.13  2005/04/25 17:11:33  ncq
# - set encoding for file
#
# Revision 1.12  2005/04/24 15:05:22  ncq
# - use gmI18N properly
#
# Revision 1.11  2004/12/23 16:20:15  ncq
# - add licence
#
# Revision 1.10  2004/07/18 20:30:53  ncq
# - wxPython.true/false -> Python.True/False as Python tells us to do
#
# Revision 1.9  2004/03/05 11:22:35  ncq
# - import from Gnumed.<pkg>
#
# Revision 1.8  2004/02/25 09:46:21  ncq
# - import from pycommon now, not python-common
#
# Revision 1.7  2003/11/05 22:21:06  sjtan
#
# let's gmDateInput specify id_callback in constructor list.
#
# Revision 1.6  2003/11/04 10:35:23  ihaywood
# match providers in gmDemographicRecord
#
# Revision 1.5  2003/10/06 17:49:40  ncq
# - remove dependancy on gmI18N on standalone test run
#
# Revision 1.4  2003/10/02 20:51:12  ncq
# - add alt-XX shortcuts, move __* to _*
#
# Revision 1.3  2003/09/30 18:47:47  ncq
# - converted date time input field into phrase wheel descendant
#
# Revision 1.2  2003/08/10 00:57:15  ncq
# - add TODO item
#
# Revision 1.1  2003/05/23 14:05:01  ncq
# - first implementation
#