jjzjj

c++ - 如何在 C++ 中实现时间本地化?

coder 2024-06-18 原文

我正在使用 time_tctime header 在我的应用程序中获取时间,一切正常。问题是,星期几和月份以英语显示,我希望它们以我的语言(葡萄牙语)显示。我怎么做?

这是我的代码:

time_t tempo = time(NULL);
char *data = ctime(&tempo);
cout << data << endl;

这是实际输出:

"Fri Aug 21 20:00:55 2015"

我是这样想的:

"20:00:55 Sex 21 Ago 2015"

Sex 是周五,Ago 是八月。

我什至用过这个:

setlocale(LC_ALL,".<code_page>");

我的完整代码是这样的(它是葡萄牙语,所以变量名可能很奇怪):

#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <fstream>
#include <locale>
#include <stdlib.h>
void titulo();
int numOrdem(int a);

using namespace std;
using namespace boost;//I'm experimenting with this, and not working...

int main()

{
    int ordem = 0;
    int mesa = 0;
    int turno = 0;
    int quantidade = 0;
    int ordemCheck = 0;
    string anterior = " ";
    string atual;


titulo();

while(true)
{
setlocale(LC_ALL,"");
time_t tempo = time(NULL);
char *data = ctime(&tempo);


minhaOrdem:
cout << "Insira o número da ordem: ";
cin >> ordem;
ordemCheck = numOrdem(ordem);
if (ordemCheck < 7 || ordemCheck > 7)
{
    cout << "Ordem incorreta.\n";
    goto minhaOrdem;
}

/*ofstream arquivo;
arquivo.open("ordersOnly.txt", ios::app);
arquivo << ordem << endl;
arquivo.close();

ifstream ler;
ler.open("ordersOnly.txt");

if(ler)
{
    while(ler >> atual)
    {
        if(atual == anterior)
        {
            cout << "Ordem " << atual << " já requisitada!\n";
            goto minhaOrdem;
        }
     anterior = atual;
    }

} Here I'm trying to catch an duplicated value...*/

cout << "Insira a quantidade de peças: ";
cin >> quantidade;

minhaMesa:
cout << "Insira o número da mesa: ";
cin >> mesa;
if(mesa < 1 || mesa > 6)
{
    cout << "Mesa incorreta.\n";
    goto minhaMesa;
}

meuturno:
cout << "Insira seu turno: ";
cin >> turno;
switch (turno)
    {
    case 100:
    break;
    case 200:
    break;
    case 300:
    break;
    default:{cout << "Turno Incorreto.\n";goto meuturno;}
    }
cout << "\n\n";


arquivo.open("ordensDifipro.txt", ios::app);
arquivo << "Ordem: " << ordem << endl;
arquivo << "Quantidade de peças: " << quantidade << endl;
arquivo << "Mesa: RL" << mesa << endl;
arquivo << "Turno: " << turno << endl;
arquivo << "Data: " << data << endl;
arquivo << endl;
arquivo.close();

}//Main while


}// int main


void titulo()
{
  cout << setw(62) << "Requisitor de Ordens - Difipro - v 1.1\n\n\n\n\n";
}

int numOrdem(int a)
{
    int counter = 0;
 while(a > 0)
    {
    a /= 10;
    counter++;
    }
 return counter;
}

希望对您有所帮助!

最佳答案

你的问题是特定于操作系统的,我关注的是 Linux 和其他 POSIX 操作系统

您可以使用 strptime(3) 解析时间字符串并使用 strftime(3) 将时间转换为字符串, 至少如果时间适合 Unix time_t (1970 年即 Unix Epoch 和 2038 年之间的时间总是如此,因为 Y2038 problem )例如使用 mktime(3) & localtime(3) .阅读locale(7)关于一些本地化和国际化的事情。您可能需要调用 setlocale(3)适本地。

在 C++11 中使用 <chrono> (另见 this );见this time_point example开始。

请注意遥远的时代(例如 1900 年代初期)可能会变得相当复杂,请参阅 this

附录:如果您希望您的日期使用葡萄牙语,请设置您的 LANGLC_ALL环境变量(参见 environ(7) )到 pt_PT.UTF-8 (也许通过在 Linux 上的 export LANG=pt_PT.UTF-8 文件中添加 ~/.bashrc,如果您的登录 shell 是 bash )或替换 setlocale(LC_ALL,"");setlocale(LC_ALL,"pt_PT.UTF-8");在你的程序中。

顺便说一句,Windows 有它的 documentation on setlocale

关于c++ - 如何在 C++ 中实现时间本地化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157471/

有关c++ - 如何在 C++ 中实现时间本地化?的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  3. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  4. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  6. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  7. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  8. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  9. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  10. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

随机推荐