Strabon

changeset 778:8df99e93a35d DiffDateTime

add java implementation of diffDateTime
author George Garbis <ggarbis@di.uoa.gr>
date Fri Nov 30 15:07:35 2012 +0200 (2012-11-30)
parents 31ad48021033
children bcf56f3e9988
files evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/DiffDateTime.java evaluation/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/evaluation/src/main/java/org/openrdf/query/algebra/evaluation/util/DiffDateTime.java	Fri Nov 30 15:07:35 2012 +0200
     1.3 @@ -0,0 +1,65 @@
     1.4 +/**
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.8 + *
     1.9 + * Copyright (C) 2012, Pyravlos Team
    1.10 + *
    1.11 + * http://www.strabon.di.uoa.gr/
    1.12 + */
    1.13 +package org.openrdf.query.algebra.evaluation.util;
    1.14 +
    1.15 +import java.text.SimpleDateFormat;
    1.16 +import java.util.Calendar;
    1.17 +import java.util.GregorianCalendar;
    1.18 +
    1.19 +import org.openrdf.model.Value;
    1.20 +import org.openrdf.model.ValueFactory;
    1.21 +import org.openrdf.query.algebra.evaluation.ValueExprEvaluationException;
    1.22 +import org.openrdf.query.algebra.evaluation.function.Function;
    1.23 +
    1.24 +/**
    1.25 + * @author Garmpis Georgios <ggarbis@di.uoa.gr>
    1.26 + *
    1.27 + */
    1.28 +public class DiffDateTime implements Function {
    1.29 +
    1.30 +	@Override
    1.31 +	public String getURI() {
    1.32 +		return "http://strdf.di.uoa.gr/extensions/ontology#diffDateTime";
    1.33 +	}
    1.34 +	
    1.35 +	@Override
    1.36 +	public Value evaluate(ValueFactory valueFactory, Value... args)
    1.37 +            throws ValueExprEvaluationException {
    1.38 +        if (args.length != 2) {
    1.39 +            throw new ValueExprEvaluationException(this.getURI()
    1.40 +                    + " requires exactly 2 arguments, got " + args.length);
    1.41 +        }
    1.42 +        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'hh:mm:ss"); //the format of xsd:Datetime
    1.43 +		
    1.44 +		long diff = 0;
    1.45 +    	try {
    1.46 +    	    String date1 = args[0].toString();
    1.47 +    	    date1 = date1.replace("^^<http://www.w3.org/2001/XMLSchema#dateTime>", "");
    1.48 +    	    date1 = date1.replace("\"", "");
    1.49 +    	    Calendar cal1 = new GregorianCalendar();
    1.50 +    		cal1.setTime(sdf.parse(date1));
    1.51 +    		
    1.52 +    		String date2 = args[1].toString();
    1.53 +    		date2 = date2.replace("^^<http://www.w3.org/2001/XMLSchema#dateTime>", "");
    1.54 +    	    date2 = date2.replace("\"", "");
    1.55 +    	    Calendar cal2 = new GregorianCalendar();
    1.56 +    		cal2.setTime(sdf.parse(date2));
    1.57 +    		
    1.58 +    		diff = cal2.getTimeInMillis() - cal1.getTimeInMillis();
    1.59 +    		
    1.60 +    	} catch (java.text.ParseException e) {
    1.61 +			// TODO Auto-generated catch block
    1.62 +			e.printStackTrace();
    1.63 +		}
    1.64 +		
    1.65 +		
    1.66 +        return valueFactory.createLiteral(diff);
    1.67 +    }
    1.68 +}
     2.1 --- a/evaluation/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function	Mon Dec 10 11:15:42 2012 +0200
     2.2 +++ b/evaluation/src/main/resources/META-INF/services/org.openrdf.query.algebra.evaluation.function.Function	Fri Nov 30 15:07:35 2012 +0200
     2.3 @@ -79,4 +79,6 @@
     2.4  org.openrdf.query.algebra.evaluation.function.spatial.geosparql.sf.SimpleFeaturesIntersectsFunc
     2.5  org.openrdf.query.algebra.evaluation.function.spatial.geosparql.sf.SimpleFeaturesOverlapsFunc
     2.6  org.openrdf.query.algebra.evaluation.function.spatial.geosparql.sf.SimpleFeaturesWithinFunc
     2.7 -org.openrdf.query.algebra.evaluation.function.spatial.geosparql.sf.SimpleFeatureTouchesFunc
     2.8 \ No newline at end of file
     2.9 +org.openrdf.query.algebra.evaluation.function.spatial.geosparql.sf.SimpleFeatureTouchesFunc
    2.10 +
    2.11 +org.openrdf.query.algebra.evaluation.util.DiffDateTime
    2.12 \ No newline at end of file