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 java.util.Calendar; 020import java.util.Date; 021import java.util.logging.Level; 022import java.util.logging.Logger; 023 024import org.jivesoftware.smack.packet.IQ; 025 026import org.jxmpp.util.XmppDateTime; 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 super(ELEMENT, NAMESPACE); 047 setType(Type.get); 048 } 049 050 /** 051 * Creates a new Time instance using the specified calendar instance as 052 * the time value to send. 053 * 054 * @param cal the time value. 055 */ 056 public Time(Calendar cal) { 057 super(ELEMENT, NAMESPACE); 058 tzo = XmppDateTime.asString(cal.getTimeZone()); 059 // Convert local time to the UTC time. 060 utc = XmppDateTime.formatXEP0082Date(cal.getTime()); 061 } 062 063 /** 064 * Returns the local time or <tt>null</tt> if the time hasn't been set. 065 * 066 * @return the local time. 067 */ 068 public Date getTime() { 069 if (utc == null) { 070 return null; 071 } 072 Date date = null; 073 try { 074 date = XmppDateTime.parseDate(utc); 075 } 076 catch (Exception e) { 077 LOGGER.log(Level.SEVERE, "Error getting local time", e); 078 } 079 return date; 080 } 081 082 /** 083 * Sets the time using the local time. 084 * 085 * @param time the current local time. 086 */ 087 public void setTime(Date time) { 088 } 089 090 /** 091 * Returns the time as a UTC formatted String using the format CCYY-MM-DDThh:mm:ssZ. 092 * 093 * @return the time as a UTC formatted String. 094 */ 095 public String getUtc() { 096 return utc; 097 } 098 099 /** 100 * Sets the time using UTC formatted String in the format CCYY-MM-DDThh:mm:ssZ. 101 * 102 * @param utc the time using a formatted String. 103 */ 104 public void setUtc(String utc) { 105 this.utc = utc; 106 } 107 108 /** 109 * Returns the time zone. 110 * 111 * @return the time zone. 112 */ 113 public String getTzo() { 114 return tzo; 115 } 116 117 /** 118 * Sets the time zone offset. 119 * 120 * @param tzo the time zone offset. 121 */ 122 public void setTzo(String tzo) { 123 this.tzo = tzo; 124 } 125 126 public static Time createResponse(IQ request) { 127 Time time = new Time(Calendar.getInstance()); 128 time.setType(Type.result); 129 time.setTo(request.getFrom()); 130 return time; 131 } 132 133 @Override 134 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 135 if (utc != null) { 136 buf.rightAngleBracket(); 137 buf.append("<utc>").append(utc).append("</utc>"); 138 buf.append("<tzo>").append(tzo).append("</tzo>"); 139 } else { 140 buf.setEmptyElement(); 141 } 142 143 return buf; 144 } 145}