Browse Source

Implements timestamp_t structure

João Brázio 9 years ago
parent
commit
9388dcfa49
1 changed files with 118 additions and 0 deletions
  1. 118
    0
      Marlin/timestamp_t.h

+ 118
- 0
Marlin/timestamp_t.h View File

@@ -0,0 +1,118 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#ifndef __TIMESTAMP_T__
24
+#define __TIMESTAMP_T__
25
+
26
+struct timestamp_t {
27
+  /**
28
+   * @brief Number of seconds
29
+   */
30
+  uint32_t timestamp;
31
+
32
+  /**
33
+   * @brief Date time blank constructor
34
+   */
35
+  timestamp_t()
36
+    : timestamp_t(0) {};
37
+
38
+  /**
39
+   * @brief Date time constructor
40
+   * @details Initializes the timestamp_t structure based on a number of seconds
41
+   *
42
+   * @param seconds The number of seconds
43
+   */
44
+  timestamp_t(uint32_t const &seconds) {
45
+    this->timestamp = seconds;
46
+  }
47
+
48
+  /**
49
+   * @brief Formats the date as number of years
50
+   * @return The number of years
51
+   */
52
+  inline uint8_t year() const {
53
+    return this->day() / 365;
54
+  }
55
+
56
+  /**
57
+   * @brief Formats the date as number of days
58
+   * @return The number of days
59
+   */
60
+  inline uint16_t day() const {
61
+    return this->hour() / 24;
62
+  }
63
+
64
+  /**
65
+   * @brief Formats the date as number of hours
66
+   * @return The number of hours
67
+   */
68
+  inline uint32_t hour() const {
69
+    return this->minute() / 60;
70
+  }
71
+
72
+  /**
73
+   * @brief Formats the date as number of minutes
74
+   * @return The number of minutes
75
+   */
76
+  inline uint32_t minute() const {
77
+    return this->second() / 60;
78
+  }
79
+
80
+  /**
81
+   * @brief Formats the date as number of seconds
82
+   * @return The number of seconds
83
+   */
84
+  inline uint32_t second() const {
85
+    return this->timestamp;
86
+  }
87
+
88
+  /**
89
+   * @brief Formats the date as a string
90
+   * @details Returns the timestamp formated as a string
91
+   *
92
+   * @param buffer The array pointed to must be able to accommodate 21 bytes
93
+   *
94
+   * String output examples:
95
+   *  123456789012345678901 (strlen)
96
+   *  135y 364d 23h 59m 59s
97
+   *  364d 23h 59m 59s
98
+   *  23h 59m 59s
99
+   *  59m 59s
100
+   *  59s
101
+   *
102
+   */
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
+
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);
115
+  }
116
+};
117
+
118
+#endif // __TIMESTAMP_T__

Loading…
Cancel
Save