0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 function msec = parse(str, format, timezone)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % second and third arguments are optional
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 if nargin < 3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 timezone = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 if nargin < 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 format = '';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % default timezone
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 if isempty(timezone)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 timezone = time.timezone;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % convert string timezone into java object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 if ischar(timezone)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 timezone = java.util.TimeZone.getTimeZone(timezone);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 % obtain a java time format description to use for parsing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 if isempty(format)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % infere it from the string
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 format = parse_time_string(str);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % convert a given MATLAB time format into a Java one
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 format = time.matfrmt2javafrmt(format);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % parse the string accordingly to the corrent format and timezone
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 tformat = java.text.SimpleDateFormat(format, java.util.Locale.UK);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 tformat.setTimeZone(timezone);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 try
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % It is necessary to replace 'GMT+03' by 'GMT+03:00' because java
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % expects the ':00' at the end.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 str = regexprep(str, '([gG][mM][tT][+-]\d\d?$)', '$1:00');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 msec = tformat.parse(str).getTime();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 catch
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 error('### unable to parse time string ''%s'' accordingly to format ''%s''', str, format);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 function str = parse_time_string(str)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 % supported patterns
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 parse = { '\s[gG][mM][tT][+-]?\d\d?$', ' z'; ... % GMT+1 or GMT+12
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 '\s[gG][mM][tT][+-]?\d\d?:\d\d$', ' z'; ... % GMT+08:12
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 '\s\w{3}$', ' z'; ... % PST
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 '\s[+-]\d{4}$', ' Z'; ... % +0800
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 '\d{2}:\d{2}:\d{2}', 'HH:mm:ss'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 '\d{2}:\d{2}', 'mm:ss'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 '\d{2} \w{3} \d{4}', 'dd MMM yyyy'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 '\d{2}-\w{3}-\d{4}', 'dd-MMM-yyyy'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 '\d{2}.\w{3}.\d{4}', 'dd.MMM.yyyy'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 '\d{2}-\d{2}-\d{4}', 'dd-MM-yyyy'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 '\d{2}\.\d{2}\.\d{4}', 'dd.MM.yyyy'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 '\d{4}-\d{2}-\d{2}', 'yyyy-MM-dd'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 '\d{4}\.\d{2}\.\d{2}', 'yyyy.MM.dd'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 '\d{2}-\d{2}', 'MM-dd'; ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 '\.\d{1,3}', '.SSS'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 % try to match the patterns to the string and replace
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 % it with the corresponding Java time format descriptor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 re = parse(:,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 frmt = parse(:,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 str = regexprep(str, re, frmt);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 end
|