C time.h Library

C time.h Library

The time.h library in C provides functions and types for date and time operations. It is widely used for:

  • Measuring time

  • Generating timestamps

  • Creating delays

  • Random number seeding


📌 Key Types in time.h

Type Description
time_t Represents calendar time (usually seconds since 1 Jan 1970)
struct tm Represents broken-down time (year, month, day, hour, minute, second)
clock_t Represents processor time used by a program

📌 Common Functions

1️⃣ Current Time

Function Description
time(time_t *t) Returns current time as time_t
difftime(time_t end, time_t start) Returns difference in seconds
clock() Returns processor time used by program

Example:

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

int main() {
time_t current_time;
current_time = time(NULL);
printf("Current time in seconds since Epoch: %ld\n", current_time);
return 0;
}


2️⃣ Broken-down Time

Function Description
localtime(const time_t *t) Converts time_t to local struct tm
gmtime(const time_t *t) Converts time_t to UTC struct tm
mktime(struct tm *tm) Converts struct tm back to time_t

Example:

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

int main() {
time_t t = time(NULL);
struct tm *local = localtime(&t);

printf("Year: %d\n", local->tm_year + 1900);
printf("Month: %d\n", local->tm_mon + 1);
printf("Day: %d\n", local->tm_mday);
printf("Hour: %d\n", local->tm_hour);
printf("Minute: %d\n", local->tm_min);
printf("Second: %d\n", local->tm_sec);

return 0;
}


3️⃣ Formatting Time

Function Description
asctime(const struct tm *tm) Converts struct tm to string
ctime(const time_t *t) Converts time_t to string
strftime(char *s, size_t max, const char *format, const struct tm *tm) Formats time into string

Example:

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

int main() {
time_t t = time(NULL);
char buffer[100];

strftime(buffer, 100, "%A, %d %B %Y %H:%M:%S", localtime(&t));
printf("Formatted date & time: %s\n", buffer);

return 0;
}


4️⃣ Delays and Timing

Function Description
clock() Measures CPU time used
sleep(unsigned int seconds) Pauses program for seconds (unistd.h on Linux)
difftime(time_t end, time_t start) Computes difference between times

Example:

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

int main() {
clock_t start = clock();

for(long i=0; i<100000000; i++); // some work

clock_t end = clock();
double time_taken = (double)(end - start) / CLOCKS_PER_SEC;

printf("Time taken: %.2f seconds\n", time_taken);
return 0;
}


5️⃣ Seeding Random Numbers

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

int main() {
srand(time(NULL)); // seed random number generator
printf("Random number: %d\n", rand());
return 0;
}


📌 Summary Table

Function Purpose
time Get current time as time_t
localtime Convert time_t to local time (struct tm)
gmtime Convert time_t to UTC time
mktime Convert struct tm to time_t
asctime Convert struct tm to string
ctime Convert time_t to string
strftime Format time as string
clock Get CPU time used
difftime Compute difference in seconds
srand(time(NULL)) Seed random numbers

✅ Notes

  1. time_t is usually seconds since 1 Jan 1970 (Epoch).

  2. struct tm fields:

    • tm_year: years since 1900

    • tm_mon: 0-11

    • tm_mday: 1-31

    • tm_hour, tm_min, tm_sec

  3. Use strftime for custom date-time formats.

  4. Combine with stdlib.h for random number seeding.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *