tgoop.com/Learncpp/1249
Create:
Last Update:
Last Update:
#ctime #time.h #difftime
🔵زمان (time)
🔹تابع difftimedouble difftime(time_t end,time_t beginning);
این تابع اختلاف دو زمان را که به صورت زمان غیر استاندارد(یا همان time_t است)را محاسبه می کند.
البته درست این است که این تابع فقط نوع time_t که همان ثانیه ها هست را محاسبه می کند ولی می توانیم کاری کنیم که حتی اختلاف ماه و سال و روز را هم محاسبه کنیم.
🔹پارامتر ها :
🔻مفهوم end :
این همان زمان اولیه است که می خواهیم اختلافش تا زمان ثانویه را محاسبه کنیم.
🔻مفهوم beginning :
این همان زمان ثانویه است.
🔹مقادیر بازگشتی :
یک مقدار از نوع double که همان اختلاف زمان به ثانیه است
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
time_t tim1,tim2;
double s;
time(&tim1);
for(int i=0;i<=9999;i++)
{
//دستورات
}
time(&tim2);
s=difftime(tim1,tim2);
cout<<s<<endl;
return 0;
}
مثال بالا زمان تلف شده توسط یک تکه کد را محاسبه می کند.
#include <iostream>
#include <time.h>
/* time_t, struct tm, difftime, time, mktime */
using namespace std;
int main ( )
{
time_t now;
struct tm newyear;
double seconds;
time(&now);
/*get current time;same as:now=time(NULL) */
newyear=*localtime(&now);
newyear.tm_hour=0;
newyear.tm_min=0;
newyear.tm_sec=0;
newyear.tm_mon=0;
newyear.tm_mday=1;
seconds=difftime(now,mktime(&newyear));
cout<<seconds since new year in the current timezone.\n"<<seconds;
return 0;
}
Output:
3777291 seconds since new year in the current timezone.
برنامه بالا یک مثال برای پیدا کردن اختلاف روز است.
🔰ترجمه شده از سایت
www.cplusplus.com
@Learncpp
BY Learning ©➕➕
Share with your friend now:
tgoop.com/Learncpp/1249