瀏覽代碼

Adds short timestamp format to timestamp_t

João Brázio 9 年之前
父節點
當前提交
ecd48027fa
共有 1 個文件被更改,包括 31 次插入21 次删除
  1. 31
    21
      Marlin/timestamp_t.h

+ 31
- 21
Marlin/timestamp_t.h 查看文件

@@ -30,13 +30,13 @@ struct timestamp_t {
30 30
   uint32_t timestamp;
31 31
 
32 32
   /**
33
-   * @brief Date time blank constructor
33
+   * @brief Timestamp blank constructor
34 34
    */
35 35
   timestamp_t()
36 36
     : timestamp_t(0) {};
37 37
 
38 38
   /**
39
-   * @brief Date time constructor
39
+   * @briefTimestamp constructor
40 40
    * @details Initializes the timestamp_t structure based on a number of seconds
41 41
    *
42 42
    * @param seconds The number of seconds
@@ -46,7 +46,7 @@ struct timestamp_t {
46 46
   }
47 47
 
48 48
   /**
49
-   * @brief Formats the date as number of years
49
+   * @brief Formats the timestamp in years
50 50
    * @return The number of years
51 51
    */
52 52
   inline uint8_t year() const {
@@ -54,7 +54,7 @@ struct timestamp_t {
54 54
   }
55 55
 
56 56
   /**
57
-   * @brief Formats the date as number of days
57
+   * @brief Formats the timestamp in days
58 58
    * @return The number of days
59 59
    */
60 60
   inline uint16_t day() const {
@@ -62,7 +62,7 @@ struct timestamp_t {
62 62
   }
63 63
 
64 64
   /**
65
-   * @brief Formats the date as number of hours
65
+   * @brief Formats the timestamp in hours
66 66
    * @return The number of hours
67 67
    */
68 68
   inline uint32_t hour() const {
@@ -70,7 +70,7 @@ struct timestamp_t {
70 70
   }
71 71
 
72 72
   /**
73
-   * @brief Formats the date as number of minutes
73
+   * @brief Formats the timestamp in minutes
74 74
    * @return The number of minutes
75 75
    */
76 76
   inline uint32_t minute() const {
@@ -78,7 +78,7 @@ struct timestamp_t {
78 78
   }
79 79
 
80 80
   /**
81
-   * @brief Formats the date as number of seconds
81
+   * @brief Formats the timestamp in seconds
82 82
    * @return The number of seconds
83 83
    */
84 84
   inline uint32_t second() const {
@@ -86,12 +86,14 @@ struct timestamp_t {
86 86
   }
87 87
 
88 88
   /**
89
-   * @brief Formats the date as a string
89
+   * @brief Formats the timestamp as a string
90 90
    * @details Returns the timestamp formated as a string
91 91
    *
92
-   * @param buffer The array pointed to must be able to accommodate 21 bytes
92
+   * @param buffer The array pointed to must be able to accommodate 21 bytes when
93
+   *               on standard mode or 10 bytes otherwise.
94
+   * @param shorty If true a short representation will be returned.
93 95
    *
94
-   * String output examples:
96
+   * Standard toString() output examples:
95 97
    *  123456789012345678901 (strlen)
96 98
    *  135y 364d 23h 59m 59s
97 99
    *  364d 23h 59m 59s
@@ -99,19 +101,27 @@ struct timestamp_t {
99 101
    *  59m 59s
100 102
    *  59s
101 103
    *
104
+   * Short toString() output examples:
105
+   *  1234567890 (strlen)
106
+   *  1193046:59
107
+   *
102 108
    */
103
-  void toString(char *buffer) const {
104
-    int y = this->year(),
105
-        d = this->day() % 365,
106
-        h = this->hour() % 24,
107
-        m = this->minute() % 60,
108
-        s = this->second() % 60;
109
+  void toString(char *buffer, bool const &shorty = false) const {
110
+    int h = this->hour() % 24,
111
+        m = this->minute() % 60;
112
+
113
+    if (shorty) sprintf_P(buffer, PSTR("%02i:%02i"), h, m);
114
+    else {
115
+      int y = this->year(),
116
+          d = this->day() % 365,
117
+          s = this->second() % 60;
109 118
 
110
-    if (y) sprintf_P(buffer, PSTR("%iy %id %ih %im %is"), y, d, h, m, s);
111
-    else if (d) sprintf_P(buffer, PSTR("%id %ih %im %is"), d, h, m, s);
112
-    else if (h) sprintf_P(buffer, PSTR("%ih %im %is"), h, m, s);
113
-    else if (m) sprintf_P(buffer, PSTR("%im %is"), m, s);
114
-    else sprintf_P(buffer, PSTR("%is"), s);
119
+      if (y) sprintf_P(buffer, PSTR("%iy %id %ih %im %is"), y, d, h, m, s);
120
+      else if (d) sprintf_P(buffer, PSTR("%id %ih %im %is"), d, h, m, s);
121
+      else if (h) sprintf_P(buffer, PSTR("%ih %im %is"), h, m, s);
122
+      else if (m) sprintf_P(buffer, PSTR("%im %is"), m, s);
123
+      else sprintf_P(buffer, PSTR("%is"), s);
124
+    }
115 125
   }
116 126
 };
117 127
 

Loading…
取消
儲存