converting milliseconds to date in C
Solution 1
Function time_t time(time_t* timer)
returns the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. In addition, if the input argument timer != NULL
, then the function also sets this argument to the same value (so you probably have no reason to call it with anything else but NULL
).
Function struct tm* localtime(const time_t* timer)
takes the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC, and returns a structure that represents the equivalent time & date. If you're working on a multi-threaded application, then please note that this function is not thread safe.
As to your question - is there any way for converting milliseconds to time & date - yes, but:
- Take into consideration that the milliseconds will be considered as of 00:00 hours, Jan 1, 1970 UTC.
- Since the
time_t
type is 32-bit long, you will not be able to convert 4G*1000 milliseconds or more.
Here is a function for converting milliseconds to time & date:
struct tm* GetTimeAndDate(unsigned long long milliseconds)
{
time_t seconds = (time_t)(milliseconds/1000);
if ((unsigned long long)seconds*1000 == milliseconds)
return localtime(&seconds);
return NULL; // milliseconds >= 4G*1000
}
Solution 2
For those of us who were searching the web for an answer to apply to embedded c applications, think pic 32 programming here is the mathematical calculation:
Date in Epoch_seconds = ( (epoch_seconds / 1000) / 86400 ) + 25569
Resulting in a 5 digit answer which is 10 bits long format dd/MM/yyyy (Note: the slashes are encoded in the result here so when converting to human readable date please account for it)
Where one day = 86400 ms
and the date 1970/1/1 = 25569
example:=( (1510827144853/1000) / 86400 ) + 25569 = 43055
put 43055 in excel and format cell to date dd/MM/yyyy and it gives you 16/11/2017
user3584114
Updated on July 27, 2022Comments
-
user3584114 almost 2 years
Is there any way of converting
milliseconds
todate
in C?What I am trying to do is write a small application in C that can return the financial year and the like(quarter, week) given the start month and
isCurentYear
bool, where the input might be milliseconds or a date!In the first place, is there any way by which this can be achieved in C?
And if so, in the process of finding out a way of converting
milliseconds
todate
I have found out that the use oftime_t
takes the current millis of our system and by creating a structure pointing to it,it permits us to extract the year,month, date, sec etc!Refer the below code:
#include <sys/time.h> #include<stdio.h> #include<time.h> void main() { time_t t = time(000); //time_t t = time(0); struct tm tm = *localtime(&t); printf("now: %d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); }
And also, can
time_t
be used to storemillisecond
values so that it can be converted to date using tm struct? -
user3584114 about 10 yearsActually I am in need of a generic conversion mechanism as to how to convert a given millisecond value to a date format in C, not the current time.
-
user3581454 about 10 yearsWell,
time_t
structure holds olny integer value, usually (ex. when returned fromtime
function) it's number of seconds since Jan 1, 1970. So you cannot legally use it to storage miliseconds. But, you can convert miliseconds to seconds, with cost of accuracy, by simply dividing it by 1000. Then, you can use dedicated functions. Second is, thattime_t
holds value of your choice. If you assing to it value returned formtime
function, then you have current time. If you assing diffrent value, for exampletime_t a = 0
, then you have Jan 1, 1970, in representation explained above. -
user3581454 about 10 yearsThen is the
localtime
, function. It just converts argument value to structure that is much easier to use, because it represent date in 'normal' calendar, with years, days etc. Of course, it assumes that argument is number of seconds since Jan 1, 1970 UTC, and converts it, to date in your local time zone, based on your system settings. Of course, you will have to choose time zone, if you want to have date represented this way. -
user3584114 about 10 yearsYes! That was what i wanted! To assign a value to time_t datatype. Thank you! Plus is there any way to get the week and quarter in the year in C using the tm struct?
-
user3581454 about 10 yearsThere is a field
tm_yday
intm
structure, containing number of days since January 1, Based on this, you can calculate week, simply dividing it by 7. Quarter can be calculated by dividingtm_mon
value by 3. In result, you get number of quarter, begining with 0 for first part of the year. -
user3584114 about 10 yearsThank you ! :) i ll try that and see!
-
barak manos about 10 yearsHi @chux. I do realize that
time_t
is not defined as a 32-bit variable by the standard, but it is a 32-bit variables in most cases, so I wanted to provide OP with an effective answer rather than to go down into the "tiny" details of the standard. Your note on therepeated roll-over
is correct, I suppose , so I'll move the1000
to the other side of the equation. Thanks. -
barak manos about 10 years@user3584114: Please see a slight change in the answer above, at the
if
statement. -
chux - Reinstate Monica about 10 yearsOnly 8865 days until Tuesday, 19 January 2038 - Year 2038 problem. I suspect
time_t
will be moving away from 32-bit integer more and more. :-) -
barak manos about 10 years@chux: Thanks. In any case, the function in my answer does not rely on
time_t
being a 32-bit type (except for the comment at the bottom), so it does not yield any additional problem on top of this 2038-issue (which I wasn't aware of it actually having a page on Wikipedia BTW, so thanks for the info). -
chux - Reinstate Monica about 10 yearsConfident
sizeof(text)-1
can besizeof(text)
. "If the total number of resulting characters including the terminating null character is not more than maxsize, the strftime function returns the number of characters placed into the array pointed to by s not including the terminating null character." C11dr §7.27.3.5 8