我试图在Linux上编写一个打印当前日期的C程序。在巴西,我希望它以dd / mm / yyyy格式打印,在美国,它将以mm / dd / yyyy格式打印。
我认为"%x" strftime的说明符应该是诀窍,但它以mm / dd / yyyy格式打印。
#include <stdio.h>
#include <time.h>
int main(){
struct tm local_now;
{
time_t now;
time(&now);
struct tm *tmp = localtime(&now);
local_now = *tmp;
}
char buf[20];
strftime(buf, sizeof buf, "%x", &local_now);
printf("%s\n", buf);
return 0;
}
这是我从运行locale
命令获得的输出:
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=pt_BR.UTF-8
LC_TIME=pt_BR.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=pt_BR.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=pt_BR.UTF-8
LC_NAME=pt_BR.UTF-8
LC_ADDRESS=pt_BR.UTF-8
LC_TELEPHONE=pt_BR.UTF-8
LC_MEASUREMENT=pt_BR.UTF-8
LC_IDENTIFICATION=pt_BR.UTF-8
LC_ALL=
显然忽略了LC_TIME=pt_BR.UTF-8
设置?
答案 0 :(得分:1)
strftime和所有其他依赖于语言环境的函数默认使用“C”语言环境。要使用用户区域设置,必须在程序初始化期间显式调用setlocale
函数:
setlocale
如果将空字符串传递给function duplicateSpreadSheets() {
var sheetName, sheet, destination, currentDate;
sheetName = "Name of sheet goes here";
sheet = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(sheetName);
destination = SpreadsheetApp.openById("Spreadsheet ID goes here (the first string of nubmers and letters in the url)");
sheet = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("Copy of " + sheetName)
.activate();
currentDate = new Date(Date.now()); //change the format of the date to what you want, DD/MM/YYYY etc.
SpreadsheetApp.getActiveSpreadsheet()
.renameActiveSheet(currentDate)
}
,那么它将根据标准语言环境变量(LC_ALL,LC_TIME等)选择语言环境。