C

  • פותח הנושא yka
  • פורסם בתאריך

yka

New member
C

אני מחפש פונקציה בשפת C שמחזירה את השעה הנוכחית. אני אשמח לקבל גם דוגמא לקטע קוד המשתמש בפונקציה. תודה.
 

ke

New member
זמן ב C

עושים include ל time.h יש שם מספר פונקציות הקשורות לזמן המערכת. שתי הפונקציות החשובות לך הם time() ו localtime(). הראשונה מחזירה את הזמן הנוכחי כמספר (מספר השניות שעברו מה 1.1.1970) והשניה ממירה את המספר ל struct עם שדות "ידידותיים". דוגמא לשימוש:
#include <stdio.h> #include <time.h> int main() { time_t now = time(0); struct tm* nowst = localtime(&now); int hour = nowst->tm_hour; int min = nowst->tm_min; int sec = nowst->tm_sec; printf("%02d:%02d:%02d\n",hour,min,sec); return 0; }​
אפשר מה struct לקבל עוד אינפומרציה :
struct tm { int tm_sec; /* Seconds */ int tm_min; /* Minutes */ int tm_hour; /* Hour (0--23) */ int tm_mday; /* Day of month (1--31) */ int tm_mon; /* Month (0--11) */ int tm_year; /* Year (calendar year minus 1900) */ int tm_wday; /* Weekday (0--6; Sunday = 0) */ int tm_yday; /* Day of year (0--365) */ int tm_isdst; /* 0 if daylight savings time is not in effect) */ };​
 
למעלה