001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.extensions;
021    
022    import java.text.SimpleDateFormat;
023    import java.util.Calendar;
024    import java.util.List;
025    
026    import org.mactor.framework.MactorException;
027    import org.mactor.framework.TestContext;
028    import org.mactor.framework.extensioninterface.ValueCommand;
029    
030    /**
031     * Extracts the current time on the format: 'yyyy-MM-dd'T'HH:mm:ss.SSSZ' <br>
032     * A single parameter can be provided to specify the number of minutes to add to
033     * or substract from the current time (5 adds five minuts -120 subtracts to
034     * hours )
035     * 
036     * @author Lars Ivar Almli
037     */
038    public class CurrentTimeExtractor implements ValueCommand {
039            public String extractValue(TestContext context, List<String> params) throws MactorException {
040                    Calendar cal = Calendar.getInstance();
041                    if (params != null && params.size() > 0 && params.get(0) != null && params.get(0).length() > 0) {
042                            try {
043                                    String v = params.get(0);
044                                    int minutes = Integer.parseInt(v);
045                                    cal.add(Calendar.MINUTE, minutes);
046                            } catch (NumberFormatException nfe) {
047                            }
048                    }
049                    String s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(cal.getTime());
050                    if (s.charAt(s.length() - 2) != ':') // Bug in SimpleDateFormat?
051                            s = s.substring(0, s.length() - 2) + ":" + s.substring(s.length() - 2);
052                    return s;
053            }
054    }