1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2007-2010, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package ohos.global.icu.util; 11 12 import java.io.Serializable; 13 14 /** 15 * <code>DateTimeRule</code> is a class representing a time in a year by 16 * a rule specified by month, day of month, day of week and 17 * time in the day. 18 * 19 * @hide exposed on OHOS 20 */ 21 public class DateTimeRule implements Serializable { 22 23 private static final long serialVersionUID = 2183055795738051443L; 24 25 /** 26 * Date rule type defined by exact day of month. 27 * For example, March 14. 28 */ 29 public static final int DOM = 0; 30 31 /** 32 * Date rule type defined by day of week in month. 33 * For example, 2nd Sunday in March. 34 */ 35 public static final int DOW = 1; 36 37 /** 38 * Date rule type defined by first day of week on or 39 * after exact day of month. 40 * For example, 1st Monday on or after March 15. 41 */ 42 public static final int DOW_GEQ_DOM = 2; 43 44 /** 45 * Date rule type defined by last day of week on or 46 * before exact day of month. 47 * For example, last Saturday on or before March 15. 48 */ 49 public static final int DOW_LEQ_DOM = 3; 50 51 /** 52 * Time rule type for local wall time. 53 */ 54 public static final int WALL_TIME = 0; 55 56 /** 57 * Time rule type for local standard time. 58 */ 59 public static final int STANDARD_TIME = 1; 60 61 /** 62 * Time rule type for coordinated universal time. 63 */ 64 public static final int UTC_TIME = 2; 65 66 // private stuff 67 private final int dateRuleType; 68 private final int month; 69 private final int dayOfMonth; 70 private final int dayOfWeek; 71 private final int weekInMonth; 72 73 private final int timeRuleType; 74 private final int millisInDay; 75 76 /** 77 * Constructs a <code>DateTimeRule</code> by the day of month and 78 * the time rule. The date rule type for an instance created by 79 * this constructor is <code>DOM</code>. 80 * 81 * @param month The rule month, for example, <code>Calendar.JANUARY</code> 82 * @param dayOfMonth The day of month, 1-based. 83 * @param millisInDay The milliseconds in the rule date. 84 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 85 * or <code>UTC_TIME</code>. 86 */ DateTimeRule(int month, int dayOfMonth, int millisInDay, int timeType)87 public DateTimeRule(int month, int dayOfMonth, 88 int millisInDay, int timeType) { 89 dateRuleType = DOM; 90 this.month = month; 91 this.dayOfMonth = dayOfMonth; 92 93 this.millisInDay = millisInDay; 94 this.timeRuleType = timeType; 95 96 // not used by this rule type 97 this.dayOfWeek = 0; 98 this.weekInMonth = 0; 99 } 100 101 /** 102 * Constructs a <code>DateTimeRule</code> by the day of week and its oridinal 103 * number and the time rule. The date rule type for an instance created 104 * by this constructor is <code>DOW</code>. 105 * 106 * @param month The rule month, for example, <code>Calendar.JANUARY</code>. 107 * @param weekInMonth The ordinal number of the day of week. Negative number 108 * may be used for specifying a rule date counted from the 109 * end of the rule month. 110 * @param dayOfWeek The day of week, for example, <code>Calendar.SUNDAY</code>. 111 * @param millisInDay The milliseconds in the rule date. 112 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 113 * or <code>UTC_TIME</code>. 114 */ DateTimeRule(int month, int weekInMonth, int dayOfWeek, int millisInDay, int timeType)115 public DateTimeRule(int month, int weekInMonth, int dayOfWeek, 116 int millisInDay, int timeType) { 117 dateRuleType = DOW; 118 this.month = month; 119 this.weekInMonth = weekInMonth; 120 this.dayOfWeek = dayOfWeek; 121 122 this.millisInDay = millisInDay; 123 this.timeRuleType = timeType; 124 125 // not used by this rule type 126 this.dayOfMonth = 0; 127 } 128 129 /** 130 * Constructs a <code>DateTimeRule</code> by the first/last day of week 131 * on or after/before the day of month and the time rule. The date rule 132 * type for an instance created by this constructor is either 133 * <code>DOM_GEQ_DOM</code> or <code>DOM_LEQ_DOM</code>. 134 * 135 * @param month The rule month, for example, <code>Calendar.JANUARY</code> 136 * @param dayOfMonth The day of month, 1-based. 137 * @param dayOfWeek The day of week, for example, <code>Calendar.SUNDAY</code>. 138 * @param after true if the rule date is on or after the day of month. 139 * @param millisInDay The milliseconds in the rule date. 140 * @param timeType The time type, <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 141 * or <code>UTC_TIME</code>. 142 */ DateTimeRule(int month, int dayOfMonth, int dayOfWeek, boolean after, int millisInDay, int timeType)143 public DateTimeRule(int month, int dayOfMonth, int dayOfWeek, boolean after, 144 int millisInDay, int timeType) { 145 this.dateRuleType = after ? DOW_GEQ_DOM : DOW_LEQ_DOM; 146 this.month = month; 147 this.dayOfMonth = dayOfMonth; 148 this.dayOfWeek = dayOfWeek; 149 150 this.millisInDay = millisInDay; 151 this.timeRuleType = timeType; 152 153 // not used by this rule type 154 this.weekInMonth = 0; 155 } 156 157 /** 158 * Gets the date rule type, such as <code>DOM</code> 159 * 160 * @return The date rule type. 161 */ getDateRuleType()162 public int getDateRuleType() { 163 return dateRuleType; 164 } 165 166 /** 167 * Gets the rule month. 168 * 169 * @return The rule month. 170 */ getRuleMonth()171 public int getRuleMonth() { 172 return month; 173 } 174 175 /** 176 * Gets the rule day of month. When the date rule type 177 * is <code>DOW</code>, the value is always 0. 178 * 179 * @return The rule day of month 180 */ getRuleDayOfMonth()181 public int getRuleDayOfMonth() { 182 return dayOfMonth; 183 } 184 185 /** 186 * Gets the rule day of week. When the date rule type 187 * is <code>DOM</code>, the value is always 0. 188 * 189 * @return The rule day of week. 190 */ getRuleDayOfWeek()191 public int getRuleDayOfWeek() { 192 return dayOfWeek; 193 } 194 195 /** 196 * Gets the rule day of week ordinal number in the month. 197 * When the date rule type is not <code>DOW</code>, the value is 198 * always 0. 199 * 200 * @return The rule day of week ordinal number in the month. 201 */ getRuleWeekInMonth()202 public int getRuleWeekInMonth() { 203 return weekInMonth; 204 } 205 206 /** 207 * Gets the time rule type 208 * 209 * @return The time rule type, either <code>WALL_TIME</code> or <code>STANDARD_TIME</code> 210 * or <code>UTC_TIME</code>. 211 */ getTimeRuleType()212 public int getTimeRuleType() { 213 return timeRuleType; 214 } 215 216 /** 217 * Gets the rule time in the rule day. 218 * 219 * @return The time in the rule day in milliseconds. 220 */ getRuleMillisInDay()221 public int getRuleMillisInDay() { 222 return millisInDay; 223 } 224 225 private static final String[] DOWSTR = {"", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 226 private static final String[] MONSTR = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 227 228 /** 229 * Returns a <code>String</code> representation of this <code>DateTimeRule</code> object. 230 * This method is used for debugging purpose only. The string representation can be changed 231 * in future version of ICU without any notice. 232 */ 233 @Override toString()234 public String toString() { 235 String sDate = null; 236 String sTimeRuleType = null; 237 238 switch (dateRuleType) { 239 case DOM: 240 sDate = Integer.toString(dayOfMonth); 241 break; 242 case DOW: 243 sDate = Integer.toString(weekInMonth) + DOWSTR[dayOfWeek]; 244 break; 245 case DOW_GEQ_DOM: 246 sDate = DOWSTR[dayOfWeek] + ">=" + Integer.toString(dayOfMonth); 247 break; 248 case DOW_LEQ_DOM: 249 sDate = DOWSTR[dayOfWeek] + "<=" + Integer.toString(dayOfMonth); 250 break; 251 } 252 253 switch (timeRuleType) { 254 case WALL_TIME: 255 sTimeRuleType = "WALL"; 256 break; 257 case STANDARD_TIME: 258 sTimeRuleType = "STD"; 259 break; 260 case UTC_TIME: 261 sTimeRuleType = "UTC"; 262 break; 263 } 264 265 int time = millisInDay; 266 int millis = time % 1000; 267 time /= 1000; 268 int secs = time % 60; 269 time /= 60; 270 int mins = time % 60; 271 int hours = time / 60; 272 273 StringBuilder buf = new StringBuilder(); 274 buf.append("month="); 275 buf.append(MONSTR[month]); 276 buf.append(", date="); 277 buf.append(sDate); 278 buf.append(", time="); 279 buf.append(hours); 280 buf.append(":"); 281 buf.append(mins/10); 282 buf.append(mins%10); 283 buf.append(":"); 284 buf.append(secs/10); 285 buf.append(secs%10); 286 buf.append("."); 287 buf.append(millis/100); 288 buf.append((millis/10)%10); 289 buf.append(millis%10); 290 buf.append("("); 291 buf.append(sTimeRuleType); 292 buf.append(")"); 293 return buf.toString(); 294 } 295 } 296