Advertisement
Guest User

g

a guest
May 15th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import Toybox.Time;
  2. import Toybox.Lang;
  3. import Toybox.System;
  4.  
  5. // Parse a number from a substring
  6. //
  7. // Throws an exception if startIndex/endIndex are out of bounds, or if the substring
  8. // can't be converted to a number
  9. function parseNumber(str as String, startIndex as Number, endIndex as Number) as Number {
  10. var num = str.substring(startIndex, endIndex);
  11. if (num == null) {
  12. throw new Exception();
  13. }
  14. num = num.toNumber();
  15. if (num == null) {
  16. throw new Exception();
  17. }
  18. return num;
  19. }
  20.  
  21. function parseISO8601(timestamp as String) as Time.Moment? {
  22. // Accepts 2 formats:
  23. //
  24. // "2025-05-15T15:16:54Z"
  25. // "2025-05-15T15:16:54.123Z"
  26. //
  27. // milliseconds are ignored
  28.  
  29. var length = timestamp.length();
  30. if (length != 20 && length != 24) {
  31. return null;
  32. }
  33.  
  34. if (
  35. !("-").equals(timestamp.substring(4, 5)) ||
  36. !("-").equals(timestamp.substring(7, 8)) ||
  37. !("T").equals(timestamp.substring(10, 11)) ||
  38. !(":").equals(timestamp.substring(13, 14)) ||
  39. !(":").equals(timestamp.substring(16, 17)) ||
  40. !("Z").equals(timestamp.substring(length-1, length))
  41. ) {
  42. return null;
  43. }
  44.  
  45. try {
  46. var options = {
  47. :year => parseNumber(timestamp, 0, 4),
  48. :month => parseNumber(timestamp, 5, 7),
  49. :day => parseNumber(timestamp, 8, 10),
  50. :hour => parseNumber(timestamp, 11, 13),
  51. :minute => parseNumber(timestamp, 14, 16),
  52. :second => parseNumber(timestamp, 17, 19),
  53. };
  54. return Gregorian.moment(options);
  55. } catch (e) {
  56. return null;
  57. }
  58. }
  59.  
  60. function momentToLocalTime(moment as Time.Moment?) as String {
  61. if (moment == null) {
  62. return "--";
  63. }
  64.  
  65. // Returns local date/time in 24h format.
  66. // TODO: Add your own code to handle 12h format if necessary
  67. var info = Gregorian.info(moment, Time.FORMAT_MEDIUM);
  68. // e.g. "Thu May 15 2025 11:16:54"
  69. return info.day_of_week + " " + info.month + " " + info.day + " " + info.year + " " + info.hour + ":" + info.min + ":" + info.sec;
  70. }
  71.  
  72. function test() as Void {
  73. var timestamp = "2025-05-15T15:16:54Z";
  74. var moment = parseISO8601(timestamp);
  75. var localTime = momentToLocalTime(moment);
  76. System.println(localTime); // "Thu May 15 2025 11:16:54"
  77.  
  78.  
  79. timestamp = "2025-05-15T15:16:54.123Z";
  80. moment = parseISO8601(timestamp);
  81. localTime = momentToLocalTime(moment);
  82. System.println(localTime); // "Thu May 15 2025 11:16:54"
  83.  
  84.  
  85. timestamp = "ABCD-05-15T15:16:54Z"; // bad timestamp
  86. moment = parseISO8601(timestamp);
  87. localTime = momentToLocalTime(moment);
  88. System.println(localTime); // "--"
  89.  
  90. timestamp = "bad timestamp";
  91. moment = parseISO8601(timestamp);
  92. localTime = momentToLocalTime(moment);
  93. System.println(localTime); // "--"
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement