2013-04-12 50 views
1

我想在不同的語言環境中使用C++打印時間戳,我在一個特定主機上得到了fr_FR的奇怪輸出。C++ strftime%b不能按預期工作

下面是代碼,

#include <time.h>  /* time_t, struct tm, time, localtime, strftime */ 
#include <stdlib.h> 
#include <locale.h> 
#include <clocale> 

#include <iostream> 
using namespace std; 

int main() 
{ 
    time_t rawtime; 
    struct tm * timeinfo; 
     char buffer [32]; 

     setlocale(LC_ALL, ""); 
     cout << "LC_ALL: " << setlocale(LC_ALL, NULL) << endl; 
     cout << "LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl; 


     time (&rawtime); 
     timeinfo = localtime (&rawtime); 

     strftime (buffer,sizeof(buffer),"%a %b %d %H:%M:%S %Y %Z",timeinfo); 
     puts (buffer); 

     return 0; 
} 

運行上面的程序我做

export LANG=fr_FR.ISO8859-1 

它給輸出,

LC_ALL: fr_FR.ISO8859-1 
LC_CTYPE: fr_FR.ISO8859-1 
ven. avril 12 08:49:55 2013 UTC 

它提供了相同的輸出,既%B之前和%B,當我檢查另一臺機器時,它按預期工作。輸出,

LC_ALL: fr_FR.ISO8859-1 
LC_CTYPE: fr_FR.ISO8859-1 
jeu avr 11 23:26:24 2013 PDT 

在一臺機器,

$date +%b 
avr 
$date +%B 
avril 

在problamatic機,

$date +%b 
avril 
$date +%B 
avril 

請幫我解決這個問題。

+0

每臺機器的操作系統是什麼? –

+0

這兩臺機器都是RHEL5。 –

回答

0

輸出顯示正確。

「avril」是「avril」Reference的合法縮寫。

「Avr」也是「avril」的縮寫。法文月份「juin」和「juillet」,均以相同的3個字母開頭,表明使用前3個字母不會總是有效。

C++沒有指定如何縮寫除默認語言環境之外的月份名稱。因此,假設縮寫是簡短的,則在全球範圍內的各種操作系統中的語言環境中相同的長度或相同的長度是有問題的。

建議檢查返回值strftime()