← Back to C Course | Chapter 12: Advanced Topics | Lesson 12 of 20

C की time.h Functions

time.h library आपके program को clock पढ़ने और dates के साथ काम करने में मदद करती है, एक calendar और clock combined की तरह। आप यह भी measure कर सकते हैं कि किसी चीज़ में कितनी देर लगी।
Syntax
c
#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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. time या clock इस्तेमाल करने से पहले #include <time.h> भूल जाना।
  2. ctime का result store करना और इसे बाद में इस्तेमाल करना, जब यह एक static buffer return करता है जो अगली call से overwrite हो जाता है।
  3. seconds में convert करते समय clock() की value को CLOCKS_PER_SEC की बजाय गलत constant से divide करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.