Hi guys,

I need a solution. I have a uint32_t variable say var1. I need to store current time value in this variable, and somewhere in my code i need that time in proper timing formet i.e hour minute and seconds .

is this possible?

Any other solution for storing time into absolute value and getting local time from that vale would be appreciated.

Thanks

Im not sure, but isn't there a fuction like, system time or something like that?

I'm not entirely sure whether you want to get a time and store it in a uint32 for later retrieval, or get the real time at any moment from a uint32.
If its the former, it's possible, for simple hours,minutes,seconds over a 24 hour range you could use a union like

union UTime
{
    uint32 time32;
    struct
    {
        unsigned hours : 5;
        unsigned minutes : 6;
        unsigned seconds : 6;
    };
};

UTime myTime;

Then access myTime.hours, myTime.minutes, myTime.seconds as you wish or use myTime.time32 when you need the integer.

Add your own constructors and access functions as required.

Of course, if you need higher resolution than seconds, or a greater range than 24 hours you may run out of bits.

inline
unsigned getMin(uint32_t t) {
    return (t/60)%60;
}
inline 
unsigned getHour(uint32_t t) {
    return t%3600;
}
inline
unsigned getSec(uint32_t t) {
    return t%60;
}
inline
uint32_t makeTimeOfDay(unsigned h, unsigned m, unsigned s = 0) {
    return s + 60*m + 3600*h;
}
inline // returns interval abs value
uint32_t diffTime(uint32_t t0, uint32_t t1) {
    return t0 > t1? t0 - t1: t1 - t0;
}
/// Local time of day in seconds since midnight
uint32_t getLocalTimeOfDay()
{
    time_t t(time(0));
    tm* p = ::localtime(&t);
    return makeTimeOfDay(p->tm_hour,p->tm_min,p->tm_sec);
}
uint32_t getUtcTimeOfDay()
{
    time_t t(time(0));
    tm* p = ::gmtime(&t);
    return makeTimeOfDay(p->tm_hour,p->tm_min,p->tm_sec);
}

Look at the header ctime. All the time functions are there. A Google search should give you definitions and explanations.

ctime ^^ thats the one :)

Regrettably, there is no a little thing in <ctime>: guarantee of 32-bit integer time type.
The time_t type is an arithmetic type capable of representing times - that's all...

Regrettably, there is no a little thing in <ctime>: guarantee of 32-bit integer time type.
The time_t type is an arithmetic type capable of representing times - that's all...

I don't understand... Is there a problem with ctime?

I don't understand... Is there a problem with ctime?

No problems with ctime. There is a problem with OP requirements:

I need a solution. I have a uint32_t variable say var1. I need to store current time value in this variable,,,

Thanks to all of you guys... giving me so many reply. It was really helpful.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.