Conversion of UTC timestamp to epoch time in C [duplicate]
This question already has an answer here:
how to convert datetime to unix timestamp in c?
6 answers
I want to convert UTC timestamp in epoch time seconds considering DST in C language. I am doing development on VS 2005 as this is legacy product. I gone through various websites but it is not useful.
We can do this by boost library but it is not recommended.
I am passing input as "2017-03-12T01:00:00Z" i.e. UTC timestamp, and I want epoch time of it i.e. in seconds. When I convert this epoch time back to UTC time, it should give me exact result as input timestamp.
c dst
marked as duplicate by gsamaras, Sander De Dycker, hyde, Jonathan Leffler
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 2 more comments
This question already has an answer here:
how to convert datetime to unix timestamp in c?
6 answers
I want to convert UTC timestamp in epoch time seconds considering DST in C language. I am doing development on VS 2005 as this is legacy product. I gone through various websites but it is not useful.
We can do this by boost library but it is not recommended.
I am passing input as "2017-03-12T01:00:00Z" i.e. UTC timestamp, and I want epoch time of it i.e. in seconds. When I convert this epoch time back to UTC time, it should give me exact result as input timestamp.
c dst
marked as duplicate by gsamaras, Sander De Dycker, hyde, Jonathan Leffler
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
boost? in C? how?
– Sourav Ghosh
Nov 16 '18 at 8:20
@SouravGhosh The product mixed of C and C++.
– Prashant
Nov 16 '18 at 8:22
That's fine, but I never known boost can be used in C.
– Sourav Ghosh
Nov 16 '18 at 8:23
@SouravGhosh All it requires is cdecl wrapper function in C++ ,which can be called from C, and then uses boost.
– hyde
Nov 16 '18 at 8:41
@hyde Technically correct..but is it worth it? Sorry if my statement was not clear earlier. :)
– Sourav Ghosh
Nov 16 '18 at 8:43
|
show 2 more comments
This question already has an answer here:
how to convert datetime to unix timestamp in c?
6 answers
I want to convert UTC timestamp in epoch time seconds considering DST in C language. I am doing development on VS 2005 as this is legacy product. I gone through various websites but it is not useful.
We can do this by boost library but it is not recommended.
I am passing input as "2017-03-12T01:00:00Z" i.e. UTC timestamp, and I want epoch time of it i.e. in seconds. When I convert this epoch time back to UTC time, it should give me exact result as input timestamp.
c dst
This question already has an answer here:
how to convert datetime to unix timestamp in c?
6 answers
I want to convert UTC timestamp in epoch time seconds considering DST in C language. I am doing development on VS 2005 as this is legacy product. I gone through various websites but it is not useful.
We can do this by boost library but it is not recommended.
I am passing input as "2017-03-12T01:00:00Z" i.e. UTC timestamp, and I want epoch time of it i.e. in seconds. When I convert this epoch time back to UTC time, it should give me exact result as input timestamp.
This question already has an answer here:
how to convert datetime to unix timestamp in c?
6 answers
c dst
c dst
edited Nov 16 '18 at 8:39
Lennart
6,167125266
6,167125266
asked Nov 16 '18 at 8:18
PrashantPrashant
11
11
marked as duplicate by gsamaras, Sander De Dycker, hyde, Jonathan Leffler
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by gsamaras, Sander De Dycker, hyde, Jonathan Leffler
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 12:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
boost? in C? how?
– Sourav Ghosh
Nov 16 '18 at 8:20
@SouravGhosh The product mixed of C and C++.
– Prashant
Nov 16 '18 at 8:22
That's fine, but I never known boost can be used in C.
– Sourav Ghosh
Nov 16 '18 at 8:23
@SouravGhosh All it requires is cdecl wrapper function in C++ ,which can be called from C, and then uses boost.
– hyde
Nov 16 '18 at 8:41
@hyde Technically correct..but is it worth it? Sorry if my statement was not clear earlier. :)
– Sourav Ghosh
Nov 16 '18 at 8:43
|
show 2 more comments
boost? in C? how?
– Sourav Ghosh
Nov 16 '18 at 8:20
@SouravGhosh The product mixed of C and C++.
– Prashant
Nov 16 '18 at 8:22
That's fine, but I never known boost can be used in C.
– Sourav Ghosh
Nov 16 '18 at 8:23
@SouravGhosh All it requires is cdecl wrapper function in C++ ,which can be called from C, and then uses boost.
– hyde
Nov 16 '18 at 8:41
@hyde Technically correct..but is it worth it? Sorry if my statement was not clear earlier. :)
– Sourav Ghosh
Nov 16 '18 at 8:43
boost? in C? how?
– Sourav Ghosh
Nov 16 '18 at 8:20
boost? in C? how?
– Sourav Ghosh
Nov 16 '18 at 8:20
@SouravGhosh The product mixed of C and C++.
– Prashant
Nov 16 '18 at 8:22
@SouravGhosh The product mixed of C and C++.
– Prashant
Nov 16 '18 at 8:22
That's fine, but I never known boost can be used in C.
– Sourav Ghosh
Nov 16 '18 at 8:23
That's fine, but I never known boost can be used in C.
– Sourav Ghosh
Nov 16 '18 at 8:23
@SouravGhosh All it requires is cdecl wrapper function in C++ ,which can be called from C, and then uses boost.
– hyde
Nov 16 '18 at 8:41
@SouravGhosh All it requires is cdecl wrapper function in C++ ,which can be called from C, and then uses boost.
– hyde
Nov 16 '18 at 8:41
@hyde Technically correct..but is it worth it? Sorry if my statement was not clear earlier. :)
– Sourav Ghosh
Nov 16 '18 at 8:43
@hyde Technically correct..but is it worth it? Sorry if my statement was not clear earlier. :)
– Sourav Ghosh
Nov 16 '18 at 8:43
|
show 2 more comments
1 Answer
1
active
oldest
votes
You can make use of mktime()
.
According to the man page, you have to parse the input and fill up the structure tm
, defined as
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
and then, make a call to mktime()
as
time_t result;
result = mktime(&t);
Point to note: Please read the linked manual and check for the applicable values of the structure members, especially
tm_mon
The number of months since January, in the range 0 to 11.
and
tm_year
The number of years since 1900.
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make use of mktime()
.
According to the man page, you have to parse the input and fill up the structure tm
, defined as
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
and then, make a call to mktime()
as
time_t result;
result = mktime(&t);
Point to note: Please read the linked manual and check for the applicable values of the structure members, especially
tm_mon
The number of months since January, in the range 0 to 11.
and
tm_year
The number of years since 1900.
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
|
show 2 more comments
You can make use of mktime()
.
According to the man page, you have to parse the input and fill up the structure tm
, defined as
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
and then, make a call to mktime()
as
time_t result;
result = mktime(&t);
Point to note: Please read the linked manual and check for the applicable values of the structure members, especially
tm_mon
The number of months since January, in the range 0 to 11.
and
tm_year
The number of years since 1900.
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
|
show 2 more comments
You can make use of mktime()
.
According to the man page, you have to parse the input and fill up the structure tm
, defined as
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
and then, make a call to mktime()
as
time_t result;
result = mktime(&t);
Point to note: Please read the linked manual and check for the applicable values of the structure members, especially
tm_mon
The number of months since January, in the range 0 to 11.
and
tm_year
The number of years since 1900.
You can make use of mktime()
.
According to the man page, you have to parse the input and fill up the structure tm
, defined as
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
and then, make a call to mktime()
as
time_t result;
result = mktime(&t);
Point to note: Please read the linked manual and check for the applicable values of the structure members, especially
tm_mon
The number of months since January, in the range 0 to 11.
and
tm_year
The number of years since 1900.
edited Nov 16 '18 at 8:41
answered Nov 16 '18 at 8:32
Sourav GhoshSourav Ghosh
111k15132191
111k15132191
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
|
show 2 more comments
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours.
– Prashant
Nov 16 '18 at 8:41
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
@Prashant Please show the code that is not working.
– Sourav Ghosh
Nov 16 '18 at 8:48
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
When I convert the UTC timestamp into "tm" structure and passed it to mktime(), it result in epoch time but when I convert back epoch time to "tm" structure again using gmtime(), there is gap of 8 hours. Eg tm localTMTest; localTMTest.tm_year = 2017 - 1900; localTMTest.tm_mon = 3 - 1; localTMTest.tm_mday = 12; localTMTest.tm_hour = 0; localTMTest.tm_min = 0; localTMTest.tm_sec = 0; localTMTest.tm_isdst = -1; time_t localTimeTest = mktime(&localTMTest); struct tm *ptm = gmtime(&localTimeTest); In ptm, hour is set to 8 because my timezone is set to "UTC-8 PST"
– Prashant
Nov 16 '18 at 8:50
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
@Prashant I dont see that, check this.
– Sourav Ghosh
Nov 16 '18 at 9:01
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
Thanks Sourav. But on my system it is showing 8 hours due to timezone in effect. When I run with IST timezone it is giving me date as 11 and time as 18:30 due to +05.30 time difference.
– Prashant
Nov 16 '18 at 9:37
|
show 2 more comments
boost? in C? how?
– Sourav Ghosh
Nov 16 '18 at 8:20
@SouravGhosh The product mixed of C and C++.
– Prashant
Nov 16 '18 at 8:22
That's fine, but I never known boost can be used in C.
– Sourav Ghosh
Nov 16 '18 at 8:23
@SouravGhosh All it requires is cdecl wrapper function in C++ ,which can be called from C, and then uses boost.
– hyde
Nov 16 '18 at 8:41
@hyde Technically correct..but is it worth it? Sorry if my statement was not clear earlier. :)
– Sourav Ghosh
Nov 16 '18 at 8:43