Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
/**
 * 
 */
package timeflow.format.field;
 
import java.text.ParseException;
import java.util.Calendar;
 
import timeflow.data.time.RoughTime;
import timeflow.data.time.TimeUnit;
import timeflow.data.time.TimeUtils;
 
public class FormatDateTime extends FieldFormat
{
   DateTimeGuesser dateGuesser=new DateTimeGuesser();
   
   @Override
   public String format(Object o) {
       return ((RoughTime)o).format();
   }
 
   @Override
   public Object _parse(String s) throws Exception {
       if (s.length()==0)
           return null;
       Object o= readTime(s);
       if (o==null)
           throw new IllegalArgumentException();
       return o;
   }
   
   
   public RoughTime readTime(Object o) throws ParseException
   {
       if (!(o instanceof String))
           throw new IllegalArgumentException("Expected String, got: "+o);
       return dateGuesser.guess((String)o);
   }
   DateTimeGuesser g=new DateTimeGuesser();
   
   @Override
   public double scoreFormatMatch(String s) {
       if (s==null || s.length()==0)
           return -.05;
       try
       {
           RoughTime f=g.guess(s);
           if (f==null)
               return -1;
           if (g.getLastGoodFormat().getUnits()==TimeUnit.YEAR)
           {
               int year=TimeUtils.cal(f.getTime()).get(Calendar.YEAR);
               if (year>2100)
                   return -1;
               if (year>1900 && year<2050)
                   return 1;
               if (year>2050 || year<1600)
                   return .1;
               return .5;
           }
           return 2;
       }
       catch (Exception e)
       {
           return -1;
       }
   }
 
 
   @Override
   public Class getType() {
       return RoughTime.class;
   }
 
   @Override
   public String getHumanName() {
       return "Date/Time";
   }        
}