You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ga...@apache.org on 2010/03/31 00:53:21 UTC

svn commit: r929330 [1/2] - in /hadoop/pig/branches/branch-0.7/contrib: ./ piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/ piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ piggybank/java...

Author: gates
Date: Tue Mar 30 22:53:20 2010
New Revision: 929330

URL: http://svn.apache.org/viewvc?rev=929330&view=rev
Log:
PIG-1310 ISO Date UDFs: Conversion, Trucation and Date Math (piggybank)

Added:
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/CustomFormatToISO.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/ISOToUnix.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/UnixToISO.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISODaysBetween.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOHoursBetween.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMinutesBetween.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMonthsBetween.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOSecondsBetween.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOYearsBetween.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToDay.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToHour.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMinute.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMonth.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToSecond.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToWeek.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToYear.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/datetime/convert/
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/datetime/convert/TestConvertDateTime.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/datetime/diff/
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/datetime/diff/TestDiffDateTime.java
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/datetime/truncate/
    hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/datetime/truncate/TestTruncateDateTime.java
Modified:
    hadoop/pig/branches/branch-0.7/contrib/CHANGES.txt

Modified: hadoop/pig/branches/branch-0.7/contrib/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/CHANGES.txt?rev=929330&r1=929329&r2=929330&view=diff
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/CHANGES.txt (original)
+++ hadoop/pig/branches/branch-0.7/contrib/CHANGES.txt Tue Mar 30 22:53:20 2010
@@ -24,6 +24,21 @@ INCOMPATIBLE CHANGES
 
 IMPROVEMENTS
 
