4 Haziran 2018 Pazartesi

std::setlocale metodu

C
Şu satırı dahil ederiz.
#include <locale.h>
Örnek
Şöyle yaparız.
char *loc = setlocale(LC_ALL, "");
printf("Locale: %s\n", loc);
Çıktı olarak şunu alırız.
Locale: en_US.UTF-8
Örnek
Şöyle yaparız.
printf("%'d\n", 1000000000);
printf("Setting LC_ALL to %s\n", getenv("LANG"));
setlocale(LC_ALL, ""); // Set user-preferred locale.
printf("%'d\n", 1000000000);
Çıktı olarak şunu alırız.
1000000000
Setting LC_ALL to en_US.UTF-8
1,000,000,000
C++
Örnek
C locale'inden işletim sisteminin seçili locale'ine geçmek için şöyle yaparız.
std::setlocale(LC_ALL,"");
Örnek
Belli bir ülkenin locale'ini kullanmak için şöyle yaparız. Bu örnek Windows'ta çalışıyor.
setlocale(LC_ALL, "Russian");

31 Mayıs 2018 Perşembe

std::wcout

Giriş
Açıklaması şöyle.
The Windows Console does not support UTF-16 output. It only supports 8-bit output, and it has partial support for 8-bit MBCS, such as Big5 or UTF-8.

To display Unicode characters on the console you will need to do conversion to UTF-8 or another MBCS in your code, and also put the console into UTF-8 mode (which requires an undocumented system call).
Dolayısıyla her wchar_t karakterini gösteremez. Bazı karakterin gösterilmediğini görmek için şöyle yaparız.
#include <cstring>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
  wchar_t c[] = L"olé";
  wchar_t d[] = L"abc";
  wcout << c << endl;
  wcout << d << endl;

  return 0;
}
Unicode Karakter Göstermek
Windows'ta şöyle yapılır
#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
    _setmode(_fileno(stdout), _O_U16TEXT);
    std::wcout << L"—";
}


29 Mayıs 2018 Salı

std::get_time metodu

Giriş
Şu satırı dahil ederiz.
#include <iomanip>
Broken time yani std::tm yapısını stream'den okumak için kullanılır. Yani bu işlem sonunda tm değişkeni doldurulur. Bu sınıfın tam tersi işlemini std::put_time ile yaparız.

std::get_time C++11 ile gelen yeni bir metod.  std::get_time yerine hep strptime() metodunu kullanıyorduk.  Ancak bu metod Windows'ta mevcut değildi, dolayısıyla kod portable olmuyordu.

%d - day
/%m - month
/%Y - year
%H- hour
%M - minute
%S - second

Gün - Ay - Yıl - Saat - Dakika - Saniye
Şöyle yaparız.
struct tm          t = {};
std::string        dateTimeFormat{"%d/%m/%Y %H:%M:%S"};
std::istringstream ss("22/12/2016 01:12:10");

ss >> std::get_time( &t, dateTimeFormat.c_str() );
Yıl - Ay- Gün
Şöyle yaparız.
std::tm t = {};
std::istringstream ss ("2015-11-27");
if (ss >> std::get_time (&t, "%Y-%m-%d")) {...}
Yıl - Ay - Gün - Saat - Dakika - Saniye
Örnek
Şöyle yaparız. Herhangi bir doğrulama kontrolü yapılmaz.
std::string str = "2016-06-31 02:00:58"; // June 31 2016 does not exist
std::istringstream ss (str);
struct tm t = {}
if (ss >> std::get_time (&t, "%Y-%m-%d %H:%M:%S")) {...}
Örnek
Şöyle yaparız.
std::istringstream time_stream = ...;  // Construct a stream from string
std::tm time;  // Time structure
time_stream >> std::get_time(&time, "%Y-%m-%d %H:%M:%S");  // Fill time struct
Yıl - Ay - Gün - T - Saat - Dakika - Saniye
Elimizde UTC bir zaman olsun. Şöyle yaparız.
std::tm t = {};
std::istringstream ss ("2010-11-04T23:23:01Z");
if (ss >> std::get_time (&t, "%Y-%m-%dT%H:%M:%S")) {...}
Saat - Dakika - Saniye
Şöyle yaparız.
std::tm t{};
std::istringstream ss("01:02:03");
ss.imbue(std::locale("de_DE"));
ss >> std::get_time(&t, "%H:%M:%S");
std::cout << ss.fail() << std::endl;
std::cout << std::put_time(&t, "%c") << '\n';
Çıktı olarak şunu alırız.
1
Sun Jan  0 00:00:00 1900
tm yapısından chrono'ya geçiş
Chrono ile gelen from_time_t() metodunu kullanırız. Şöyle yaparız.
std::tm tm;
std::stringstream ss("Jan 9 2014 12:35:34");
ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));




21 Mayıs 2018 Pazartesi

std::u32string Sınıfı

Giriş
Bu sınıf aslıdna std::basic_string<char32_t> olarak tanımlı. std::wstring'den daha portable