我有这样的事情:
char *current_day, *current_time;
system("date +%F");
system("date +%T");
Run Code Online (Sandbox Code Playgroud)
它在stdout中打印当前日期和时间,但我想获取此输出或将它们分配给current_day和current_time变量,以便稍后我可以使用这些值进行一些处理.
current_day ==> current day
current_time ==> current time
Run Code Online (Sandbox Code Playgroud)
我现在能想到的唯一解决方案是将输出定向到某个文件,然后读取文件,然后将日期和时间的值分配给current_day和current_time.但我认为这不是一个好方法.还有其他短而优雅的方式吗?
我正在使用time.hC中的文件来帮助我们处理时间/日期功能.
我碰到:
struct tm * _Cdecl localtime(const time_t *__timer);
Run Code Online (Sandbox Code Playgroud)
...似乎返回指向tm结构的指针.我发现按地址返回主要用于返回新的内存分配.
如果是这样,上面的返回实际上是如何工作的(a的返回地址struct tm).返回的对象是在某处定义的吗?
谢谢
在尝试构建一个对延迟敏感的应用程序时,需要每秒发送100条消息,每条消息都有时间字段,我们要考虑优化gettimeofday.首先想到的是rdtsc基于优化.有什么想法吗 ?还有其他指针吗?返回的时间值所需的准确度以毫秒为单位,但如果该值偶尔与接收器不同步1-2毫秒,则不是很大.试图比62纳秒的gettimeofday做得更好
在C++ 11中是否有一种简单的方法可以使用与正在使用的ostream相关联的语言环境的相应格式规则来打印当前的挂起时间?
我真正想做的是这样的事情:
myStream << std::chrono::system_clock::now();
Run Code Online (Sandbox Code Playgroud)
并根据与之相关的任何区域设置打印日期和时间myStream.C++ 11提供put_time,但它需要格式化字符串,我希望格式由与流关联的语言环境确定.还有time_put和time_put_byname,但基于在cppreference.com的例子中,这些功能配合使用put_time.
有没有简单的方法来打印时间点值而不手动格式化它?
目前我使用此代码
string now() {
time_t t = time(0);
char buffer[9] = {0};
strftime(buffer, 9, "%H:%M:%S", localtime(&t));
return string(buffer);
}
Run Code Online (Sandbox Code Playgroud)
格式化时间.我需要添加毫秒,因此输出的格式为:16:56:12.321
如何获得当前日期d/m/y.例如,我需要他们有3个不同的变量而不是一个day=d; month=m; year=y;.
如何在C++中以秒为单位获取系统的当前日期时间?
我试过这个:
struct tm mytm = { 0 };
time_t result;
result = mktime(&mytm);
printf("%lld\n", (long long) result);
Run Code Online (Sandbox Code Playgroud)
但我得到了:-1?
我有一个问题,用毫秒来获得实际的系统时间.我发现的唯一一个好方法是Windows.h,但我不能使用它.我应该用std::chrono.我怎样才能做到这一点?
我花了很多时间试图谷歌它,但我发现只有第二精度的例子.
我想要得到这样的字符串:
[2014-11-25 22:15:38:449]
Run Code Online (Sandbox Code Playgroud) 我一直在尝试用 C++ 获取当前日期一段时间,但我不知道我做错了什么。我查看了几个站点,我实现的所有解决方案都收到一个错误,指出 \xe2\x80\x9cThis 函数或变量可能不安全。考虑使用 localtime_s 代替。\xe2\x80\x9d 我尝试了此处找到的几种解决方案(包括下面的解决方案),但我无法让其中任何一个工作。我究竟做错了什么?
\n\n#include <iostream>\n#include <iomanip>\n#include <string>\n#include <ctime>\n\nusing namespace std;\n\nint main()\n{\n\n const int SALARY = 18;\n const int COMMISSION = .08;\n const int BONUS = .03;\n\n int monthlySales;\n int appointmentNumber;\n\n time_t t = time(0); // get time now\n struct tm * now = localtime(&t);\n\n string name;\n\n\n//this is where the user adds their name and date\n cout << "Please enter the sales representative\'s name: ";\n cin >> name;\n cout << "Please enter the …Run Code Online (Sandbox Code Playgroud) 是否有更多面向对象的替代方法在Linux上使用C++中的gettimeofday()?我喜欢例如能够编写类似于此的代码:
DateTime now = new DateTime;
DateTime duration = new DateTime(2300, DateTime.MILLISECONDS)
DateTime deadline = now + duration;
while(now < deadline){
DoSomething();
delete now;
now = new DateTime()
}
Run Code Online (Sandbox Code Playgroud)
目标是一个嵌入式Linux系统,没有Boost库,但也许有一些容易移植的东西(例如仅使用头文件实现的东西).
C++ 中是否有任何日期时间模块?
我在谷歌上研究了一下,开始了解<ctime>头文件,但它只是超出了我的头脑。
还有什么可以做我的工作吗?
我一直在使用Windows的GetTickCount(),但我读过它的性能/分辨率很差,我想问一下最好的方法是什么.
我尝试使用<ctime>库,但它不会用于毫秒或微秒,我需要达到这个精度的东西.
谢谢!