1048147e0Sopenharmony_ci/**
2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License.
5048147e0Sopenharmony_ci * You may obtain a copy of the License at
6048147e0Sopenharmony_ci *
7048147e0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8048147e0Sopenharmony_ci *
9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and
13048147e0Sopenharmony_ci * limitations under the License.
14048147e0Sopenharmony_ci */
15048147e0Sopenharmony_ciimport common from '../data/commonData';
16048147e0Sopenharmony_ciimport HiLog from './HiLog';
17048147e0Sopenharmony_ci
18048147e0Sopenharmony_ci// Time constant
19048147e0Sopenharmony_ciconst ONE_MINUTE_IN_MILLISECOND = 60000;
20048147e0Sopenharmony_ciconst ONE_HOUR_IN_MILLISECOND = 3600000;
21048147e0Sopenharmony_ci
22048147e0Sopenharmony_ciconst TAG = 'DateUtil';
23048147e0Sopenharmony_ci
24048147e0Sopenharmony_ciexport default {
25048147e0Sopenharmony_ci
26048147e0Sopenharmony_ci  /**
27048147e0Sopenharmony_ci   * Converts the timestamp of an item to a time (for the SMS message list).
28048147e0Sopenharmony_ci   *
29048147e0Sopenharmony_ci   * @param messageItem
30048147e0Sopenharmony_ci   * @param is24HourTime
31048147e0Sopenharmony_ci   * @return
32048147e0Sopenharmony_ci   */
33048147e0Sopenharmony_ci  convertDateFormatForItem(messageItem, is24HourTime) {
34048147e0Sopenharmony_ci    let now = new Date();
35048147e0Sopenharmony_ci    let currentDate = {
36048147e0Sopenharmony_ci      timeOfNow: now.getTime(),
37048147e0Sopenharmony_ci      yearOfNow: now.getFullYear(),
38048147e0Sopenharmony_ci      monthOfNow: now.getMonth() + 1,
39048147e0Sopenharmony_ci      dayOfNow: now.getDate()
40048147e0Sopenharmony_ci    };
41048147e0Sopenharmony_ci    let timeMillisecond = messageItem.timeMillisecond;
42048147e0Sopenharmony_ci    messageItem.time = this.convertTimeStampToTime(timeMillisecond, currentDate, is24HourTime);
43048147e0Sopenharmony_ci  },
44048147e0Sopenharmony_ci
45048147e0Sopenharmony_ci  /**
46048147e0Sopenharmony_ci   * Converting a Timestamp to a Time (for SMS List)
47048147e0Sopenharmony_ci   *
48048147e0Sopenharmony_ci   * @param timeStampFromDb Timeline to be converted
49048147e0Sopenharmony_ci   * @param currentDate Current Time
50048147e0Sopenharmony_ci   * @param is24HourTime
51048147e0Sopenharmony_ci   * @return
52048147e0Sopenharmony_ci   */
53048147e0Sopenharmony_ci  convertTimeStampToTime(timeStampFromDb, currentDate, is24HourTime) {
54048147e0Sopenharmony_ci    let sms = new Date(timeStampFromDb);
55048147e0Sopenharmony_ci    let timeStampOfSms = sms.getTime();
56048147e0Sopenharmony_ci    let yearOfSms = sms.getFullYear();
57048147e0Sopenharmony_ci    let monthOfSms = sms.getMonth() + 1;
58048147e0Sopenharmony_ci    let dayOfSms = sms.getDate();
59048147e0Sopenharmony_ci    let hoursOfSms = sms.getHours();
60048147e0Sopenharmony_ci    let minutesOfSms = sms.getMinutes();
61048147e0Sopenharmony_ci    let diff = currentDate.timeOfNow - timeStampOfSms;
62048147e0Sopenharmony_ci    if (currentDate.yearOfNow == yearOfSms && currentDate.monthOfNow ==
63048147e0Sopenharmony_ci      monthOfSms && currentDate.dayOfNow == dayOfSms) {
64048147e0Sopenharmony_ci      if (diff < ONE_MINUTE_IN_MILLISECOND) {
65048147e0Sopenharmony_ci        return $r('app.string.justNow');
66048147e0Sopenharmony_ci      } else if (diff < ONE_HOUR_IN_MILLISECOND) {
67048147e0Sopenharmony_ci        return $r('app.string.minAgo', Math.floor(diff / ONE_MINUTE_IN_MILLISECOND));
68048147e0Sopenharmony_ci      } else {
69048147e0Sopenharmony_ci        let hoursOfSmsStr: string = '' + hoursOfSms;
70048147e0Sopenharmony_ci        let minutesOfSmsStr: string = '' + minutesOfSms;
71048147e0Sopenharmony_ci        if (hoursOfSms < 10) {
72048147e0Sopenharmony_ci          hoursOfSmsStr = '0' + hoursOfSms;
73048147e0Sopenharmony_ci        }
74048147e0Sopenharmony_ci        if (minutesOfSms < 10) {
75048147e0Sopenharmony_ci          minutesOfSmsStr = '0' + minutesOfSms;
76048147e0Sopenharmony_ci        }
77048147e0Sopenharmony_ci        if (is24HourTime) {
78048147e0Sopenharmony_ci          return $r('app.string.hourAndMinute', hoursOfSmsStr, minutesOfSmsStr);
79048147e0Sopenharmony_ci        } else {
80048147e0Sopenharmony_ci          return this.timeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr);
81048147e0Sopenharmony_ci        }
82048147e0Sopenharmony_ci      }
83048147e0Sopenharmony_ci    } else if (currentDate.yearOfNow == yearOfSms && currentDate.monthOfNow ==
84048147e0Sopenharmony_ci      monthOfSms && currentDate.dayOfNow - dayOfSms == 1) {
85048147e0Sopenharmony_ci      return $r('app.string.yesterday');
86048147e0Sopenharmony_ci    } else if (currentDate.yearOfNow == yearOfSms) {
87048147e0Sopenharmony_ci      return $r('app.string.monthDay', monthOfSms, dayOfSms);
88048147e0Sopenharmony_ci    } else {
89048147e0Sopenharmony_ci      return $r('app.string.yearMonthDay', yearOfSms, monthOfSms, dayOfSms);
90048147e0Sopenharmony_ci    }
91048147e0Sopenharmony_ci  },
92048147e0Sopenharmony_ci
93048147e0Sopenharmony_ci  timeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr) {
94048147e0Sopenharmony_ci    let time: Resource = null;
95048147e0Sopenharmony_ci    if (hoursOfSms <= 12) {
96048147e0Sopenharmony_ci      time = this.morningTimeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr);
97048147e0Sopenharmony_ci    } else {
98048147e0Sopenharmony_ci      time = this.afterTimeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr);
99048147e0Sopenharmony_ci    }
100048147e0Sopenharmony_ci    return time;
101048147e0Sopenharmony_ci  },
102048147e0Sopenharmony_ci
103048147e0Sopenharmony_ci  morningTimeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr) {
104048147e0Sopenharmony_ci    let time: Resource = null;
105048147e0Sopenharmony_ci    // 12-hour clock
106048147e0Sopenharmony_ci    if (hoursOfSms < 1) {
107048147e0Sopenharmony_ci      time = $r('app.string.postMidnight', 12 + '', minutesOfSmsStr);
108048147e0Sopenharmony_ci    }
109048147e0Sopenharmony_ci    if (hoursOfSms >= 1 && hoursOfSms < 4) {
110048147e0Sopenharmony_ci      time = $r('app.string.beforeDawn', hoursOfSmsStr, minutesOfSmsStr);
111048147e0Sopenharmony_ci    }
112048147e0Sopenharmony_ci    if (hoursOfSms >= 4 && hoursOfSms < 6) {
113048147e0Sopenharmony_ci      time = $r('app.string.earlyMorning', hoursOfSmsStr, minutesOfSmsStr);
114048147e0Sopenharmony_ci    }
115048147e0Sopenharmony_ci    if (hoursOfSms >= 6 && hoursOfSms < 9) {
116048147e0Sopenharmony_ci      time = $r('app.string.morning', hoursOfSmsStr, minutesOfSmsStr);
117048147e0Sopenharmony_ci    }
118048147e0Sopenharmony_ci    if (hoursOfSms >= 9 && hoursOfSms < 11) {
119048147e0Sopenharmony_ci      time = $r('app.string.forenoon', hoursOfSmsStr, minutesOfSmsStr);
120048147e0Sopenharmony_ci    }
121048147e0Sopenharmony_ci    if (hoursOfSms >= 11 && hoursOfSms <= 12) {
122048147e0Sopenharmony_ci      time = $r('app.string.preNoon', hoursOfSms + '', minutesOfSmsStr);
123048147e0Sopenharmony_ci    }
124048147e0Sopenharmony_ci    return time;
125048147e0Sopenharmony_ci  },
126048147e0Sopenharmony_ci
127048147e0Sopenharmony_ci  afterTimeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr) {
128048147e0Sopenharmony_ci    let time: Resource = null;
129048147e0Sopenharmony_ci    if (hoursOfSms > 12 && hoursOfSms < 13) {
130048147e0Sopenharmony_ci      time = $r('app.string.postNoon', hoursOfSms + '', minutesOfSmsStr);
131048147e0Sopenharmony_ci    }
132048147e0Sopenharmony_ci    if (hoursOfSms >= 13 && hoursOfSms < 17) {
133048147e0Sopenharmony_ci      time = $r('app.string.afternoon', (hoursOfSms - 12) + '', minutesOfSmsStr);
134048147e0Sopenharmony_ci    }
135048147e0Sopenharmony_ci    if (hoursOfSms >= 17 && hoursOfSms < 19) {
136048147e0Sopenharmony_ci      time = $r('app.string.towardEvening', (hoursOfSms - 12) + '', minutesOfSmsStr);
137048147e0Sopenharmony_ci    }
138048147e0Sopenharmony_ci    if (hoursOfSms >= 19 && hoursOfSms < 23) {
139048147e0Sopenharmony_ci      time = $r('app.string.evening', (hoursOfSms - 12) + '', minutesOfSmsStr);
140048147e0Sopenharmony_ci    }
141048147e0Sopenharmony_ci    if (hoursOfSms >= 23) {
142048147e0Sopenharmony_ci      time = $r('app.string.preMidnight', (hoursOfSms - 12) + '', minutesOfSmsStr);
143048147e0Sopenharmony_ci    }
144048147e0Sopenharmony_ci    return time;
145048147e0Sopenharmony_ci  },
146048147e0Sopenharmony_ci
147048147e0Sopenharmony_ci  /**
148048147e0Sopenharmony_ci   * Converting Time
149048147e0Sopenharmony_ci   * @param messageItem
150048147e0Sopenharmony_ci   * @param is24HourTime
151048147e0Sopenharmony_ci   * @param that
152048147e0Sopenharmony_ci   * @return
153048147e0Sopenharmony_ci   */
154048147e0Sopenharmony_ci  convertTimeStampToDateWeek(messageItem, is24HourTime) {
155048147e0Sopenharmony_ci    let now = new Date();
156048147e0Sopenharmony_ci    let yearOfNow = now.getFullYear();
157048147e0Sopenharmony_ci    let monthOfNow = now.getMonth() + 1;
158048147e0Sopenharmony_ci    let dayOfNow = now.getDate();
159048147e0Sopenharmony_ci    let timeMillisecond = messageItem.timeMillisecond;
160048147e0Sopenharmony_ci    this.convertTimeStampToDate(messageItem, timeMillisecond, yearOfNow, monthOfNow, dayOfNow);
161048147e0Sopenharmony_ci  },
162048147e0Sopenharmony_ci
163048147e0Sopenharmony_ci  /**
164048147e0Sopenharmony_ci   * Converts the timestamp form to the date form.
165048147e0Sopenharmony_ci   */
166048147e0Sopenharmony_ci  convertTimeStampToDate(messageItem, timeStampFromDb, yearOfNow, monthOfNow, dayOfNow) {
167048147e0Sopenharmony_ci    let date: Resource = null;
168048147e0Sopenharmony_ci    let sms = new Date(timeStampFromDb);
169048147e0Sopenharmony_ci    let yearOfSms = sms.getFullYear();
170048147e0Sopenharmony_ci    let monthOfSms = sms.getMonth() + 1;
171048147e0Sopenharmony_ci    let dayOfSms = sms.getDate();
172048147e0Sopenharmony_ci    let weekOfSms = sms.getDay();
173048147e0Sopenharmony_ci    if (yearOfNow == yearOfSms && monthOfNow == monthOfSms && dayOfNow == dayOfSms) {
174048147e0Sopenharmony_ci      date = $r('app.string.recentDateToday', '');
175048147e0Sopenharmony_ci    } else if (yearOfNow == yearOfSms && monthOfNow == monthOfSms && dayOfNow - dayOfSms == 1) {
176048147e0Sopenharmony_ci      date = $r('app.string.recentDateYesterday', '');
177048147e0Sopenharmony_ci    } else if (yearOfNow == yearOfSms) {
178048147e0Sopenharmony_ci      date = $r('app.string.monthDayDate', monthOfSms, dayOfSms, '');
179048147e0Sopenharmony_ci    } else {
180048147e0Sopenharmony_ci      date = $r('app.string.yearMonthDayDate', yearOfSms, monthOfSms, dayOfSms, '');
181048147e0Sopenharmony_ci    }
182048147e0Sopenharmony_ci    messageItem.date = date;
183048147e0Sopenharmony_ci    messageItem.week = this.getWeek(weekOfSms);
184048147e0Sopenharmony_ci  },
185048147e0Sopenharmony_ci
186048147e0Sopenharmony_ci  /**
187048147e0Sopenharmony_ci   * Get Week/Day of the Week
188048147e0Sopenharmony_ci   * @param day
189048147e0Sopenharmony_ci   * @param that
190048147e0Sopenharmony_ci   * @return
191048147e0Sopenharmony_ci   */
192048147e0Sopenharmony_ci  getWeek(day) {
193048147e0Sopenharmony_ci    let week: Resource = null;
194048147e0Sopenharmony_ci    switch (day) {
195048147e0Sopenharmony_ci      case 0:
196048147e0Sopenharmony_ci        week = $r('app.string.Sunday');
197048147e0Sopenharmony_ci        break;
198048147e0Sopenharmony_ci      case 1:
199048147e0Sopenharmony_ci        week = $r('app.string.Monday');
200048147e0Sopenharmony_ci        break;
201048147e0Sopenharmony_ci      case 2:
202048147e0Sopenharmony_ci        week = $r('app.string.Tuesday');
203048147e0Sopenharmony_ci        break;
204048147e0Sopenharmony_ci      case 3:
205048147e0Sopenharmony_ci        week = $r('app.string.Wednesday');
206048147e0Sopenharmony_ci        break;
207048147e0Sopenharmony_ci      case 4:
208048147e0Sopenharmony_ci        week = $r('app.string.Thursday');
209048147e0Sopenharmony_ci        break;
210048147e0Sopenharmony_ci      case 5:
211048147e0Sopenharmony_ci        week = $r('app.string.Friday');
212048147e0Sopenharmony_ci        break;
213048147e0Sopenharmony_ci      case 6:
214048147e0Sopenharmony_ci        week = $r('app.string.Saturday');
215048147e0Sopenharmony_ci        break;
216048147e0Sopenharmony_ci
217048147e0Sopenharmony_ci    }
218048147e0Sopenharmony_ci    return week;
219048147e0Sopenharmony_ci  },
220048147e0Sopenharmony_ci
221048147e0Sopenharmony_ci  convertTimeStampDate(timeStampFromDb) {
222048147e0Sopenharmony_ci    let date: Resource = null;
223048147e0Sopenharmony_ci    let sms = new Date(Number(timeStampFromDb));
224048147e0Sopenharmony_ci    let yearOfSms = sms.getFullYear();
225048147e0Sopenharmony_ci    let monthOfSms = sms.getMonth() + 1;
226048147e0Sopenharmony_ci    let dayOfSms = sms.getDate();
227048147e0Sopenharmony_ci    let weekOfSms = sms.getDay();
228048147e0Sopenharmony_ci    date = $r('app.string.yearMonthDayDate', yearOfSms, monthOfSms, dayOfSms, '');
229048147e0Sopenharmony_ci    return date;
230048147e0Sopenharmony_ci  },
231048147e0Sopenharmony_ci
232048147e0Sopenharmony_ci  /**
233048147e0Sopenharmony_ci   *
234048147e0Sopenharmony_ci   * @param timeStampFromDb
235048147e0Sopenharmony_ci   * @param is24HourTime
236048147e0Sopenharmony_ci   * @param that
237048147e0Sopenharmony_ci   * @return
238048147e0Sopenharmony_ci   */
239048147e0Sopenharmony_ci  convertTimeStampTime(timeStampFromDb, is24HourTime) {
240048147e0Sopenharmony_ci    let sms = new Date(Number(timeStampFromDb));
241048147e0Sopenharmony_ci    let hoursOfSms = sms.getHours();
242048147e0Sopenharmony_ci    let minutesOfSms = sms.getMinutes();
243048147e0Sopenharmony_ci    let hoursOfSmsStr: string = hoursOfSms + '';
244048147e0Sopenharmony_ci    let minutesOfSmsStr: string = minutesOfSms + '';
245048147e0Sopenharmony_ci    if (hoursOfSms < 10) {
246048147e0Sopenharmony_ci      hoursOfSmsStr = '0' + hoursOfSms;
247048147e0Sopenharmony_ci    }
248048147e0Sopenharmony_ci    if (minutesOfSms < 10) {
249048147e0Sopenharmony_ci      minutesOfSmsStr = '0' + minutesOfSms;
250048147e0Sopenharmony_ci    }
251048147e0Sopenharmony_ci    if (is24HourTime) {
252048147e0Sopenharmony_ci      // 24-hour clock
253048147e0Sopenharmony_ci      return $r('app.string.hourAndMinute', hoursOfSmsStr, minutesOfSmsStr);
254048147e0Sopenharmony_ci    }
255048147e0Sopenharmony_ci    return this.timeTwelveHourSystem(hoursOfSms, minutesOfSmsStr, hoursOfSmsStr);
256048147e0Sopenharmony_ci  },
257048147e0Sopenharmony_ci
258048147e0Sopenharmony_ci  fullDate(item) {
259048147e0Sopenharmony_ci    let timeOfSms: Resource = null;
260048147e0Sopenharmony_ci    let fullDate: Resource = null;
261048147e0Sopenharmony_ci    let sms = new Date(item.timeMillisecond);
262048147e0Sopenharmony_ci    let yearOfSms = sms.getFullYear();
263048147e0Sopenharmony_ci    let monthOfSms = sms.getMonth() + 1;
264048147e0Sopenharmony_ci    let dayOfSms = sms.getDate();
265048147e0Sopenharmony_ci    let hoursOfSms = sms.getHours();
266048147e0Sopenharmony_ci    let minutesOfSms = sms.getMinutes();
267048147e0Sopenharmony_ci    let hoursOfSmsStr: string = hoursOfSms + '';
268048147e0Sopenharmony_ci    let minutesOfSmsStr: string = minutesOfSms + '';
269048147e0Sopenharmony_ci    if (hoursOfSms > 12) {
270048147e0Sopenharmony_ci      hoursOfSmsStr = '' + (hoursOfSms - 12);
271048147e0Sopenharmony_ci    }
272048147e0Sopenharmony_ci    if (minutesOfSms < 10) {
273048147e0Sopenharmony_ci      minutesOfSmsStr = '0' + minutesOfSms;
274048147e0Sopenharmony_ci    }
275048147e0Sopenharmony_ci    if (hoursOfSms < 12) {
276048147e0Sopenharmony_ci      timeOfSms = $r('app.string.forenoon', hoursOfSmsStr, minutesOfSmsStr);
277048147e0Sopenharmony_ci    } else {
278048147e0Sopenharmony_ci      timeOfSms = $r('app.string.afternoon', hoursOfSmsStr, minutesOfSmsStr);
279048147e0Sopenharmony_ci    }
280048147e0Sopenharmony_ci    fullDate = $r('app.string.yearMonthDayDate', yearOfSms, monthOfSms, dayOfSms, '');
281048147e0Sopenharmony_ci    item.fullDate = fullDate;
282048147e0Sopenharmony_ci    item.timeOfSms = timeOfSms;
283048147e0Sopenharmony_ci  }
284048147e0Sopenharmony_ci}