Back to index

texmacs  1.0.7.15
win32_get_date.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : win32_get_date.cpp
00004 * DESCRIPTION: return formatted date under Windows
00005 * COPYRIGHT  : (C) 2009  David Michel
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #if defined(__MINGW__) || defined(__MINGW32__) || defined(OS_WIN32)
00013 #include <sstream>
00014 
00015 #include "analyze.hpp"
00016 #include "hyphenate.hpp"
00017 #include "impl_language.hpp"
00018 #include "sys_utils.hpp"
00019 #include "converter.hpp"
00020 
00021 namespace win32 {
00022   #include <windows.h>
00023   #include <wchar.h>
00024 
00025   const int MAX_UTF8_LEN = 3;
00026 
00027   WORD
00028   language_to_LCID (string s) {
00029     if (s == "american") return MAKELCID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
00030     if (s == "british") return MAKELCID(LANG_ENGLISH, SUBLANG_ENGLISH_UK);
00031     if (s == "bulgarian") return MAKELCID(LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA);
00032     if (s == "chinese") return MAKELCID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
00033     if (s == "czech") return MAKELCID(LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC);
00034     if (s == "danish") return MAKELCID(LANG_DANISH, SUBLANG_DANISH_DENMARK);
00035     if (s == "dutch") return MAKELCID(LANG_DUTCH, SUBLANG_DUTCH);
00036     if (s == "english") return MAKELCID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
00037     if (s == "finnish") return MAKELCID(LANG_FINNISH, SUBLANG_FINNISH_FINLAND);
00038     if (s == "french") return MAKELCID(LANG_FRENCH, SUBLANG_FRENCH);
00039     if (s == "german") return MAKELCID(LANG_GERMAN, SUBLANG_GERMAN);
00040     if (s == "hungarian") return MAKELCID(LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY);
00041     if (s == "italian") return MAKELCID(LANG_ITALIAN, SUBLANG_ITALIAN);
00042     if (s == "japanese") return MAKELCID(LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN);
00043     if (s == "korean") return MAKELCID(LANG_KOREAN, SUBLANG_KOREAN);
00044     if (s == "polish") return MAKELCID(LANG_POLISH, SUBLANG_POLISH_POLAND);
00045     if (s == "portuguese") return MAKELCID(LANG_PORTUGUESE, SUBLANG_PORTUGUESE);
00046     if (s == "romanian") return MAKELCID(LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA);
00047     if (s == "russian") return MAKELCID(LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA);
00048     if (s == "slovene") return MAKELCID(LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA);
00049     if (s == "spanish") return MAKELCID(LANG_SPANISH, SUBLANG_SPANISH);
00050     if (s == "swedish") return MAKELCID(LANG_SWEDISH, SUBLANG_SWEDISH);
00051     if (s == "taiwanese") return MAKELCID(LANG_THAI, SUBLANG_THAI_THAILAND);
00052     if (s == "ukrainian") return MAKELCID(LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE);
00053     return MAKELCID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
00054   }
00055 
00056   int
00057   WCHAR_to_utf8 (char* uc, WCHAR wc)
00058   {
00059     if (wc < 0x80) {
00060       uc[0] = wc;
00061       return 1;
00062     } else if (wc < 0x800) {
00063       uc[0] = (wc >> 6) | 0xc0;
00064       uc[1] = (wc & 0x3f) | 0x80;
00065       return 2;
00066     } else {
00067       uc[0] = (wc >> 12) | 0xe0;
00068       uc[1] = ((wc >> 6) & 0x3f) | 0x80;
00069       uc[2] = (wc & 0x3f) | 0x80;
00070       return 3;
00071     }
00072   }
00073 
00074   string
00075   WCHARP_to_string (WCHAR* wcs) {
00076     int size = wcslen(wcs);
00077     char *cs = tm_new_array<char>(MAX_UTF8_LEN * size);
00078     int j = 0;
00079     for (int i = 0; i < size; ++i) {
00080       int t = WCHAR_to_utf8(&cs[j], wcs[i]);
00081       if (t !=  -1) j += t;
00082       else cs[j++] = '?';
00083     }
00084     cs[j] = 0;
00085     string res = cs;
00086     tm_delete_array(cs);
00087     return utf8_to_cork(res);
00088   }
00089 
00090   WCHAR*
00091   string_to_WCHARP (string s) {
00092     WCHAR* wcs = tm_new_array<WCHAR>(N(s) + 1);
00093     char* cs = as_charp(cork_to_utf8(s));
00094     int t, i = 0, j = 0;
00095     while (t = mbtowc(&wcs[j], &cs[i], MB_CUR_MAX)) {
00096       if (t != -1) {
00097         j++;
00098         i += t;
00099       } else {
00100         wcs[j] = L'?';
00101         wcs[j + 1] = 0;
00102         tm_delete_array(cs);
00103         return wcs;
00104       }
00105     }
00106     wcs[j] = 0;
00107     tm_delete_array(cs);
00108     return wcs;
00109   }
00110 
00111   string
00112   get_date (string lan, string fm) {
00113     SYSTEMTIME localtime;
00114     GetLocalTime(&localtime);
00115     if (fm == "") {
00116       if ((lan == "british") || (lan == "english") || (lan == "american"))
00117         fm = "MMMM d, yyyy";
00118       else if (lan == "german")
00119         fm = "d. MMMM yyyy";
00120       else if (lan == "chinese" || lan == "japanese" ||
00121                lan == "korean" || lan == "taiwanese")
00122       {
00123         string y = as_string(localtime.wYear);
00124         string m = as_string(localtime.wMonth);
00125         string d = as_string(localtime.wDay);
00126         if (lan == "japanese")
00127           return y * "<#5e74>" * m * "<#6708>" * d * "<#65e5>";
00128         if (lan == "korean")
00129           return y * "<#b144> " * m * "<#c6d4> " * d * "<#c77c>";
00130         return y * "," * m * "," * d;
00131       }
00132       else fm = "d MMMM yyyy";
00133     }
00134     string loc = language_to_locale(lan);
00135     string old = get_env("LANG");
00136     set_env("LANG", loc);
00137     WCHAR* format = string_to_WCHARP(fm);
00138     int size = 1 + GetDateFormatW(language_to_LCID(lan), 0, &localtime, format, 0, 0);
00139     WCHAR* wcdate = tm_new_array<WCHAR>(size);
00140     GetDateFormatW(language_to_LCID(lan), 0, &localtime, format, wcdate, size);
00141     tm_delete_array(format);
00142     string date = WCHARP_to_string(wcdate);
00143     tm_delete_array(wcdate);
00144     set_env("LANG", old);
00145     return date;
00146   }
00147 }
00148 #endif
00149