Changeset 9383 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2016-01-10T13:04:06+01:00 (8 years ago)
Author:
simon04
Message:

Deprecate PrimaryDateParser in favour of DateUtils

Location:
trunk/src/org/openstreetmap/josm
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/WayPoint.java

    r8846 r9383  
    1212import org.openstreetmap.josm.data.coor.LatLon;
    1313import org.openstreetmap.josm.data.projection.Projections;
    14 import org.openstreetmap.josm.tools.date.PrimaryDateParser;
     14import org.openstreetmap.josm.tools.date.DateUtils;
    1515import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
    1616
    1717public class WayPoint extends WithAttributes implements Comparable<WayPoint>, TemplateEngineDataProvider {
    18 
    19     private static ThreadLocal<PrimaryDateParser> dateParser = new ThreadLocal<PrimaryDateParser>() {
    20         @Override protected PrimaryDateParser initialValue() {
    21             return new PrimaryDateParser();
    22         }
    23     };
    2418
    2519    public double time;
     
    9993
    10094    /**
     95     * Sets the {@link #time} field as well as the {@link #PT_TIME} attribute to the specified time
     96     *
     97     * @param time the time to set
     98     * @since 9383
     99     */
     100    public void setTime(Date time) {
     101        this.time = time.getTime() / 1000.;
     102        this.attr.put(PT_TIME, DateUtils.fromDate(time));
     103    }
     104
     105    /**
    101106     * Convert the time stamp of the waypoint into seconds from the epoch
    102107     */
    103108    public void setTime() {
     109        setTimeFromAttribute();
     110    }
     111
     112    /**
     113     * Convert the time stamp of the waypoint into seconds from the epoch
     114     * @return The parsed time if successful, or {@code null}
     115     * @since 9383
     116     */
     117    public Date setTimeFromAttribute() {
    104118        if (attr.containsKey(PT_TIME)) {
    105119            try {
    106                 time = dateParser.get().parse(get(PT_TIME).toString()).getTime() / 1000.; /* ms => seconds */
     120                final Date time = DateUtils.fromString(get(PT_TIME).toString());
     121                setTime(time);
     122                return time;
    107123            } catch (Exception e) {
     124                Main.warn(e);
    108125                time = 0;
    109126            }
    110127        }
     128        return null;
    111129    }
    112130
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java

    r9270 r9383  
    8181import org.openstreetmap.josm.tools.Utils;
    8282import org.openstreetmap.josm.tools.date.DateUtils;
    83 import org.openstreetmap.josm.tools.date.PrimaryDateParser;
    8483import org.xml.sax.SAXException;
    8584
     
    10141013
    10151014            List<ImageEntry> imgs = getSortedImgList();
    1016             PrimaryDateParser dateParser = new PrimaryDateParser();
    10171015
    10181016            // no images found, exit
     
    10321030                for (GpxTrackSegment segment : trk.getSegments()) {
    10331031                    for (WayPoint curWp : segment.getWayPoints()) {
    1034                         String curDateWpStr = curWp.getString(GpxConstants.PT_TIME);
    1035                         if (curDateWpStr == null) {
    1036                             continue;
    1037                         }
    1038 
    10391032                        try {
    1040                             firstGPXDate = dateParser.parse(curDateWpStr).getTime()/1000;
    1041                             break outer;
     1033                            final Date parsedTime = curWp.setTimeFromAttribute();
     1034                            if (parsedTime != null) {
     1035                                firstGPXDate = parsedTime.getTime();
     1036                                break outer;
     1037                            }
    10421038                        } catch (Exception e) {
    10431039                            Main.warn(e);
     
    11531149        int ret = 0;
    11541150
    1155         PrimaryDateParser dateParser = new PrimaryDateParser();
    1156 
    11571151        for (GpxTrack trk : selectedGpx.tracks) {
    11581152            for (GpxTrackSegment segment : trk.getSegments()) {
     
    11621156
    11631157                for (WayPoint curWp : segment.getWayPoints()) {
    1164 
    1165                     String curWpTimeStr = curWp.getString(GpxConstants.PT_TIME);
    1166                     if (curWpTimeStr != null) {
    1167 
    1168                         try {
    1169                             long curWpTime = dateParser.parse(curWpTimeStr).getTime() + offset;
     1158                    try {
     1159                        final Date parsedTime = curWp.setTimeFromAttribute();
     1160                        if (parsedTime != null) {
     1161                            final long curWpTime = parsedTime.getTime() + offset;
    11701162                            ret += matchPoints(images, prevWp, prevWpTime, curWp, curWpTime, offset);
    11711163
    11721164                            prevWp = curWp;
    11731165                            prevWpTime = curWpTime;
    1174 
    1175                         } catch (ParseException e) {
    1176                             Main.error("Error while parsing date \"" + curWpTimeStr + '"');
    1177                             Main.error(e);
    1178                             prevWp = null;
    1179                             prevWpTime = 0;
     1166                            continue;
    11801167                        }
    1181                     } else {
    1182                         prevWp = null;
    1183                         prevWpTime = 0;
     1168                    } catch (Exception e) {
     1169                        Main.warn(e);
    11841170                    }
     1171                    prevWp = null;
     1172                    prevWpTime = 0;
    11851173                }
    11861174            }
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/ImageEntry.java

    r9331 r9383  
    55import java.io.File;
    66import java.io.IOException;
    7 import java.text.ParseException;
    87import java.util.Calendar;
    98import java.util.Collections;
     
    449448        try {
    450449            setExifTime(ExifReader.readTime(file));
    451         } catch (ParseException ex) {
     450        } catch (RuntimeException ex) {
    452451            setExifTime(null);
    453452        }
  • trunk/src/org/openstreetmap/josm/tools/ExifReader.java

    r8509 r9383  
    55import java.io.File;
    66import java.io.IOException;
    7 import java.text.ParseException;
    87import java.util.Date;
    98
    109import org.openstreetmap.josm.Main;
    1110import org.openstreetmap.josm.data.coor.LatLon;
    12 import org.openstreetmap.josm.tools.date.PrimaryDateParser;
     11import org.openstreetmap.josm.tools.date.DateUtils;
    1312
    1413import com.drew.imaging.jpeg.JpegMetadataReader;
     
    3837     * @param filename The JPEG file to read
    3938     * @return The date/time read in the EXIF section, or {@code null} if not found
    40      * @throws ParseException if {@link PrimaryDateParser#parse} fails to parse date/time
    41      */
    42     public static Date readTime(File filename) throws ParseException {
     39     */
     40    public static Date readTime(File filename) {
    4341        try {
    4442            Metadata metadata = JpegMetadataReader.readMetadata(filename);
     
    6058            if (dateStr != null) {
    6159                dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228
    62                 return new PrimaryDateParser().parse(dateStr);
    63             }
    64         } catch (ParseException e) {
    65             throw e;
     60                return DateUtils.fromString(dateStr);
     61            }
    6662        } catch (Exception e) {
    6763            Main.error(e);
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r9059 r9383  
    4545     * with the timezone lookup, is very expensive.
    4646     */
    47     private static GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
     47    private static final GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
     48    private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault());
    4849    private static final DatatypeFactory XML_DATE;
    4950
    5051    static {
    5152        calendar.setTimeInMillis(0);
     53        calendarLocale.setTimeInMillis(0);
    5254
    5355        DatatypeFactory fact = null;
     
    7880        if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
    7981                checkLayout(str, "xxxx-xx-xxTxx:xx:xx") ||
     82                checkLayout(str, "xxxx:xx:xx xx:xx:xx") ||
    8083                checkLayout(str, "xxxx-xx-xx xx:xx:xx UTC") ||
    8184                checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") ||
    8285                checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) {
    83             calendar.set(
     86            final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx") ? calendarLocale : calendar; // consider EXIF date in default timezone
     87            c.set(
    8488                parsePart4(str, 0),
    8589                parsePart2(str, 5)-1,
     
    9296                int plusHr = parsePart2(str, 20);
    9397                int mul = str.charAt(19) == '+' ? -3600000 : 3600000;
    94                 return calendar.getTimeInMillis()+plusHr*mul;
     98                return c.getTimeInMillis()+plusHr*mul;
    9599            }
    96100
    97             return calendar.getTimeInMillis();
     101            return c.getTimeInMillis();
    98102        } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") ||
    99103                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") ||
     104                checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ||
    100105                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") ||
    101106                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) {
    102             calendar.set(
     107            final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? calendarLocale : calendar; // consider EXIF date in default timezone
     108            c.set(
    103109                parsePart4(str, 0),
    104110                parsePart2(str, 5)-1,
     
    112118            }
    113119
    114             return calendar.getTimeInMillis() + millis;
     120            return c.getTimeInMillis() + millis;
    115121        } else {
    116122            // example date format "18-AUG-08 13:33:03"
  • trunk/src/org/openstreetmap/josm/tools/date/PrimaryDateParser.java

    r9078 r9383  
    1717 *
    1818 * @author Brett Henderson
     19 * @deprecated Use {@link DateUtils} instead!
    1920 */
     21@Deprecated
    2022public class PrimaryDateParser {
    2123    private DatatypeFactory datatypeFactory;
Note: See TracChangeset for help on using the changeset viewer.