+OPTIMIZATIONS
+
+BUG FIXES
+
+
+PIG 0.7.0
+
+INCOMPATIBLE CHANGES
+
+IMPROVEMENTS
+
+PIG-1310 
+ISO Date UDFs: Conversion, Trucation and Date Math (piggybank) (rjurney via
+gates)
+
 PIG-1284 Added XMLLoader to piggybank (aloknsingh via gates)
 
 OPTIMIZATIONS

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/CustomFormatToISO.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/CustomFormatToISO.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/CustomFormatToISO.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/CustomFormatToISO.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.convert;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * CustomFormatToISO converts arbitrary date formats to ISO format.
+ * 
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ * Jodatime custom date formats: http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html
+ *
+ * Example usage:
+ *
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE CustomFormatToISO org.apache.pig.piggybank.evaluation.datetime.convert.CustomFormatToISO();
+ * CustomIn = LOAD 'test3.tsv' USING PigStorage('\t') AS (dt:chararray);
+ *
+ * DESCRIBE CustomIn;
+ * CustomIn: {dt: chararray}
+ *
+ * DUMP CustomIn;
+ *
+ * (10-1-2010)
+ *
+ * toISO = FOREACH CustomIn GENERATE CustomFormatToISO(dt, "MM-dd-YYYY") AS ISOTime:chararray;
+ *
+ * DESCRIBE toISO;
+ * toISO: {ISOTime: chararray}
+ *
+ * DUMP toISO;
+ * (2010-10-01T00:00:00.000Z)
+ * ...
+ *
+ */
+
+public class CustomFormatToISO extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        String date = input.get(0).toString();
+        String format = input.get(1).toString();
+
+        // See http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormat.html
+        DateTimeFormatter parser = DateTimeFormat.forPattern(format);
+        DateTime result = parser.parseDateTime(date);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/ISOToUnix.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/ISOToUnix.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/ISOToUnix.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/ISOToUnix.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.convert;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOToUnix converts ISO8601 datetime strings to Unix Time Longs
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ * Unix Time: http://en.wikipedia.org/wiki/Unix_time
+ *
+ * Example usage:
+ *
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToUnix org.apache.pig.piggybank.evaluation.datetime.convert.ISOToUnix();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * toUnix = FOREACH ISOin GENERATE ISOToUnix(dt) AS unixTime:long;
+ *
+ * DESCRIBE toUnix;
+ * toUnix: {unixTime: long}
+ *
+ * DUMP toUnix;
+ *
+ * (1231290421000L)
+ * (1202263562000L)
+ * (1173063903000L)
+ * ...
+ *
+ */
+
+public class ISOToUnix extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime result = new DateTime(input.get(0).toString());
+
+        return result.getMillis();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/UnixToISO.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/UnixToISO.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/UnixToISO.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/convert/UnixToISO.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.convert;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * UnixToISO converts Unix Time Long datetimes to ISO8601 datetime strings
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ * Unix Time: http://en.wikipedia.org/wiki/Unix_time
+ *
+ * Example usage:
+ *
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ * 
+ * DEFINE UnixToISO org.apache.pig.piggybank.evaluation.datetime.convert.UnixToISO();
+ *
+ * UnixIn = LOAD 'test2.tsv' USING PigStorage('\t') AS (dt:long);
+ *
+ * DESCRIBE UnixIn;
+ * UnixIn: {dt: long}
+ *
+ * DUMP UnixIn;
+ *
+ * (1231290421000L)
+ * (1233885962000L)
+ * (1236222303000L)
+ *
+ * toISO = FOREACH UnixIn GENERATE UnixToISO(dt) AS ISOTime:chararray;
+ *
+ * DESCRIBE toISO;
+ * toISO: {ISOTime: chararray}
+ *
+ * DUMP toISO;
+ *
+ * (2009-01-07T01:07:01.000Z)
+ * (2009-02-06T02:06:02.000Z)
+ * (2009-03-05T03:05:03.000Z)
+ * ...
+ *
+ */
+
+public class UnixToISO extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime result = new DateTime(DataType.toLong(input.get(0)));
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.LONG))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISODaysBetween.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISODaysBetween.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISODaysBetween.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISODaysBetween.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.diff;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.Days;
+import org.joda.time.ReadableInstant;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISODaysBetween returns the number of days between two ISO8601 datetimes as a Long
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ *
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOYearsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOYearsBetween();
+ * DEFINE ISOMonthsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMonthsBetween();
+ * DEFINE ISODaysBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISODaysBetween();
+ * DEFINE ISOHoursBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOHoursBetween();
+ * DEFINE ISOMinutesBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMinutesBetween();
+ * DEFINE ISOSecondsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOSecondsBetween();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * diffs = FOREACH ISOin GENERATE ISOYearsBetween(dt, dt2) AS years,
+ *  ISOMonthsBetween(dt, dt2) AS months,
+ *  ISODaysBetween(dt, dt2) AS days,
+ * 	ISOHoursBetween(dt, dt2) AS hours,
+ * 	ISOMinutesBetween(dt, dt2) AS mins,
+ * 	ISOSecondsBetween(dt, dt2) AS secs;
+ *
+ * DESCRIBE diffs;
+ * diffs: {years: long,months: long,days: long,hours: long,mins: long,secs: long}
+ *
+ * DUMP diffs;
+ *
+ * (0L,11L,341,8185L,491107L,29466421L)
+ * (0L,0L,5,122L,7326L,439562L)
+ * (0L,-10L,-332,-7988L,-479334L,-28760097L)
+ *
+ */
+
+public class ISODaysBetween extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime startDate = new DateTime(input.get(0).toString());
+        DateTime endDate = new DateTime(input.get(1).toString());
+
+        // Larger date first
+        Days d = Days.daysBetween(endDate, startDate);
+        long days = d.getDays();
+
+        return days;
+
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOHoursBetween.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOHoursBetween.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOHoursBetween.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOHoursBetween.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.diff;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.*;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOHoursBetween returns the number of hours between two ISO8601 datetimes as a Long
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOYearsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOYearsBetween();
+ * DEFINE ISOMonthsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMonthsBetween();
+ * DEFINE ISODaysBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISODaysBetween();
+ * DEFINE ISOHoursBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOHoursBetween();
+ * DEFINE ISOMinutesBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMinutesBetween();
+ * DEFINE ISOSecondsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOSecondsBetween();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * diffs = FOREACH ISOin GENERATE ISOYearsBetween(dt, dt2) AS years,
+ *     ISOMonthsBetween(dt, dt2) AS months,
+ *     ISODaysBetween(dt, dt2) AS days,
+ * 	   ISOHoursBetween(dt, dt2) AS hours,
+ * 	   ISOMinutesBetween(dt, dt2) AS mins,
+ * 	   ISOSecondsBetween(dt, dt2) AS secs;
+ *
+ * DESCRIBE diffs;
+ * diffs: {years: long,months: long,days: long,hours: long,mins: long,secs: long}
+ *
+ * DUMP diffs;
+ *
+ * (0L,11L,341,8185L,491107L,29466421L)
+ * (0L,0L,5,122L,7326L,439562L)
+ * (0L,-10L,-332,-7988L,-479334L,-28760097L)
+ *
+ */
+
+public class ISOHoursBetween extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime startDate = new DateTime(input.get(0).toString());
+        DateTime endDate = new DateTime(input.get(1).toString());
+
+        // Larger date first
+        Hours h = Hours.hoursBetween(endDate, startDate);
+        long hours = h.getHours();
+
+        return hours;
+
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMinutesBetween.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMinutesBetween.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMinutesBetween.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMinutesBetween.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.diff;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.*;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOMinutesBetween returns the number of minutes between two ISO8601 datetimes as a Long
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOYearsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOYearsBetween();
+ * DEFINE ISOMonthsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMonthsBetween();
+ * DEFINE ISODaysBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISODaysBetween();
+ * DEFINE ISOHoursBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOHoursBetween();
+ * DEFINE ISOMinutesBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMinutesBetween();
+ * DEFINE ISOSecondsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOSecondsBetween();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * diffs = FOREACH ISOin GENERATE ISOYearsBetween(dt, dt2) AS years,
+ *     ISOMonthsBetween(dt, dt2) AS months,
+ *     ISODaysBetween(dt, dt2) AS days,
+ *     ISOHoursBetween(dt, dt2) AS hours,
+ * 	   ISOMinutesBetween(dt, dt2) AS mins,
+ * 	   ISOSecondsBetween(dt, dt2) AS secs;
+ *
+ * DESCRIBE diffs;
+ * diffs: {years: long,months: long,days: long,hours: long,mins: long,secs: long}
+ *
+ * DUMP diffs;
+ *
+ * (0L,11L,341,8185L,491107L,29466421L)
+ * (0L,0L,5,122L,7326L,439562L)
+ * (0L,-10L,-332,-7988L,-479334L,-28760097L)
+ *
+ */
+
+public class ISOMinutesBetween extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime startDate = new DateTime(input.get(0).toString());
+        DateTime endDate = new DateTime(input.get(1).toString());
+
+        // Larger date first
+        Minutes m = Minutes.minutesBetween(endDate, startDate);
+        long minutes = m.getMinutes();
+
+        return minutes;
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMonthsBetween.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMonthsBetween.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMonthsBetween.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOMonthsBetween.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.diff;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.Months;
+import org.joda.time.ReadableInstant;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOMonthsBetween returns the number of months between two ISO8601 datetimes as a Long
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOYearsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOYearsBetween();
+ * DEFINE ISOMonthsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMonthsBetween();
+ * DEFINE ISODaysBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISODaysBetween();
+ * DEFINE ISOHoursBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOHoursBetween();
+ * DEFINE ISOMinutesBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMinutesBetween();
+ * DEFINE ISOSecondsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOSecondsBetween();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * diffs = FOREACH ISOin GENERATE ISOYearsBetween(dt, dt2) AS years,
+ *     ISOMonthsBetween(dt, dt2) AS months,
+ *     ISODaysBetween(dt, dt2) AS days,
+ *     ISOHoursBetween(dt, dt2) AS hours,
+ * 	   ISOMinutesBetween(dt, dt2) AS mins,
+ * 	   ISOSecondsBetween(dt, dt2) AS secs;
+ *
+ * DESCRIBE diffs;
+ * diffs: {years: long,months: long,days: long,hours: long,mins: long,secs: long}
+ *
+ * DUMP diffs;
+ *
+ * (0L,11L,341,8185L,491107L,29466421L)
+ * (0L,0L,5,122L,7326L,439562L)
+ * (0L,-10L,-332,-7988L,-479334L,-28760097L)
+ *
+ */
+
+public class ISOMonthsBetween extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime startDate = new DateTime(input.get(0).toString());
+        DateTime endDate = new DateTime(input.get(1).toString());
+
+        // Larger value first
+        Months m = Months.monthsBetween(endDate, startDate);
+        long months = m.getMonths();
+
+        return months;
+
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOSecondsBetween.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOSecondsBetween.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOSecondsBetween.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOSecondsBetween.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.diff;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.*;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOSecondsBetween returns the number of seconds between two ISO8601 datetimes as a Long
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOYearsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOYearsBetween();
+ * DEFINE ISOMonthsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMonthsBetween();
+ * DEFINE ISODaysBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISODaysBetween();
+ * DEFINE ISOHoursBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOHoursBetween();
+ * DEFINE ISOMinutesBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMinutesBetween();
+ * DEFINE ISOSecondsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOSecondsBetween();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * diffs = FOREACH ISOin GENERATE ISOYearsBetween(dt, dt2) AS years,
+ *     ISOMonthsBetween(dt, dt2) AS months,
+ *     ISODaysBetween(dt, dt2) AS days,
+ * 	   ISOHoursBetween(dt, dt2) AS hours,
+ * 	   ISOMinutesBetween(dt, dt2) AS mins,
+ * 	   ISOSecondsBetween(dt, dt2) AS secs;
+ *
+ * DESCRIBE diffs;
+ * diffs: {years: long,months: long,days: long,hours: long,mins: long,secs: long}
+ *
+ * DUMP diffs;
+ *
+ * (0L,11L,341,8185L,491107L,29466421L)
+ * (0L,0L,5,122L,7326L,439562L)
+ * (0L,-10L,-332,-7988L,-479334L,-28760097L)
+ *
+ */
+
+public class ISOSecondsBetween extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime startDate = new DateTime(input.get(0).toString());
+        DateTime endDate = new DateTime(input.get(1).toString());
+
+        // Larger date first
+        Seconds s = Seconds.secondsBetween(endDate, startDate);
+        long seconds = s.getSeconds();
+
+        return seconds;
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOYearsBetween.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOYearsBetween.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOYearsBetween.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/diff/ISOYearsBetween.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.diff;
+
+/**
+ * ISOYearsBetween returns the number of years between two ISO8601 datetimes as a Long
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOYearsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOYearsBetween();
+ * DEFINE ISOMonthsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMonthsBetween();
+ * DEFINE ISODaysBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISODaysBetween();
+ * DEFINE ISOHoursBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOHoursBetween();
+ * DEFINE ISOMinutesBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOMinutesBetween();
+ * DEFINE ISOSecondsBetween org.apache.pig.piggybank.evaluation.datetime.diff.ISOSecondsBetween();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * diffs = FOREACH ISOin GENERATE ISOYearsBetween(dt, dt2) AS years,
+ *     ISOMonthsBetween(dt, dt2) AS months,
+ *     ISODaysBetween(dt, dt2) AS days,
+ * 	   ISOHoursBetween(dt, dt2) AS hours,
+ * 	   ISOMinutesBetween(dt, dt2) AS mins,
+ * 	   ISOSecondsBetween(dt, dt2) AS secs;
+ *
+ * DESCRIBE diffs;
+ * diffs: {years: long,months: long,days: long,hours: long,mins: long,secs: long}
+ *
+ * DUMP diffs;
+ *
+ * (0L,11L,341,8185L,491107L,29466421L)
+ * (0L,0L,5,122L,7326L,439562L)
+ * (0L,-10L,-332,-7988L,-479334L,-28760097L)
+ *
+ */
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.*;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ISOYearsBetween extends EvalFunc<Long> {
+
+    @Override
+    public Long exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 2) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime startDate = new DateTime(input.get(0).toString());
+        DateTime endDate = new DateTime(input.get(1).toString());
+
+        // Larger value first
+        Years y = Years.yearsBetween(endDate, startDate);
+        long years = y.getYears();
+
+        return years;
+
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        Schema s = new Schema();
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+        funcList.add(new FuncSpec(this.getClass().getName(), s));
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToDay.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToDay.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToDay.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToDay.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOToDay truncates an ISO8601 datetime string to the precision of the day field
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ * 
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ */
+
+public class ISOToDay extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the the hour, minute, second and milliseconds to 0
+        DateTime result = dt.hourOfDay().setCopy(0).minuteOfHour().setCopy(0).secondOfMinute().setCopy(0).millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToHour.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToHour.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToHour.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToHour.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.*;
+
+/**
+ * ISOToHour truncates an ISO8601 datetime string to the precision of the hour field
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ */
+
+public class ISOToHour extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the minute, second and milliseconds to 0
+        DateTime result = dt.minuteOfHour().setCopy(0).secondOfMinute().setCopy(0).millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMinute.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMinute.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMinute.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMinute.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOToMinute truncates an ISO8601 datetime string to the precision of the minute field
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ */
+
+public class ISOToMinute extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the the second and milliseconds to 0
+        DateTime result = dt.secondOfMinute().setCopy(0).millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMonth.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMonth.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMonth.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToMonth.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOToMonth truncates an ISO8601 datetime string to the precision of the month field
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ *
+ */
+
+public class ISOToMonth extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the day to 1 and the hour, minute, second and milliseconds to 0
+        DateTime result = dt.dayOfMonth().setCopy(1).hourOfDay().setCopy(0).minuteOfHour().setCopy(0).secondOfMinute().setCopy(0).millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToSecond.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToSecond.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToSecond.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToSecond.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOToSecond truncates an ISO8601 datetime string to the precision of the second field
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ *
+ */
+
+public class ISOToSecond extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the the second and milliseconds to 0
+        DateTime result = dt.millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToWeek.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToWeek.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToWeek.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToWeek.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * ISOToWeek truncates an ISO8601 datetime string to the precision of the day field,
+ * for the first day of the week of the datetime.  This 'rounds' to the week's monday, see
+ * http://joda-time.sourceforge.net/cal_iso.html
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ *
+ */
+
+public class ISOToWeek extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the the day to 1, and the hour, minute, second and milliseconds to 0
+        DateTime result = dt.dayOfWeek().setCopy(1).hourOfDay().setCopy(0).minuteOfHour().setCopy(0).secondOfMinute().setCopy(0).millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}

