-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepochToDatetime.cpp
More file actions
48 lines (44 loc) · 1.33 KB
/
Copy pathepochToDatetime.cpp
File metadata and controls
48 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Q . Converting the epoch timestamp to datetime timestamp
Earlier thought of creating a calendar function doing from scratch , it would be larger
so optimised it using preimplimented functions and libraries .
Author: om khard
*/
#include <bits/stdc++.h>
using namespace std;
// these function are unused and would be implimented on for a larger solution
double secondsToMinutes(int sec){
return ((double)sec/60);
}
double minutesToHours(double min){
return (min/60);
}
double hoursToDays(double hrs){
return (hrs/24);
}
double calculateDaysFromEpoch(int epoch){
return hoursToDays(minutesToHours(secondsToMinutes(epoch)));
}
// Doing with Premimplimented library time.h
struct tm* getDateTimeFromEpoch(time_t epoch){
struct tm* timeinfo;
timeinfo = localtime(&epoch);
return timeinfo;
}
string formatDateTime(struct tm* timeinfo){
char buf[80];
strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",timeinfo);
string formatedDateTime = buf;
return formatedDateTime;
}
int main(){
int epoch = 1000; //seconds
// epoch starts from 1-Jan-1970
double days = calculateDaysFromEpoch(epoch);
std::cout<<days<<" days from epoch"<<endl;
time_t timefromEpoch = (time_t)epoch;
struct tm* timeinfo= getDateTimeFromEpoch(timefromEpoch);
string format = formatDateTime(timeinfo);
std::cout<<"Datetime(epoch:"<<epoch<<"):"<<format;
return 1;
}