summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/syndication/util/SyndDateUtils.java
blob: 56687ac2ec93939f761fae6c5ae6ce2604ccb8c7 (plain)
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
package de.danoeh.antennapod.syndication.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Parses several date formats.
 */
public class SyndDateUtils {
    private static final String TAG = "DateUtils";

    private static final String[] RFC822DATES = {"dd MMM yy HH:mm:ss Z",};

    /**
     * RFC 3339 date format for UTC dates.
     */
    public static final String RFC3339UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    /**
     * RFC 3339 date format for localtime dates with offset.
     */
    public static final String RFC3339LOCAL = "yyyy-MM-dd'T'HH:mm:ssZ";

    private static ThreadLocal<SimpleDateFormat> RFC822Formatter = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(RFC822DATES[0], Locale.US);
        }

    };

    private static ThreadLocal<SimpleDateFormat> RFC3339Formatter = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(RFC3339UTC, Locale.US);
        }

    };

    public static Date parseRFC822Date(String date) {
        Date result = null;
        if (date.contains("PDT")) {
            date = date.replace("PDT", "PST8PDT");
        }
        if (date.contains(",")) {
            // Remove day of the week
            date = date.substring(date.indexOf(",") + 1).trim();
        }
        SimpleDateFormat format = RFC822Formatter.get();
        for (int i = 0; i < RFC822DATES.length; i++) {
            try {
                format.applyPattern(RFC822DATES[i]);
                result = format.parse(date);
                break;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (result == null) {
            Log.e(TAG, "Unable to parse feed date correctly");
        }

        return result;
    }

    public static Date parseRFC3339Date(String date) {
        Date result = null;
        SimpleDateFormat format = RFC3339Formatter.get();
        boolean isLocal = date.endsWith("Z");
        if (date.contains(".")) {
            // remove secfrac
            int fracIndex = date.indexOf(".");
            String first = date.substring(0, fracIndex);
            String second = null;
            if (isLocal) {
                second = date.substring(date.length() - 1);
            } else {
                if (date.contains("+")) {
                    second = date.substring(date.indexOf("+"));
                } else {
                    second = date.substring(date.indexOf("-"));
                }
            }

            date = first + second;
        }
        if (isLocal) {
            try {
                result = format.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            format.applyPattern(RFC3339LOCAL);
            // remove last colon
            StringBuffer buf = new StringBuffer(date.length() - 1);
            int colonIdx = date.lastIndexOf(':');
            for (int x = 0; x < date.length(); x++) {
                if (x != colonIdx)
                    buf.append(date.charAt(x));
            }
            String bufStr = buf.toString();
            try {
                result = format.parse(bufStr);
            } catch (ParseException e) {
                e.printStackTrace();
                Log.e(TAG, "Unable to parse date");
            } finally {
                format.applyPattern(RFC3339UTC);
            }

        }

        return result;

    }

    /**
     * Takes a string of the form [HH:]MM:SS[.mmm] and converts it to
     * milliseconds.
     *
     * @throws java.lang.NumberFormatException if the number segments contain invalid numbers.
     */
    public static long parseTimeString(final String time) {
        String[] parts = time.split(":");
        long result = 0;
        int idx = 0;
        if (parts.length == 3) {
            // string has hours
            result += Integer.valueOf(parts[idx]) * 3600000L;
            idx++;
        }
        result += Integer.valueOf(parts[idx]) * 60000L;
        idx++;
        result += (Float.valueOf(parts[idx])) * 1000L;
        return result;
    }

    public static String formatRFC822Date(Date date) {
        SimpleDateFormat format = RFC822Formatter.get();
        return format.format(date);
    }

    public static String formatRFC3339Local(Date date) {
        SimpleDateFormat format = RFC3339Formatter.get();
        format.applyPattern(RFC3339LOCAL);
        String result = format.format(date);
        format.applyPattern(RFC3339UTC);
        return result;
    }

    public static String formatRFC3339UTC(Date date) {
        SimpleDateFormat format = RFC3339Formatter.get();
        format.applyPattern(RFC3339UTC);
        return format.format(date);
    }
}