001/* 002 * Copyright (c) 2004-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: welker $' 006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 007 * '$Revision: 24234 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.sdm.spa; 031 032import java.text.SimpleDateFormat; 033import java.util.Date; 034 035import ptolemy.actor.lib.Source; 036import ptolemy.data.StringToken; 037import ptolemy.data.expr.StringParameter; 038import ptolemy.data.type.BaseType; 039import ptolemy.kernel.CompositeEntity; 040import ptolemy.kernel.util.IllegalActionException; 041import ptolemy.kernel.util.NameDuplicationException; 042 043////////////////////////////////////////////////////////////////////////// 044//// Timestamp 045/** 046 * Returns the current date and time in "yyyy-MM-dd z HH:mm:ss" format. 047 * 048 * @author Ilkay Altintas 049 * @version $Id: Timestamp.java 24234 2010-05-06 05:21:26Z welker $ 050 * 051 * Trigger: Any token received along this port will cause the actor to 052 * fire. 053 * 054 * Output: The current timestamp is output through this port. 055 */ 056 057public class Timestamp extends Source { 058 059 /** 060 * Construct a constant source with the given container and name. Create the 061 * <i>value</i> parameter, initialize its value to the default value of an 062 * IntToken with value 1. 063 * 064 * @param container 065 * The container. 066 * @param name 067 * The name of this actor. 068 * @exception IllegalActionException 069 * If the entity cannot be contained by the proposed 070 * container. 071 * @exception NameDuplicationException 072 * If the container already has an actor with this name. 073 */ 074 public Timestamp(CompositeEntity container, String name) 075 throws NameDuplicationException, IllegalActionException { 076 super(container, name); 077 078 // Set the type constraint. 079 output.setTypeEquals(BaseType.STRING); 080 081 format = new StringParameter(this, "format"); 082 format.setExpression("yyyy-MM-dd z HH:mm:ss"); 083 format.addChoice("yyyy-MM-dd z HH:mm:ss"); 084 format.addChoice("EEE, d MMM yyyy HH:mm:ss Z"); 085 format.addChoice("yyyyMMddHHmmssS"); 086 087 _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" " 088 + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n" 089 + "</svg>\n"); 090 } 091 092 public StringParameter format; 093 094 // ///////////////////////////////////////////////////////////////// 095 // // public methods //// 096 /** 097 * Send the token in the <i>value</i> parameter to the output. 098 * 099 * @exception IllegalActionException 100 * If it is thrown by the send() method sending out the 101 * token. 102 */ 103 public void fire() throws IllegalActionException { 104 super.fire(); 105 Date currentDatetime = new Date(System.currentTimeMillis()); 106 String dateFormat = format.getExpression(); 107 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); 108 String myDate = formatter.format(currentDatetime); 109 output.send(0, new StringToken(myDate)); 110 } 111 112 /** 113 * Post fire the actor. Return false to indicated that the process has 114 * finished. If it returns true, the process will continue indefinitely. 115 */ 116 public boolean postfire() { 117 return true; 118 } 119}