Conversion of C++ time and timestamp

Timestamp to standard time

// timestamp.cpp

#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;

typedef struct times {
    
    
  int Year;
  int Mon;
  int Day;
  int Hour;
  int Min;
  int Second;
} Times;

Times stamp_to_standard(int stampTime) {
    
    
  time_t tick = (time_t)stampTime;
  struct tm tm;
  char s[100];
  Times standard;

  // tick = time(NULL);
  tm = *localtime(&tick);
  strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);
  printf("%d: %s\n", (int)tick, s);

  standard.Year = atoi(s);
  standard.Mon = atoi(s + 5);
  standard.Day = atoi(s + 8);
  standard.Hour = atoi(s + 11);
  standard.Min = atoi(s + 14);
  standard.Second = atoi(s + 17);
  return standard;
}

int main(int argc, const char *argv[]) {
    
    
  long timeStamp = 1549133300623;  // 13位时间戳,精确到毫秒

  stamp_to_standard(timeStamp / 1000);
  return 0;
}

The results of compiling and running are as follows:

patten@patten-hp:~/workspace/xjCollide$ g++ timestamp.cpp -std=c++11
patten@patten-hp:~/workspace/xjCollide$ ./a.out 1549133300:
2019-02-03 02:48:20 patten@patten-hp:~/workspace/xjCollide$

Standard Time to Timestamp

#include <string.h>
#include <iostream>
using namespace std;

long standard_to_stamp(char *str_time) {
    
    
  struct tm stm;
  int iY, iM, iD, iH, iMin, iS;
  memset(&stm, 0, sizeof(stm));
  iY = atoi(str_time);
  iM = atoi(str_time + 5);
  iD = atoi(str_time + 8);
  iH = atoi(str_time + 11);
  iMin = atoi(str_time + 14);
  iS = atoi(str_time + 17);
  stm.tm_year = iY - 1900;
  stm.tm_mon = iM - 1;
  stm.tm_mday = iD;
  stm.tm_hour = iH;
  stm.tm_min = iMin;
  stm.tm_sec = iS;
  printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);

  return (long)mktime(&stm);
}

int main() {
    
    
  char arr[20] = "2019-10-21 16:02:30";
  char *a = arr;
  std::cout << "timeStamp: " << standard_to_stamp(arr) << std::endl;

  return 0;
}

The results of compiling and running are as follows:

patten@patten-hp:~/workspace/others/cpp/demo$ g++ -g -std=c++11
main.cpp patten@patten-hp:~/workspace/others/cpp/demo$ ./a.out
2019-10-21 16:2:30 timeStamp: 1571644950
patten@patten-hp:~/workspace/others/cpp/demo$

The following methods are a summary of data collected from the Internet. For the old methods, problems are prone to occur, such as: using the ftime function, it is not universal
under ndk, and it cannot be compiled (the function is deprecated). The following methods are all This is a more general approach, and I hope it will be helpful to everyone.

method one:

#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
/*
取当前时间,精确到微秒;
*/
int main(int argc, char *argv[])
{
    
    
    auto now = std::chrono::system_clock::now();
    //通过不同精度获取相差的毫秒数
    uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()
        - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
    time_t tt = std::chrono::system_clock::to_time_t(now);
    auto time_tm = localtime(&tt);
    char strTime[25] = {
    
     0 };
    sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d %03d", time_tm->tm_year + 1900,
        time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour,
        time_tm->tm_min, time_tm->tm_sec, (int)dis_millseconds);
    std::cout << strTime << std::endl;
    return 1;
}

Method Two

#include <ctime>
#include <string>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <iostream>
 
// use strftime to format time_t into a "date time"
std::string date_time(std::time_t posix)
{
    
    
    char buf[20]; // big enough for 2015-07-08 10:06:51\0
    std::tm tp = *std::localtime(&posix);
    return {
    
    buf, std::strftime(buf, sizeof(buf), "%F %T", &tp)};
}
 
std::string stamp()
{
    
    
    using namespace std;
    using namespace std::chrono;
 
    // get absolute wall time
    auto now = system_clock::now();
 
    // find the number of milliseconds
    auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
 
    // build output string
    std::ostringstream oss;
    oss.fill('0');
 
    // convert absolute time to time_t seconds
    // and convert to "date time"
    oss << date_time(system_clock::to_time_t(now));
    oss << '.' << setw(3) << ms.count();
 
    return oss.str();
}
 
int main()
{
    
    
    std::cout << stamp() << '\n';
}

Method Three (microseconds)

std::string stamp()
{
    
    
    using namespace std;
    using namespace std::chrono;
 
    auto now = system_clock::now();
 
    // use microseconds % 1000000 now
    auto us = duration_cast<microseconds>(now.time_since_epoch()) % 1000000;
 
    std::ostringstream oss;
    oss.fill('0');
 
    oss << date_time(system_clock::to_time_t(now));
    oss << '.' << setw(6) << us.count();
 
    return oss.str();
}

Guess you like

Origin blog.csdn.net/jiyanghao19/article/details/130408140