001/** 002 * 003 * Copyright 2021 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.text.ParseException; 020import java.util.Calendar; 021 022import org.jivesoftware.smack.XMPPConnection; 023import org.jivesoftware.smack.packet.IqBuilder; 024import org.jivesoftware.smack.packet.IqData; 025import org.jivesoftware.smack.util.StringUtils; 026 027import org.jxmpp.util.XmppDateTime; 028 029// TODO: Use java.time.ZonedDataTime once Smack's minimum Android SDK API level is 26 or higher. 030public class TimeBuilder extends IqBuilder<TimeBuilder, Time> implements TimeView { 031 032 private String utc; 033 private String tzo; 034 035 TimeBuilder(IqData iqCommon) { 036 super(iqCommon); 037 } 038 039 TimeBuilder(XMPPConnection connection) { 040 super(connection); 041 } 042 043 TimeBuilder(String stanzaId) { 044 super(stanzaId); 045 } 046 047 /** 048 * Sets the time using UTC formatted String, in the format CCYY-MM-DDThh:mm:ssZ, and the provided timezone 049 * definition in the format (+|-)hh:mm. 050 * 051 * @param utc the time using a formatted String. 052 * @param tzo the time zone definition. 053 * @return a reference to this builder. 054 * @throws ParseException if the provided string is not parsable (e.g. because it does not follow the expected 055 * format). 056 */ 057 public TimeBuilder setUtcAndTzo(String utc, String tzo) throws ParseException { 058 this.utc = StringUtils.requireNotNullNorEmpty(utc, "Must provide utc argument"); 059 // Sanity check the provided string. 060 XmppDateTime.parseDate(utc); 061 062 this.tzo = StringUtils.requireNotNullNorEmpty(tzo, "Must provide tzo argument"); 063 064 return getThis(); 065 } 066 067 public TimeBuilder setTime(Calendar calendar) { 068 // Convert local time to the UTC time. 069 utc = XmppDateTime.formatXEP0082Date(calendar.getTime()); 070 tzo = XmppDateTime.asString(calendar.getTimeZone()); 071 072 return getThis(); 073 } 074 075 @Override 076 public String getUtc() { 077 return utc; 078 } 079 080 @Override 081 public String getTzo() { 082 return tzo; 083 } 084 085 @Override 086 public Time build() { 087 return new Time(this); 088 } 089 090 @Override 091 public TimeBuilder getThis() { 092 return this; 093 } 094}