summaryrefslogtreecommitdiff
path: root/lib-common/src/Common/Format.cpp
blob: b56ca2b1a0cee2799216866598791649b52eea58 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright 2017 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Common/Format.h"
#include "I18n/DateFormatter.h"

#include <app_i18n.h>
#include <array>

#define DAY_COUNT   7
#define HOUR_COUNT  24
#define MIN_COUNT   60
#define SEC_COUNT   60

#define TIME_BUFFER_SIZE 64
#define REPEAT_BUFFER_SIZE 256
#define MESSAGE_BUFFER_SIZE 128

namespace
{
	const char *weekdayNames[DAY_COUNT] = {
		"WDS_ALM_TBOPT_SUNDAY",
		"WDS_ALM_TBOPT_MONDAY",
		"WDS_ALM_TBOPT_TUESDAY",
		"WDS_ALM_TBOPT_WEDNESDAY",
		"WDS_ALM_TBOPT_THURSDAY",
		"WDS_ALM_TBOPT_FRIDAY",
		"WDS_ALM_TBOPT_SATURDAY"
	};

	const char *weekdayLetters[DAY_COUNT] = {
		"WDS_ALM_BUTTON_S_M_SUNDAY_ABB",
		"WDS_ALM_BUTTON_M_M_MONDAY_ABB",
		"WDS_ALM_BUTTON_T_M_TUESDAY_ABB",
		"WDS_ALM_BUTTON_W_M_WEDNESDAY_ABB",
		"WDS_ALM_BUTTON_T_M_THURSDAY_ABB",
		"WDS_ALM_BUTTON_F_M_FRIDAY_ABB",
		"WDS_ALM_BUTTON_S_M_SATURDAY_ABB"
	};

	const struct {
		const char *singular;
		const char *plural;
	} durations[] = {
		{ "1_DAY_", "DAYS_" },
		{ "1_HR_",  "HRS_" },
		{ "1_MIN_", "MINS_" }
	};

	std::array<int, 3> getDateDiff(const tm &date)
	{
		time_t now = time(nullptr);
		now -= now % SEC_COUNT;

		int diff = (mktime((tm *) &date) - now) / SEC_COUNT;
		int mins = diff % MIN_COUNT;
		int hours = (diff / MIN_COUNT) % HOUR_COUNT;
		int days = diff / MIN_COUNT / HOUR_COUNT;

		return { { days, hours, mins } };
	}
}

Utils::Range<const char **> Common::getWeekdayNames()
{
	return { weekdayNames };
}

Utils::Range<const char **> Common::getWeekdayLetters()
{
	static const char *days[DAY_COUNT];
	for (size_t i = 0; i < Utils::count(days); ++i) {
		days[i] = _(weekdayLetters[i]);
	}
	return { days };
}

bool Common::is24HourFormat()
{
	bool is24hour = true;
	system_settings_get_value_bool(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR, &is24hour);
	return is24hour;
}

const char *Common::formatTime(const tm &time)
{
	static char buffer[TIME_BUFFER_SIZE];
	strftime(buffer, sizeof(buffer), is24HourFormat() ? "%H:%M" : "%I:%M %p", &time);
	return buffer;
}

const char *Common::formatTime(const tm &time, int fontSize, const char *fontStyle)
{
	static char buffer[TIME_BUFFER_SIZE];
	if (is24HourFormat()) {
		strftime(buffer, sizeof(buffer), "%H:%M", &time);
	} else {
		char format[TIME_BUFFER_SIZE];
		snprintf(format, sizeof(format), "%%I:%%M<font=Tizen:style=%s font_size=%d> %%p</font>", fontStyle, fontSize);
		strftime(buffer, sizeof(buffer), format, &time);
	}

	return buffer;
}

std::string Common::formatDate(const tm &date)
{
	static I18n::DateFormatter formatter("MMMdEEE");
	return formatter.formatDate(date);
}

std::string Common::formatVerbalDate(const tm &date)
{
	static I18n::DateFormatter formatter("MMMMdEEEE");
	return formatter.formatDate(date);
}

const char *Common::formatRepeat(int repeat)
{
	static std::string buffer;
	auto days = getWeekdayLetters();

	buffer.clear();
	for (int i = 0; i < DAY_COUNT; ++i) {
		if (!buffer.empty()) {
			buffer += " ";
		}
		if (repeat & (1 << i)) {
			buffer += "<match>";
			buffer += days[i];
			buffer += "</match>";
		} else {
			buffer += days[i];
		}
	}
	return buffer.c_str();
}

const char *Common::formatVerbalRepeat(int repeat)
{
	static std::string buffer;
	auto days = getWeekdayNames();

	buffer.clear();
	for (int i = 0; i < DAY_COUNT; ++i) {
		if (repeat & (1 << i)) {
			if (!buffer.empty()) {
				buffer += " ";
			}
			buffer += _(days[i]);
		}
	}

	return buffer.c_str();
}

/*
 * The function selects a translatable string such as:
 *      WDS_ALM_TPOP_ALARM_SET_FOR_1_HR_PD_MINS_FROM_NOW_ABB
 *      WDS_ALM_TPOP_ALARM_SET_FOR_P1SD_HRS_P2SD_MINS_FROM_NOW_ABB
 * by formatting it based on date difference.
 *
 * Translatable strings use "PD" to represent "%d" when plural form (>1)
 * of days, hours or minutes is used.
 *
 * If there are several plurals in the string - positional specifiers are used
 * instead of "%d" such as "%1$d", "%2$d" and "%3$d" which are represented by
 * "P1SD", "P2SD" and "P3SD" respectively.
 */
const char *Common::formatAlarmSetMessage(const tm &date)
{
	auto diffs = getDateDiff(date);
	int args[] = { 0, 0, 0 };
	bool isMultiPlurals = (int(diffs[0] > 1) + int(diffs[1] > 1) + int(diffs[2] > 1)) > 1;

	std::string format("WDS_ALM_TPOP_ALARM_SET_FOR_");
	for (size_t i = 0, j = 0; i < diffs.size(); ++i) {
		auto &duration = durations[i];
		if (diffs[i] > 0) {
			if (diffs[i] > 1) {
				args[j++] = diffs[i];
				if (isMultiPlurals) {
					format += 'P';
					format += j + '0';
					format += "SD_";
				} else {
					format += "PD_";
				}
				format += duration.plural;
			} else {
				format += duration.singular;
			}
		}
	}
	format += "FROM_NOW_ABB";

	static char buffer[MESSAGE_BUFFER_SIZE];
	snprintf(buffer, sizeof(buffer), _(format.c_str()), args[0], args[1], args[2]);
	return buffer;
}