C Language में time.h Header File: परिचय
यदि आप C programming में समय (time) से संबंधित कार्य करना चाहते हैं, तो time.h header file आपकी पहली जरूरत है। यह header file आपको date, time, timestamp और timers से जुड़ी functionality देती है। चाहे आप stopwatch बनाना चाह रहे हों या कोई event timestamp record करना चाहते हों, time.h आपके लिए essential है।
इस ब्लॉग में हम time.h के functions, practical examples, और real-world implementation के बारे में विस्तार से जानेंगे।
time.h Header File का Overview
time.h C Standard Library का हिस्सा है और यह आपको निम्न सुविधाएं देती है:
- System time और calendar functions तक access
- Time measurement और delays implement करना
- Real-time applications के लिए timestamps generate करना
time.h के Important Functions
1. time()
यह function current calendar time return करता है।
#include <time.h>
time_t t;
t = time(NULL);
printf("Current timestamp: %ld\n", t);
यह आपको seconds में current time देता है। Hindi: वर्तमान समय, timestamp in seconds
2. localtime()
time_t value को local time structure में convert करता है।
struct tm *local;
local = localtime(&t);
printf("Year: %d\n", local->tm_year + 1900);
यह function दिन, महीना और वर्ष जैसी जानकारी extract करने में मदद करता है।
3. ctime()
time_t को human-readable string में convert करता है।
printf("Readable Time: %s", ctime(&t));
Example Output: "Wed Mar 4 14:30:00 2026"
4. difftime()
दो timestamps के बीच difference calculate करता है।
time_t start, end;
double diff;
start = time(NULL);
/* कुछ process */
end = time(NULL);
diff = difftime(end, start);
printf("Process took %.2f seconds\n", diff);
5. mktime()
struct tm को time_t में convert करता है। Useful for custom dates.
struct tm date = {0};
date.tm_year = 2026 - 1900;
date.tm_mon = 2; // March
date.tm_mday = 4;
time_t t_custom = mktime(&date);
printf("Custom timestamp: %ld\n", t_custom);
Practical Examples of time.h in C
Example 1: Current Date और Time Print करना
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("Current Date: %02d-%02d-%04d\n", tm.tm_mday, tm.tm_mon+1, tm.tm_year+1900);
printf("Current Time: %02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
return 0;
}
Example 2: Program Execution Time Measure करना
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double cpu_time_used;
start = clock();
for(int i=0; i<1000000; i++); // dummy loop
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Execution Time: %.6f seconds\n", cpu_time_used);
return 0;
}
Advanced Example 1: strftime() के साथ Custom Date/Time Format
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
char buffer[80];
// Custom format: Day-Month-Year Hour:Minute:Second
strftime(buffer, sizeof(buffer), "%d-%m-%Y %H:%M:%S", &tm);
printf("Formatted Date & Time: %s\n", buffer);
return 0;
}
Advanced Example 2: sleep() और nanosleep() से Delay Implement करना
#include <stdio.h>
#include <time.h>
#include <unistd.h> // sleep()
int main() {
printf("Process starts...\n");
// 3 seconds delay using sleep()
sleep(3);
printf("After 3 seconds using sleep()\n");
// 1.5 seconds delay using nanosleep()
struct timespec ts;
ts.tv_sec = 1;
ts.tv_nsec = 500000000; // 0.5 seconds
nanosleep(&ts, NULL);
printf("After 1.5 seconds using nanosleep()\n");
return 0;
}
Advanced Example 3: Countdown Timer
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main() {
int seconds = 5;
while(seconds > 0) {
printf("Countdown: %d seconds remaining\n", seconds);
sleep(1); // 1 second delay
seconds--;
}
printf("Time's up!\n");
return 0;
}
Actionable Insights:
- Use
strftime()for displaying time in readable/custom formats. - Use
sleep()for simple delays;nanosleep()for precise timing. - Countdown timers, event delays, and task scheduling can all be implemented with these functions.
- Always check system compatibility for
nanosleep()(POSIX systems).
Data Types और Structures in time.h
C की time.h library में मुख्य data types हैं:
- time_t: Calendar time store करता है, seconds since UNIX epoch (1 Jan 1970)।
- clock_t: CPU clock ticks measure करता है, performance timing के लिए useful।
- struct tm: Date और time components को अलग fields में रखता है जैसे second, minute, hour, day, month, year।
struct tm {
int tm_sec; // seconds (0–59)
int tm_min; // minutes (0–59)
int tm_hour; // hours (0–23)
int tm_mday; // day of month (1–31)
int tm_mon; // months since January (0–11)
int tm_year; // years since 1900
int tm_wday; // day of week (0–6)
int tm_yday; // day in year (0–365)
int tm_isdst; // daylight saving time flag
};
asctime(): Human-Readable Time
asctime() function struct tm को human-readable string में convert करता है:
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *tm_info = localtime(&t);
char *time_str = asctime(tm_info);
printf("Readable Time: %s", time_str);
return 0;
}
gmtime(): UTC Time Print करना
gmtime() function आपको UTC (Coordinated Universal Time) return करता है, जो timezone-independent होता है।
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *utc_time = gmtime(&t);
printf("UTC Time: %s", asctime(utc_time));
return 0;
}
clock() और CLOCKS_PER_SEC का उपयोग
CPU execution time measure करने के लिए clock() और macro CLOCKS_PER_SEC का उपयोग करें:
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
double cpu_time_used;
start = clock();
for(int i=0; i<1000000; i++); // dummy loop
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("CPU Time: %.6f seconds\n", cpu_time_used);
return 0;
}
Actionable Insights:
- Use
struct tmto extract individual date/time components for custom applications. asctime()औरstrftime()के combination से readable और formatted output create करें।- UTC timestamp जरूरत हो तो
gmtime()use करें। - Performance analysis के लिए
clock()औरCLOCKS_PER_SECका प्रयोग करें। - All examples are beginner-friendly और real-world C programming में directly apply किए जा सकते हैं।
Post a Comment
Blogger FacebookYour Comment Will be Show after Approval , Thanks