Convert time_t To Integer
Solution 1
If able, use sleep()
to pause.
Use time()
and localtime()
to determine nap time.
#include <unistd.h>
#include <time.h>
#include <stdio.h>
int main() {
while (1) {
time_t Now;
if (time(&Now) == -1) {
return -1;
}
struct tm tm;
tm = *localtime(&Now);
tm.tm_mday++;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
time_t Then;
Then = mktime(&tm);
if (Then == -1) {
return -1;
}
if (Then <= Now) {
return -1; // Time moving backwards - Hmmmm?
}
unsigned int NapTime = (unsigned int) (Then - Now);
printf("Going to sleep %u\n", NapTime );
fflush(stdout); // Let's empty the buffers before nap time
if (0 != sleep(NapTime)) {
return -1; // trouble sleeping
}
// Done sleeping!
printf("Midnight\n");
fflush(stdout);
sleep(10);
}
}
Solution 2
In general time_t is implemented as a 32 or 64-bit integer. Though, there's no standard which defines this. So, if you're concerned about portability, you probably shouldn't use it. However, if you're not, then you should just test out how it's implemented on your system. It may already be treated as an integer.
Edit: If you're trying to break out some of the elements of time_t, then use something like,
struct tm * localtime ( const time_t * ptr_time );
The tm struct, which is returned, looks like this,
struct tm {
int tm_sec; /* seconds, range 0 to 59 */
int tm_min; /* minutes, range 0 to 59 */
int tm_hour; /* hours, range 0 to 23 */
int tm_mday; /* day of the month, range 1 to 31 */
int tm_mon; /* month, range 0 to 11 */
int tm_year; /* The number of years since 1900 */
int tm_wday; /* day of the week, range 0 to 6 */
int tm_yday; /* day in the year, range 0 to 365 */
int tm_isdst; /* daylight saving time */
};
Edit 2:
This example was pulled from gnu.org. I took out the print stuff, since you're not wanting to use those, but I left the rest for you to figure out.
#include <time.h>
#include <stdio.h>
int
main (void)
{
time_t curtime;
struct tm *loctime;
/* Get the current time. */
curtime = time (NULL);
/* Convert it to local time representation. */
loctime = localtime (&curtime);
return 0;
}
Related videos on Youtube
Neto
Updated on June 28, 2022Comments
-
Neto almost 2 years
How should I modify this code to print("Its Midnight") every 00:00AM ?
#include <time.h> #include <stdio.h> int main(void) { struct tm * localtime ( const time_t * ptr_time ); struct tm { int tm_sec; /* segundos, range 0 to 59 */ int tm_min; /* minutos, range 0 to 59 */ int tm_hour; /* horas, range 0 to 23 */ int tm_mday; /* dia do mes, range 1 to 31 */ int tm_mon; /* mes, range 0 to 11 */ int tm_year; /* numero de anos desde 1900 */ int tm_wday; /* dia da semana, range 0 to 6 */ int tm_yday; /* dia no ano, range 0 to 365 */ int tm_isdst; }; time_t mytime; mytime = time(NULL); printf(ctime(&mytime)); /* if(str_time.tm_hour == 00 && str_time.tm_min == 00 && str_time.tm_sec == 00 ) { printf("Its midnight"); } */ return 0; }
The Output of time_t is: Www Mmm dd hh:mm:ss yyyy
Example: Tue Feb 26 09:01:47 2009
-
Neto over 10 yearsWhat I really want to do is put my date/hours into a variable(integer or string).
-
dsell002 over 10 yearsAh, I originally thought you were going for something like this, but second guessed my original post. The time.h tm struct gives you all the info you need.
-
Neto over 10 yearsSorry, im very stupid, so i wnat to put a condition:
-
dsell002 over 10 yearsyou can't access a member of a struct directly. if you're referencing a struct through a pointer you use struct_name->tm_min. if you're referencing a struct directly us struct_name.tm_min.
-
Neto over 10 yearsI updated my question...What i really want is like a schedule program, but have no idea how to do that.
-
chux - Reinstate Monica over 10 years@user2887098 You may also want to check out
thrd_sleep()
, new in C11. I have no experience with it. (Thanks for putting me over 5,000 reps)