C की time.h Functions
In this page:
#include <time.h>
time_t now = time(NULL);
ctime(&now); // readable date string
Current Time पाना
time() current calendar time को Unix epoch (आधी रात, 1 जनवरी, 1970 UTC) से बीते seconds की संख्या की तरह return करता है, आपको एक single portable integer value देते हुए जिसे आप store, compare, या बाद में एक readable date में convert कर सकते हैं।
यह C में किसी भी time-related calculation का usual starting point है।
उदाहरण: Getting Current Time
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
// Print formatted text to the screen
printf("%d", now > 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Time को String की तरह Format करना
ctime() time() से returned raw time_t value लेता है और इसे day of week, month, date, time, और year रखने वाली एक fixed, human-readable string में format करता है, आपको खुद manually एक date string assemble करने से बचाते हुए।
Returned string पहले से एक newline पर खत्म होती है, जो इसे कहीं और concatenate करते समय भूलना आसान है।
उदाहरण: Formatting Time as String
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
char *str = ctime(&now);
// Print formatted text to the screen
printf("%d", str != NULL);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Time को Deconstruct करना
localtime() एक time_t value को year, month, day, hour, minute, और second के लिए separate fields वाले एक struct tm में तोड़ता है, local time zone के लिए adjusted, ताकि आप single opaque seconds-since-epoch number के साथ काम करने की बजाय एक date के individual components inspect या manipulate कर सकें।
उदाहरण: Deconstructing Time
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
struct tm *local = localtime(&now);
// Print formatted text to the screen
printf("%d", local->tm_year + 1900);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Execution Time Measure करना
clock() report करता है कि program खुद चलना शुरू करने के बाद से कितने CPU clock ticks बीते हैं, जो यह measure करने के लिए useful है कि code का एक specific piece execute होने में कितना समय लेता है।
Result को CLOCKS_PER_SEC से divide करना उस tick count को seconds की एक संख्या में बदल देता है जिसके बारे में आप actually reason कर सकते हैं।
उदाहरण: Measuring Execution Time
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
clock_t start = clock();
// Loop: repeat while the condition holds
for (int i = 0; i < 1000; i++) {}
clock_t end = clock();
// Print formatted text to the screen
printf("%d", end >= start);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Custom Time Formatting
strftime() आपको printf जैसे format specifiers इस्तेमाल करके एक custom date string बनाने देता है, जैसे चार-digit year के लिए %Y या 24-hour-clock hour के लिए %H, ctime() के fixed format तक सीमित होने की बजाय एक date exactly कैसे display होती है इस पर पूरा control देते हुए।
उदाहरण: Custom Time Formatting
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
time_t now = time(NULL);
struct tm *local = localtime(&now);
char buffer[20];
strftime(buffer, 20, "%Y", local);
// Print formatted text to the screen
printf("%s", buffer);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
timeयाclockइस्तेमाल करने से पहले#include <time.h>भूल जाना।ctimeका result store करना और इसे बाद में इस्तेमाल करना, जब यह एक static buffer return करता है जो अगली call से overwrite हो जाता है।- seconds में convert करते समय
clock()की value कोCLOCKS_PER_SECकी बजाय गलत constant से divide करना।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions