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