# time

***

**1. Getting the Current Time**

```c
#include <time.h>

time_t t = time(NULL);
printf("Current time: %s", ctime(&t));
```

**2. Converting Time to String**

```c
#include <time.h>
#include <stdio.h>

char buf[100];
time_t t = time(NULL);
strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", localtime(&t));
printf("Formatted time: %s\n", buf);
```

**3. Converting String to Time**

```c
#include <time.h>
#include <stdio.h>

struct tm tm;
strptime("Tue Sep 20 12:00:00 2022", "%a %b %d %H:%M:%S %Y", &tm);
time_t t = mktime(&tm);
printf("Time from string: %s", ctime(&t));
```

**4. Calculating Time Difference**

```c
#include <time.h>
#include <stdio.h>

time_t t1, t2;
t1 = time(NULL);
// Perform some operation
t2 = time(NULL);
printf("Time difference: %ld seconds\n", t2 - t1);
```

**5. Sleeping for a Specified Duration**

```c
#include <time.h>
#include <stdio.h>

sleep(5); // Sleep for 5 seconds
printf("Woke up from sleep!\n");
```

**6. Measuring Execution Time of a Function**

```c
#include <time.h>
#include <stdio.h>

clock_t start, end;
start = clock();
// Execute some function
end = clock();
printf("Execution time: %lf seconds\n", (double)(end - start) / CLOCKS_PER_SEC);
```

**7. Creating a Timer**

```c
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

void timer_callback() {
    printf("Timer fired!\n");
    exit(0); // Exit the program
}

time_t t = time(NULL);
timer_t timerid;
struct sigevent se;

se.sigev_notify = SIGEV_SIGNAL;
se.sigev_signo = SIGALRM;
se.sigev_value.sival_ptr = &timerid;
se.sigev_notify_function = timer_callback;

if (timer_create(CLOCK_REALTIME, &se, &timerid) == -1) {
    perror("timer_create");
    exit(1);
}

if (timer_settime(timerid, 0, &(struct itimerspec) {
    { 5, 0 }, // 5 seconds
    { 0, 0 }, // No repeat
}, NULL) == -1) {
    perror("timer_settime");
    exit(1);
}

// Wait for the timer to fire
while (1);
```

**8. Formatting Time as ISO 8601 String**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
char buf[100];
strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&t));
printf("ISO 8601 time: %s\n", buf);
```

**9. Comparing Times**

```c
#include <time.h>
#include <stdio.h>

time_t t1, t2;
t1 = time(NULL);
// ...
t2 = time(NULL);
if (difftime(t2, t1) > 60) {
    printf("More than a minute has passed!\n");
}
```

**10. Setting the Time**

```c
#include <time.h>
#include <stdio.h>

time_t new_time = 1656028800; // Timestamp for September 20, 2022 12:00:00 UTC
if (stime(&new_time) == -1) {
    perror("stime");
}

printf("Time updated!\n");
```

**11. Calculating Timestamps**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
printf("Timestamp: %ld\n", t);
```

**12. Converting Timestamps to Strings**

```c
#include <time.h>
#include <stdio.h>

time_t t = 1656028800; // Timestamp for September 20, 2022 12:00:00 UTC
char buf[100];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", gmtime(&t));
printf("Formatted timestamp: %s\n", buf);
```

**13. Converting Strings to Timestamps**

```c
#include <time.h>
#include <stdio.h>

struct tm tm;
strptime("2022-09-20 12:00:00", "%Y-%m-%d %H:%M:%S", &tm);
time_t t = mktime(&tm);
printf("Timestamp from string: %ld\n", t);
```

**14. Getting the Current Calendar Day**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current calendar day: %d\n", tm->tm_mday);
```

**15. Getting the Current Month**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current month: %d\n", tm->tm_mon + 1);
```

**16. Getting the Current Year**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current year: %d\n", tm->tm_year + 1900);
```

**17. Getting the Current Day of the Week**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current day of the week: %s\n", tm->tm_wday);
```

**18. Getting the Current Hour**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current hour: %d\n", tm->tm_hour);
```

**19. Getting the Current Minute**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current minute: %d\n", tm->tm_min);
```

**20. Getting the Current Second**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Current second: %d\n", tm->tm_sec);
```

**21. Formatting a Date**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Formatted date: %s %s %d\n", tm->tm_wday, tm->tm_mon, tm->tm_mday);
```

**22. Formatting a Time**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Formatted time: %d:%d:%d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
```

**23. Converting a Date to a Timestamp**

```c
#include <time.h>
#include <stdio.h>

time_t timestamp;
struct tm tm = {
    .tm_year = 2022 - 1900,
    .tm_mon = 9 - 1,
    .tm_mday = 20,
    .tm_hour = 12,
    .tm_min = 0,
    .tm_sec = 0
};
timestamp = mktime(&tm);
printf("Timestamp: %ld\n", timestamp);
```

**24. Converting a Timestamp to a Date**

```c
#include <time.h>
#include <stdio.h>

time_t timestamp = 1656028800; // Example timestamp
struct tm *tm = localtime(&timestamp);
printf("Date: %s %s %d\n", tm->tm_wday, tm->tm_mon, tm->tm_mday);
```

**25. Getting the Days of the Month**

```c
#include <time.h>
#include <stdio.h>

time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("Days in month: %d\n", tm->tm_days);
```

**26. Getting the Number of Days Since the Epoch**
