sukovanej
sukovanej

Reputation: 688

C++ converting datetime to timestamp

I know these values

unsigned char year = 17; // means 2017
unsigned char month = 8;
unsigned char day = 25;
unsigned char hour = 14;
unsigned char minute = 23;
unsigned char second = 54; 

How can I convert these into unix time stamp? I'm not sure if the unsigned char is the proper way to represent the value, I just need each value to be a 1byte in size.

Upvotes: 1

Views: 15762

Answers (4)

Nguyen Si Anh
Nguyen Si Anh

Reputation: 24

#include <iostream>
#include <string>
#include <chrono>
#include <sstream>
#include <iomanip>

const std::string timeFormat{"%Y%m%d %H:%M:%S"};

int64_t getTimeStamp(const std::string& timeString)
{
    std::istringstream m_istream{timeString};
    std::tm m_tm { 0 };
    std::time_t m_timet { 0 };
    m_istream >> std::get_time(&m_tm, timeFormat.c_str());
    m_timet = std::mktime(&m_tm);
    m_timet *= 1000; // convert to milliseconds
    return m_timet;
}

int main() {

    const std::string timeString{ "20220509 10:07:38" };
    int64_t result = getTimeStamp(timeString);
    std::cout << "result:" << result << std::endl;

    return 0;
}

Upvotes: 0

Howard Hinnant
Howard Hinnant

Reputation: 219395

Perhaps the easiest and highest performance way is to use Howard Hinnant's free, open-source, header-only datetime library:

#include "date.h"
#include <iostream>

date::sys_seconds
to_sys_time(unsigned char y, unsigned char m, unsigned char d,
            unsigned char h, unsigned char M, unsigned char s)
{
    using namespace date;
    using namespace std::chrono;
    return sys_days{year{y+2000}/m/d} + hours{h} + minutes{M} + seconds{s};
}

int
main()
{
    std::cout << to_sys_time(17, 9, 25, 14, 23, 54).time_since_epoch().count() << '\n';
}

This outputs:

1503671034

This library extends the <chrono> library to handle calendrical computations, and is even being proposed for standardization.

Upvotes: 4

Kaveh Vahedipour
Kaveh Vahedipour

Reputation: 3477

That's how I'd do it embracing a little more of c++ 11.

std::string timepointToString(std::chrono::system_clock::time_point const& t) {
  time_t tt = std::chrono::system_clock::to_time_t(t);
  struct tm tb;
  size_t const len(21);
  char buffer[len];
  TRI_gmtime(tt, &tb);
  ::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &tb);
  return std::string(buffer, len - 1);
}

std::chrono::system_clock::time_point stringToTimepoint(std::string const& s) {
  if (!s.empty()) {
    try {
      std::tm tt;
      tt.tm_year = std::stoi(s.substr(0, 4)) - 1900;
      tt.tm_mon = std::stoi(s.substr(5, 2)) - 1;
      tt.tm_mday = std::stoi(s.substr(8, 2));
      tt.tm_hour = std::stoi(s.substr(11, 2));
      tt.tm_min = std::stoi(s.substr(14, 2));
      tt.tm_sec = std::stoi(s.substr(17, 2));
      tt.tm_isdst = 0;
      auto time_c = TRI_timegm(&tt);
      return std::chrono::system_clock::from_time_t(time_c);
    } catch (...) {}
  }
  return std::chrono::time_point<std::chrono::system_clock>();
}

Upvotes: 0

schorsch312
schorsch312

Reputation: 5714

Ubervan answered your question

Break the date down into its components i.e. day, month, year, then:

struct tm  tm;
time_t rawtime;
time ( &rawtime );
tm = *localtime ( &rawtime );
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
mktime(&tm);

tm can now be converted to a time_t and be manipulated.

Your problem is also addressed here.

Upvotes: 6

Related Questions