001/* 002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved. 003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved. 004 * 005 * Project and contact information: http://www.cascading.org/ 006 * 007 * This file is part of the Cascading project. 008 * 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 */ 021 022package cascading.operation.text; 023 024import java.beans.ConstructorProperties; 025import java.text.SimpleDateFormat; 026import java.util.Calendar; 027import java.util.Locale; 028import java.util.TimeZone; 029 030import cascading.flow.FlowProcess; 031import cascading.operation.Function; 032import cascading.operation.FunctionCall; 033import cascading.tuple.Fields; 034import cascading.tuple.TupleEntry; 035import cascading.util.Pair; 036 037/** 038 * Class DateFormatter is used to convert a date timestamp to a formatted string, where a timestamp 039 * is the number of milliseconds since January 1, 1970, 00:00:00 GMT, using the {@link SimpleDateFormat} syntax. 040 * <p> 041 * Note the timezone data is given to the SimpleDateFormat, not the internal Calendar instance which interprets 042 * the 'timestamp' value as it is assumed the timestamp is already in GMT. 043 */ 044public class DateFormatter extends DateOperation implements Function<Pair<SimpleDateFormat, TupleEntry>> 045 { 046 /** Field FIELD_NAME */ 047 public static final String FIELD_NAME = "datetime"; 048 049 /** 050 * Constructor DateParser creates a new DateParser instance that creates a simple long time stamp of the parsed date. 051 * 052 * @param dateFormatString of type String 053 */ 054 @ConstructorProperties({"dateFormatString"}) 055 public DateFormatter( String dateFormatString ) 056 { 057 super( 1, new Fields( FIELD_NAME ), dateFormatString ); 058 } 059 060 /** 061 * Constructor DateParser creates a new DateParser instance. 062 * 063 * @param fieldDeclaration of type Fields 064 * @param dateFormatString of type String 065 */ 066 @ConstructorProperties({"fieldDeclaration", "dateFormatString"}) 067 public DateFormatter( Fields fieldDeclaration, String dateFormatString ) 068 { 069 super( 1, fieldDeclaration, dateFormatString ); 070 } 071 072 /** 073 * Constructor DateFormatter creates a new DateFormatter instance. 074 * 075 * @param fieldDeclaration of type Fields 076 * @param dateFormatString of type String 077 * @param zone of type TimeZone 078 */ 079 @ConstructorProperties({"fieldDeclaration", "dateFormatString", "zone"}) 080 public DateFormatter( Fields fieldDeclaration, String dateFormatString, TimeZone zone ) 081 { 082 super( 1, fieldDeclaration, dateFormatString, zone, null ); 083 } 084 085 /** 086 * Constructor DateFormatter creates a new DateFormatter instance. 087 * 088 * @param fieldDeclaration of type Fields 089 * @param dateFormatString of type String 090 * @param zone of type TimeZone 091 * @param locale of type Locale 092 */ 093 @ConstructorProperties({"fieldDeclaration", "dateFormatString", "zone", "locale"}) 094 public DateFormatter( Fields fieldDeclaration, String dateFormatString, TimeZone zone, Locale locale ) 095 { 096 super( 1, fieldDeclaration, dateFormatString, zone, locale ); 097 } 098 099 @Override 100 public void operate( FlowProcess flowProcess, FunctionCall<Pair<SimpleDateFormat, TupleEntry>> functionCall ) 101 { 102 long ts = functionCall.getArguments().getLong( 0 ); 103 104 Calendar calendar = getCalendar(); 105 106 calendar.setTimeInMillis( ts ); 107 108 String formatted = functionCall.getContext().getLhs().format( calendar.getTime() ); 109 110 functionCall.getContext().getRhs().setString( 0, formatted ); 111 112 functionCall.getOutputCollector().add( functionCall.getContext().getRhs() ); 113 } 114 }