File: time_string.cc

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (199 lines) | stat: -rw-r--r-- 7,213 bytes parent folder | download | duplicates (2)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright (C) 2008-2009 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include "base/time_string.h"

#include <cassert>
#include <ctime>
#include <string>

#include <stdint.h>

#include "base/i18n.h"

namespace {
char timestring_buffer[] =
   "YYYY-MM-DDThh.mm.ss";  //  ':' is not a valid file name character for FAT FS
char gamestringbuffer[] = "000:00:00";
}  // namespace

char* timestring() {
	time_t t;
	time(&t);
	struct tm const* const t_components = localtime(&t);
	{
		uint16_t year = 1900 + t_components->tm_year;  //  years start at 1900
		assert(year < 9999);
		timestring_buffer[0] = '0' + year / 1000;
		year %= 1000;
		timestring_buffer[1] = '0' + year / 100;
		year %= 100;
		timestring_buffer[2] = '0' + year / 10;
		year %= 10;
		timestring_buffer[3] = '0' + year;
	}
	{
		uint16_t mon = 1 + t_components->tm_mon;  //  months start at 0
		assert(mon <= 99);
		timestring_buffer[5] = '0' + mon / 10;
		mon %= 10;
		timestring_buffer[6] = '0' + mon;
	}
	{
		uint16_t mday = t_components->tm_mday;
		assert(mday <= 99);
		timestring_buffer[8] = '0' + mday / 10;
		mday %= 10;
		timestring_buffer[9] = '0' + mday;
	}
	{
		uint16_t hour = t_components->tm_hour;
		assert(hour <= 99);
		timestring_buffer[11] = '0' + hour / 10;
		hour %= 10;
		timestring_buffer[12] = '0' + hour;
	}
	{
		uint16_t min = t_components->tm_min;
		assert(min <= 99);
		timestring_buffer[14] = '0' + min / 10;
		min %= 10;
		timestring_buffer[15] = '0' + min;
	}
	{
		uint16_t sec = t_components->tm_sec;
		assert(sec <= 99);
		timestring_buffer[17] = '0' + sec / 10;
		sec %= 10;
		timestring_buffer[18] = '0' + sec;
	}
	return timestring_buffer;
}

std::string localize_month(int8_t month) {
	switch (month) {
	case 0:
		/** TRANSLATORS: January. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Jan");
	case 1:
		/** TRANSLATORS: February. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Feb");
	case 2:
		/** TRANSLATORS: March. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Mar");
	case 3:
		/** TRANSLATORS: April. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Apr");
	case 4:
		/** TRANSLATORS: May. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("May");
	case 5:
		/** TRANSLATORS: June. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Jun");
	case 6:
		/** TRANSLATORS: July. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Jul");
	case 7:
		/** TRANSLATORS: August. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Aug");
	case 8:
		/** TRANSLATORS: September. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Sep");
	case 9:
		/** TRANSLATORS: October. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Oct");
	case 10:
		/** TRANSLATORS: November. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		/** TRANSLATORS: http://www.unicode.org/cldr/charts/latest/summary/root.html */
		return _("Nov");
	case 11:
		/** TRANSLATORS: December. Keep this to 4 letters maximum. */
		/** TRANSLATORS: Use the "Months - abbreviated - Formatting" definition from the CLDR */
		/** TRANSLATORS: definition if there is one for your language. */
		return _("Dec");
	default:
		return std::to_string(month);
	}
}

char* gamestring_with_leading_zeros(uint32_t gametime) {
	uint32_t time = gametime / 1000;
	gamestringbuffer[8] = '0' + time % 10;
	gamestringbuffer[7] = '0' + (time /= 10) % 6;
	gamestringbuffer[5] = '0' + (time /= 6) % 10;
	gamestringbuffer[4] = '0' + (time /= 10) % 6;
	gamestringbuffer[2] = '0' + (time /= 6) % 10;
	gamestringbuffer[1] = '0' + (time /= 10) % 10;
	gamestringbuffer[0] = '0' + (time /= 10);
	return gamestringbuffer;
}

std::string gametimestring(uint32_t gametime, bool show_seconds) {
	// update buffer
	std::string result = gamestring_with_leading_zeros(gametime);
	if (!show_seconds) {
		result = result.substr(0, result.size() - 3);
	}

	// remove leading 0s
	int i = 0;
	while (result.at(i) == '0')
		++i;
	if (result.at(i) == ':') {
		if (show_seconds) {
			++i;
		} else {
			--i;
		}
	}

	return result.substr(i);
}