001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.time.packet;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.packet.Packet;
021import org.jivesoftware.smack.util.XmppDateTime;
022
023import java.util.Calendar;
024import java.util.Date;
025import java.util.logging.Level;
026import java.util.logging.Logger;
027
028/**
029 * A Time IQ packet, which is used by XMPP clients to exchange their respective local
030 * times. Clients that wish to fully support the entity time protocol should register
031 * a PacketListener for incoming time requests that then respond with the local time.
032 *
033 * @see <a href="http://www.xmpp.org/extensions/xep-0202.html">XEP-202</a>
034 * @author Florian Schmaus
035 */
036public class Time extends IQ {
037    public static final String NAMESPACE = "urn:xmpp:time";
038    public static final String ELEMENT = "time";
039
040    private static final Logger LOGGER = Logger.getLogger(Time.class.getName());
041
042    private String utc;
043    private String tzo;
044
045    public Time() {
046        setType(Type.GET);
047    }
048
049    /**
050     * Creates a new Time instance using the specified calendar instance as
051     * the time value to send.
052     *
053     * @param cal the time value.
054     */
055    public Time(Calendar cal) {
056        tzo = XmppDateTime.asString(cal.getTimeZone());
057        // Convert local time to the UTC time.
058        utc = XmppDateTime.formatXEP0082Date(cal.getTime());
059    }
060
061    /**
062     * Returns the local time or <tt>null</tt> if the time hasn't been set.
063     *
064     * @return the local time.
065     */
066    public Date getTime() {
067        if (utc == null) {
068            return null;
069        }
070        Date date = null;
071        try {
072            date = XmppDateTime.parseDate(utc);
073        }
074        catch (Exception e) {
075            LOGGER.log(Level.SEVERE, "Error getting local time", e);
076        }
077        return date;
078    }
079
080    /**
081     * Sets the time using the local time.
082     *
083     * @param time the current local time.
084     */
085    public void setTime(Date time) {
086    }
087
088    /**
089     * Returns the time as a UTC formatted String using the format CCYY-MM-DDThh:mm:ssZ.
090     *
091     * @return the time as a UTC formatted String.
092     */
093    public String getUtc() {
094        return utc;
095    }
096
097    /**
098     * Sets the time using UTC formatted String in the format CCYY-MM-DDThh:mm:ssZ.
099     *
100     * @param utc the time using a formatted String.
101     */
102    public void setUtc(String utc) {
103        this.utc = utc;
104    }
105
106    /**
107     * Returns the time zone.
108     *
109     * @return the time zone.
110     */
111    public String getTzo() {
112        return tzo;
113    }
114
115    /**
116     * Sets the time zone offset.
117     *
118     * @param tzo the time zone offset.
119     */
120    public void setTzo(String tzo) {
121        this.tzo = tzo;
122    }
123
124    public static Time createResponse(Packet request) {
125        Time time = new Time(Calendar.getInstance());
126        time.setType(Type.RESULT);
127        time.setTo(request.getFrom());
128        return time;
129    }
130
131    public String getChildElementXML() {
132        StringBuilder buf = new StringBuilder();
133        buf.append("<" + ELEMENT +  " xmlns='" + NAMESPACE + "'>");
134        if (utc != null) {
135            buf.append("<utc>").append(utc).append("</utc>");
136            buf.append("<tzo>").append(tzo).append("</tzo>");
137        }
138        buf.append("</" + ELEMENT + ">");
139        return buf.toString();
140    }
141}