DateFormatUtil.ets 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2023 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { CommonConstants } from '../constants/CommonConstants';
  16. class DateFormatUtil {
  17. /**
  18. * Seconds converted to HH:mm:ss.
  19. *
  20. * @param seconds Maximum video duration (seconds).
  21. * @return Time after conversion.
  22. */
  23. secondToTime(seconds: number): string {
  24. let hourUnit = CommonConstants.TIME_UNIT * CommonConstants.TIME_UNIT;
  25. let hour = Math.floor(seconds / hourUnit);
  26. let minute = Math.floor((seconds - hour * hourUnit) / CommonConstants.TIME_UNIT);
  27. let second = seconds - hour * hourUnit - minute * CommonConstants.TIME_UNIT;
  28. if (hour > 0) {
  29. return `${this.padding(hour.toString())}${':'}
  30. ${this.padding(minute.toString())}${':'}${this.padding(second.toString())}`;
  31. }
  32. if (minute > 0) {
  33. return `${this.padding(minute.toString())}${':'}${this.padding(second.toString())}`;
  34. } else {
  35. return `${CommonConstants.INITIAL_TIME_UNIT}${':'}${this.padding(second.toString())}`;
  36. }
  37. }
  38. /**
  39. * Zero padding, 2 bits.
  40. *
  41. * @param num Number to be converted.
  42. * @return Result after zero padding.
  43. */
  44. padding(num: string): string {
  45. let length = CommonConstants.PADDING_LENGTH;
  46. for (let len = (num.toString()).length; len < length; len = num.length) {
  47. num = `${CommonConstants.PADDING_STR}${num}`;
  48. }
  49. return num;
  50. }
  51. }
  52. export default new DateFormatUtil();