@Honey_moon @ltd bên bên C++ thì mình dùng ntn hả?
học C rồi, nhưng e chưa từng viết hàm lấy TG nên cũng ko hiểu gì, qua C++ gặp nó để tính thời gian sử dụng của 1 thuê bao ĐT, mà ko pít lấy thời gian trong máy
2 người giúp với!
Hàm lấy thời gian trong C++
Xem bài này này:
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
int main() {
std::cout << "currentDateTime()=" << currentDateTime() << std::endl;
getchar(); // wait for keyboard input
}
Output:
currentDateTime()=2012-05-06.21:47:59
1 Like
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
===> cái này là khai báo tstruct theo struct tm hả a?
struct tm: là struct đặc biệt dùng cho TG hả?