-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbTime.cpp
More file actions
134 lines (120 loc) · 2.29 KB
/
fbTime.cpp
File metadata and controls
134 lines (120 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* fbTime
* Time manager
* counts the secounds from midnight
* @author Byron Heads
* @date March 15, 2008
*/
#include "fbTime.h"
/*
* fbTime
* Default constructor, initilizes to current local time
*/
fbTime::fbTime():ticks(0), hour(0), min(0), sec(0)
{
setTicksLocal();
}
/*
* fbTime
* Constructor, sets time to given values
* @param h Hours
* @param m Mins
* @param s Sounds
*/
fbTime::fbTime(int h, int m, int s):ticks(0), hour(0), min(0), sec(0)
{
setTicks(h,m,s);
}
/*
* fbTime
* Constructor, sets time to give tick values, loaded from file?
* @param _ticks new tick value
*/
fbTime::fbTime(long _ticks):ticks(_ticks), hour(0), min(0), sec(0)
{
update();
}
/*
* fbTime
* constructor, seeds time with tm struct
* @param time, tm struct ref
*/
fbTime::fbTime(tm& time):ticks(0), hour(0), min(0), sec(0)
{
setTicks(time.tm_hour, time.tm_min, time.tm_sec);
}
/*
* fbTime
* constructor, copies an existing time structures tick value
* @param time, fbTime to copy
*/
fbTime::fbTime(fbTime& time):ticks(time.ticks), hour(0), min(0), sec(0)
{
update();
}
/*
* setTicks
* Sets the ticks to given time
* @param h Hours
* @param m Mins
* @param s Sounds
*/
void fbTime::setTicks(int h, int m, int s)
{
ticks = (h * 3600) + (m * 60) + s;
update();
}
/*
* setTicksLocal
* Sets the ticks to the current local time
*/
void fbTime::setTicksLocal()
{
time_t raw = 0;
time(&raw);
tm time; // = *localtime(&raw);
#ifndef Win32
localtime_r(&raw, &time);
#else
//need to be changed in windows!!
time = *localtime(&raw);
#endif
setTicks(time.tm_hour, time.tm_min, time.tm_sec);
}
/*
* update
* recalculates time based on ticks
* call it after anythime ticks is changed!!
*/
void fbTime::update()
{
ticks %= 86400;
hour = ticks / 3600;
sec = ticks % 60;
min = (ticks - (hour * 3600) - sec) / 60;
}
void fbTime::hms(string& t)
{
t += (hour < 10 ? "0" : "");
ltostr(hour, t);
t += ":";
t += (min < 10 ? "0" : "");
ltostr(min, t);
t += ":";
t += (sec < 10 ? "0" : "");
ltostr(sec, t);
}
void fbTime::hm(string& t)
{
t += (hour < 10 ? "0" : "");
ltostr(hour, t);
t += ":";
t += (min < 10 ? "0" : "");
ltostr(min, t);
}
void fbTime::ltostr(long val, string& str)
{
char buff[20];
snprintf(buff, sizeof(buff)-1, "%ld", val);
str += buff;
}