Pebble Foundation Classes  0.2.0
C++ for Pebble
pebble_time.hpp
1 #pragma once
2 
3 namespace Pebble {
4 
5 enum DayOfWeek
6 {
7  Sunday,
8  Monday,
9  Tuesday,
10  Wednesday,
11  Thursday,
12  Friday,
13  Saturday
14 };
15 
16 class TimeSpan
17 {
18 public:
19  TimeSpan(int seconds) : TimeSpan(0, 0, seconds) { }
20  TimeSpan(int hours, int minutes, int seconds) : TimeSpan(0, hours, minutes, seconds) { }
21  TimeSpan(int days, int hours, int minutes, int seconds) {
22  seconds_ = seconds % 60;
23  minutes += (seconds / 60);
24  minutes_ = minutes % 60;
25  hours += (minutes / 60);
26  hours_ = hours % 24;
27  days_ = days + (hours / 24);
28  }
29  inline int Days() { return days_; }
30  inline int Hours() { return hours_; }
31  inline int Minutes() { return minutes_; }
32  inline int Seconds() { return seconds_; }
33 protected:
34  int days_;
35  int hours_;
36  int minutes_;
37  int seconds_;
38 };
39 
40 class DateTime
41 {
42 public:
43  DateTime(time_t timestamp) : date_(*localtime(&timestamp))
44  {
45  }
46 
47  DateTime(const tm& tm) : date_(tm)
48  {
49  }
50 
51  DateTime(int year, int month, int day) : DateTime(year, month, day, 0, 0, 0)
52  {
53  }
54 
55  DateTime(int year, int month, int day, int hour, int minute, int second)
56  {
57  date_.tm_year = year-1900;
58  date_.tm_mon = month-1;
59  date_.tm_mday = day;
60  date_.tm_hour = hour;
61  date_.tm_min = minute;
62  date_.tm_sec = second;
63  Normalize();
64  }
65 
66  operator const ::tm*() const { return &date_; }
67 
68  inline int GetDay() const { return date_.tm_mday; }
69  inline int GetMonth() const { return date_.tm_mon; }
70  inline int GetYear() const { return date_.tm_year+1900; }
71  inline int GetHour() const { return date_.tm_hour; }
72  inline int GetMinute() const { return date_.tm_min; }
73  inline int GetSecond() const { return date_.tm_sec; }
74  inline DayOfWeek GetDayOfWeek() const { return (DayOfWeek)date_.tm_wday; }
75  inline time_t GetTimestamp() const { return mktime(const_cast<tm*>(&date_)); }
76 
77  inline DateTime AddYears(int years) const { return DateTime(GetYear()+years, GetMonth(), GetDay(), GetHour(), GetMinute(), GetSecond()); }
78  inline DateTime AddMonths(int months) const { return DateTime(GetYear(), GetMonth()+months, GetDay(), GetHour(), GetMinute(), GetSecond()); }
79  inline DateTime AddDays(int days) const { return DateTime(GetYear(), GetMonth(), GetDay()+days, GetHour(), GetMinute(), GetSecond()); }
80  inline DateTime AddHours(int hours) const { return DateTime(GetYear(), GetMonth(), GetDay(), GetHour()+hours, GetMinute(), GetSecond()); }
81  inline DateTime AddMinutes(int minutes) const { return DateTime(GetYear(), GetMonth(), GetDay(), GetHour(), GetMinute()+minutes, GetSecond()); }
82  inline DateTime AddSeconds(int seconds) const { return DateTime(GetYear(), GetMonth(), GetDay(), GetHour(), GetMinute(), GetSecond()+seconds); }
83 
84  inline size_t Format(char* s, size_t max, const char* fmt) const { return strftime(s, max, fmt, &date_); }
85 
86  static inline DateTime Now() { return DateTime(time(NULL)); }
87 
88 protected:
89  inline void Normalize()
90  {
91  time_t timestamp = GetTimestamp();
92  date_ = *localtime(&timestamp);
93  }
94 
95  tm date_;
96 };
97 
98 }
time
time_t time(time_t *tloc) noexcept
localtime
struct tm * localtime(const time_t *timep)
tm::tm_min
int tm_min
Definition: pebble.h:8313
mktime
time_t mktime(struct tm *tb)
tm::tm_wday
int tm_wday
Definition: pebble.h:8318
tm::tm_sec
int tm_sec
Definition: pebble.h:8312
tm::tm_mon
int tm_mon
Definition: pebble.h:8316
tm::tm_mday
int tm_mday
Definition: pebble.h:8315
tm::tm_hour
int tm_hour
Definition: pebble.h:8314
Pebble::TimeSpan
Definition: pebble_time.hpp:16
tm
Definition: pebble.h:8311
tm::tm_year
int tm_year
Definition: pebble.h:8317
strftime
int strftime(char *s, size_t maxsize, const char *format, const struct tm *tm_p)
Pebble::DateTime
Definition: pebble_time.hpp:40