Skip to content

Instantly share code, notes, and snippets.

@jefgen
Created August 4, 2020 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefgen/2d7702e6e70295bd34167e11fde12ed1 to your computer and use it in GitHub Desktop.
Save jefgen/2d7702e6e70295bd34167e11fde12ed1 to your computer and use it in GitHub Desktop.
Getting the Narrow Era Names from ICU
#include "stdio.h"
#include "icu.h"
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <functional>
#include <algorithm>
std::wstring Escape(const std::wstring& s)
{
std::wostringstream out;
std::for_each(s.begin(), s.end(),
[&out](wchar_t ch)
{
if (L' ' <= ch && ch <= 0x7f)
{
out << ch;
}
else
{
out << L"\\u" << std::hex << std::setfill(L'0') << std::setw(4) << static_cast<int>(ch);
}
});
return out.str();
}
int main()
{
UErrorCode status = U_ZERO_ERROR;
UCalendar* jCal = ucal_open(NULL, 0, "ja_JP@calendar=japanese", UCAL_DEFAULT, &status);
if (U_FAILURE(status)) {
printf("FAILED: Line:%d, error: %s\n", __LINE__, u_errorName(status)); return -1;
}
ucal_set(jCal, UCAL_EXTENDED_YEAR, 9999);
int32_t maxEra = ucal_get(jCal, UCAL_ERA, &status);
if (U_FAILURE(status)) {
printf("FAILED: Line:%d, error: %s\n", __LINE__, u_errorName(status)); return -1;
}
printf("max era number = %d\n", maxEra);
UChar pattern[] = u"GGGGG"; // This is the pattern for the Era, with narrow width.
UDateFormat* jUdatFormatter = udat_open(UDAT_PATTERN, UDAT_PATTERN, "ja_JP@calendar=japanese", NULL, 0, pattern, -1, &status);
if (U_FAILURE(status)) {
printf("FAILED: Line:%d, error: %s\n", __LINE__, u_errorName(status)); return -1;
}
for (int i = maxEra; i >= 0; i--)
{
ucal_clear(jCal);
ucal_set(jCal, UCAL_ERA, i);
int32_t eYear = ucal_get(jCal, UCAL_EXTENDED_YEAR, &status);
if (U_FAILURE(status)) {
printf("FAILED: Line:%d, error: %s\n", __LINE__, u_errorName(status)); return -1;
}
printf("era: %d, year: %d\n", i, eYear);
UDate timestamp = ucal_getMillis(jCal, &status);
if (U_FAILURE(status)) {
printf("FAILED: Line:%d, error: %s\n", __LINE__, u_errorName(status)); return -1;
}
printf("era: %d, timestamp: %f\n", i, timestamp);
UChar outputBuffer[256] = {};
int32_t outputLength = udat_format(jUdatFormatter, timestamp, outputBuffer, 256, nullptr, &status);
if (U_FAILURE(status)) {
printf("FAILED: Line:%d, error: %s\n", __LINE__, u_errorName(status)); return -1;
}
std::wstring out = Escape(std::wstring((wchar_t*)outputBuffer));
wprintf(L"Narrow Era Name = '%s'\n", out.c_str());
}
std::cout << "Done.\n";
return 0;
}
/*
The output looks like this:
max era number = 236
era: 236, year: 2019
era: 236, timestamp: 1556694000000.000000
Narrow Era Name = 'R'
era: 235, year: 1989
era: 235, timestamp: 600249600000.000000
Narrow Era Name = 'H'
era: 234, year: 1926
era: 234, timestamp: -1357574400000.000000
Narrow Era Name = 'S'
era: 233, year: 1912
era: 233, timestamp: -1812124800000.000000
Narrow Era Name = 'T'
era: 232, year: 1868
era: 232, timestamp: -3197116052000.000000
Narrow Era Name = 'M'
era: 231, year: 1865
era: 231, timestamp: -3305116052000.000000
Narrow Era Name = '\u6176\u5fdc'
era: 230, year: 1864
era: 230, timestamp: -3340712852000.000000
Narrow Era Name = '\u5143\u6cbb'
era: 229, year: 1861
era: 229, timestamp: -3435407252000.000000
Narrow Era Name = '\u6587\u4e45'
era: 228, year: 1860
era: 228, timestamp: -3464610452000.000000
Narrow Era Name = '\u4e07\u5ef6'
era: 227, year: 1854
...
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment