001/* Output elements of date (year, month, day, hour, etc.) from date. 002 003 @Copyright (c) 2008-2015 The Regents of the University of California. 004 All rights reserved. 005 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the 009 above copyright notice and the following two paragraphs appear in all 010 copies of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 */ 029package ptolemy.actor.lib; 030 031import java.text.SimpleDateFormat; 032 033import ptolemy.actor.TypedAtomicActor; 034import ptolemy.actor.TypedIOPort; 035import ptolemy.data.BooleanToken; 036import ptolemy.data.DateToken; 037import ptolemy.data.IntToken; 038import ptolemy.data.LongToken; 039import ptolemy.data.Token; 040import ptolemy.data.expr.SingletonParameter; 041import ptolemy.data.type.BaseType; 042import ptolemy.kernel.CompositeEntity; 043import ptolemy.kernel.util.IllegalActionException; 044import ptolemy.kernel.util.NameDuplicationException; 045 046/////////////////////////////////////////////////////////////////// 047//// DateElements 048 049/** 050 Output elements of date (year, month, day, hour, etc.) from date. 051 052 @author Patricia Derler 053 @version $Id$ 054 @since Ptolemy II 10.0 055 @Pt.ProposedRating Red (pd) 056 @Pt.AcceptedRating Red (pd) 057 */ 058public class DateElements extends TypedAtomicActor { 059 /** Construct an actor with the given container and name. 060 * @param container The container. 061 * @param name The name of this actor. 062 * @exception IllegalActionException If the actor cannot be contained 063 * by the proposed container. 064 * @exception NameDuplicationException If the container already has an 065 * actor with this name. 066 */ 067 public DateElements(CompositeEntity container, String name) 068 throws NameDuplicationException, IllegalActionException { 069 super(container, name); 070 071 input = new TypedIOPort(this, "input", true, false); 072 input.setTypeEquals(BaseType.DATE); 073 074 year = new TypedIOPort(this, "year", false, true); 075 year.setTypeEquals(BaseType.INT); 076 new SingletonParameter(year, "_showName").setToken(BooleanToken.TRUE); 077 078 month = new TypedIOPort(this, "month", false, true); 079 month.setTypeEquals(BaseType.INT); 080 new SingletonParameter(month, "_showName").setToken(BooleanToken.TRUE); 081 082 day = new TypedIOPort(this, "day", false, true); 083 day.setTypeEquals(BaseType.INT); 084 new SingletonParameter(day, "_showName").setToken(BooleanToken.TRUE); 085 086 hour = new TypedIOPort(this, "hour", false, true); 087 hour.setTypeEquals(BaseType.INT); 088 new SingletonParameter(hour, "_showName").setToken(BooleanToken.TRUE); 089 090 minute = new TypedIOPort(this, "minute", false, true); 091 minute.setTypeEquals(BaseType.INT); 092 new SingletonParameter(minute, "_showName").setToken(BooleanToken.TRUE); 093 094 second = new TypedIOPort(this, "second", false, true); 095 second.setTypeEquals(BaseType.INT); 096 new SingletonParameter(second, "_showName").setToken(BooleanToken.TRUE); 097 098 millisecond = new TypedIOPort(this, "millisecond", false, true); 099 millisecond.setTypeEquals(BaseType.INT); 100 new SingletonParameter(millisecond, "_showName") 101 .setToken(BooleanToken.TRUE); 102 103 microsecond = new TypedIOPort(this, "microsecond", false, true); 104 microsecond.setTypeEquals(BaseType.INT); 105 new SingletonParameter(microsecond, "_showName") 106 .setToken(BooleanToken.TRUE); 107 108 nanosecond = new TypedIOPort(this, "nanosecond", false, true); 109 nanosecond.setTypeEquals(BaseType.INT); 110 new SingletonParameter(nanosecond, "_showName") 111 .setToken(BooleanToken.TRUE); 112 113 timeZoneOffset = new TypedIOPort(this, "timeZoneOffset", false, true); 114 timeZoneOffset.setTypeEquals(BaseType.INT); 115 new SingletonParameter(timeZoneOffset, "_showName") 116 .setToken(BooleanToken.TRUE); 117 118 timeInMillis = new TypedIOPort(this, "timeInMillis", false, true); 119 timeInMillis.setTypeEquals(BaseType.LONG); 120 new SingletonParameter(timeInMillis, "_showName") 121 .setToken(BooleanToken.TRUE); 122 } 123 124 /** Input for date token. 125 */ 126 public TypedIOPort input; 127 128 /** Year of date received on input. 129 */ 130 public TypedIOPort year; 131 132 /** Month of date received on input. 133 */ 134 public TypedIOPort month; 135 136 /** Day of the month of date received on input. 137 */ 138 public TypedIOPort day; 139 140 /** Hour of date received on input. 141 */ 142 public TypedIOPort hour; 143 144 /** Minute of date received on input. 145 */ 146 public TypedIOPort minute; 147 148 /** Second of date received on input. 149 */ 150 public TypedIOPort second; 151 152 /** Millisecond of date received on input. 153 */ 154 public TypedIOPort millisecond; 155 156 /** Microsecond of date received on input. 157 */ 158 public TypedIOPort microsecond; 159 160 /** Nanosecond of date received on input. 161 */ 162 public TypedIOPort nanosecond; 163 164 /** Time in UTC milliseconds since epoch. 165 */ 166 public TypedIOPort timeInMillis; 167 168 /** Time zone offset of date received on input. 169 */ 170 public TypedIOPort timeZoneOffset; 171 172 /////////////////////////////////////////////////////////////////// 173 //// public methods //// 174 175 /** Output all elements of a date if input has a date token. 176 * @exception IllegalActionException If there is no director. 177 */ 178 @Override 179 public void fire() throws IllegalActionException { 180 super.fire(); 181 if (input.hasToken(0)) { 182 Token token = input.get(0); 183 if (token instanceof DateToken) { 184 DateToken dateToken = (DateToken) token; 185 year.send(0, new IntToken(dateToken.getYear())); 186 month.send(0, new IntToken(dateToken.getMonth())); 187 day.send(0, new IntToken(dateToken.getDay())); 188 hour.send(0, new IntToken(dateToken.getHour())); 189 minute.send(0, new IntToken(dateToken.getMinute())); 190 second.send(0, new IntToken(dateToken.getSecond())); 191 millisecond.send(0, new IntToken(dateToken.getMillisecond())); 192 microsecond.send(0, new IntToken(dateToken.getMicrosecond())); 193 nanosecond.send(0, new IntToken(dateToken.getNanosecond())); 194 SimpleDateFormat sdf = new SimpleDateFormat("ZZZZZ"); 195 sdf.setTimeZone(dateToken.getTimeZone()); 196 timeZoneOffset.send(0, new IntToken(Integer.parseInt(sdf 197 .format(dateToken.getCalendarInstance().getTime())))); 198 timeInMillis.send(0, new LongToken( 199 dateToken.getCalendarInstance().getTimeInMillis())); 200 } 201 } 202 } 203}