Added: hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToYear.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToYear.java?rev=929330&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToYear.java (added)
+++ hadoop/pig/branches/branch-0.7/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/datetime/truncate/ISOToYear.java Tue Mar 30 22:53:20 2010
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pig.piggybank.evaluation.datetime.truncate;
+
+/**
+ * ISOToYear truncates an ISO8601 datetime string to the precision of the year field
+ *
+ * Jodatime: http://joda-time.sourceforge.net/
+ * ISO8601 Date Format: http://en.wikipedia.org/wiki/ISO_8601
+ *
+ * Example usage:
+ * 
+ * REGISTER /Users/me/commiter/piggybank/java/piggybank.jar ;
+ * REGISTER /Users/me/commiter/piggybank/java/lib/joda-time-1.6.jar ;
+ *
+ * DEFINE ISOToYear org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToYear();
+ * DEFINE ISOToMonth org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMonth();
+ * DEFINE ISOToWeek org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToWeek();
+ * DEFINE ISOToDay org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToDay();
+ * DEFINE ISOToHour org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToHour();
+ * DEFINE ISOToMinute org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToMinute();
+ * DEFINE ISOToSecond org.apache.pig.piggybank.evaluation.datetime.truncate.ISOToSecond();
+ *
+ * ISOin = LOAD 'test.tsv' USING PigStorage('\t') AS (dt:chararray, dt2:chararray);
+ *
+ * DESCRIBE ISOin;
+ * ISOin: {dt: chararray,dt2: chararray}
+ *
+ * DUMP ISOin;
+ *
+ * (2009-01-07T01:07:01.000Z,2008-02-01T00:00:00.000Z)
+ * (2008-02-06T02:06:02.000Z,2008-02-01T00:00:00.000Z)
+ * (2007-03-05T03:05:03.000Z,2008-02-01T00:00:00.000Z)
+ * ...
+ *
+ * truncated = FOREACH ISOin GENERATE ISOToYear(dt) AS year,
+ *     ISOToMonth(dt) as month,
+ *     ISOToWeek(dt) as week,
+ *     ISOToDay(dt) AS day,
+ * 	   ISOToHour(dt) AS hour,
+ * 	   ISOToMinute(dt) AS min,
+ * 	   ISOToSecond(dt) as sec;
+ *
+ * DESCRIBE truncated;
+ * truncated: {year: chararray,month: chararray,week: chararray,day: chararray,hour: chararray,min: chararray,sec: chararray}
+ *
+ * DUMP truncated;
+ * (2009-01-01T00:00:00.000Z,2009-01-01T00:00:00.000Z,2009-01-05T00:00:00.000Z,2009-01-07T00:00:00.000Z,2009-01-07T01:00:00.000Z,2009-01-07T01:07:00.000Z,2009-01-07T01:07:01.000Z)
+ * (2008-01-01T00:00:00.000Z,2008-02-01T00:00:00.000Z,2008-02-04T00:00:00.000Z,2008-02-06T00:00:00.000Z,2008-02-06T02:00:00.000Z,2008-02-06T02:06:00.000Z,2008-02-06T02:06:02.000Z)
+ * (2007-01-01T00:00:00.000Z,2007-03-01T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T00:00:00.000Z,2007-03-05T03:00:00.000Z,2007-03-05T03:05:00.000Z,2007-03-05T03:05:03.000Z)
+ *
+ */
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ISOToYear extends EvalFunc<String> {
+
+    @Override
+    public String exec(Tuple input) throws IOException
+    {
+        if (input == null || input.size() < 1) {
+            return null;
+        }
+        
+        // Set the time to default or the output is in UTC
+        DateTimeZone.setDefault(DateTimeZone.UTC);
+
+        DateTime dt = new DateTime((String)input.get(0).toString());
+
+        // Set the month and day to 1 and the hour, minute, second and milliseconds to 0
+        DateTime result = dt.monthOfYear().setCopy(1).dayOfMonth().setCopy(1).hourOfDay().setCopy(0).minuteOfHour().setCopy(0).secondOfMinute().setCopy(0).millisOfSecond().setCopy(0);
+
+        return result.toString();
+    }
+
+	@Override
+	public Schema outputSchema(Schema input) {
+        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+	}
+
+    @Override
+    public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+        List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+        funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY))));
+
+        return funcList;
+    }
+}
+