Last active
June 1, 2017 08:34
-
-
Save wuthmone/dad7fab89ef42d438456e75cc23878a2 to your computer and use it in GitHub Desktop.
C++ Method to convert from Time t to String.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> // for std::rand | |
#include <iomanip> | |
#include <time.h> | |
#include <stdio.h> | |
#include <ctime> | |
#include <string> | |
#include <sstream> | |
#include <iostream> | |
using namespace std; | |
int main(int argc, char const *argv[]) | |
{ | |
string time, tm_year, tm_mday, tm_mon, tm_hour, tm_min, tm_sec; | |
time= "20172203122023"; | |
struct tm tm; | |
tm_year = time.substr(0,4); | |
tm_mday = time.substr(4,2); | |
tm_mon = time.substr(6,2); | |
tm_hour = time.substr(8,2); | |
tm_min = time.substr(10,2); | |
tm_sec = time.substr(12,2); | |
// insert the value to tm struct | |
tm.tm_year = stoi(tm_year) - 1900; | |
tm.tm_mon = stoi(tm_mon) -1; | |
tm.tm_mday = stoi(tm_mday); | |
tm.tm_min = stoi(tm_min); | |
tm.tm_hour = stoi(tm_hour); | |
tm.tm_sec = stoi(tm_sec); | |
tm.tm_isdst = -1; | |
cout <<"Year:" << tm.tm_year + 1900 << "\nMonth:" << tm.tm_mon + 1<< endl; | |
cout <<"Day:" << tm.tm_mday << "\nHour:" << tm.tm_hour<< endl; | |
cout <<"Min:" << tm.tm_min << "\nSecond:"<< tm.tm_sec<<endl; | |
time_t tStart = mktime(&tm); | |
char buff[20]; | |
char buff1[20]; | |
//time_t now = time(NULL); | |
strftime(buff, 20, "%Y/%m/%d %H:%M", localtime(&tStart)); | |
strftime(buff1, 20, "%Y%m%d", localtime(&tStart)); | |
puts(buff); | |
puts(buff1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment