-
- Time.h
- Time_t
- Time.h
Time_t 它本身是一个long 类型
Time(time_t *time_t) 是给一个time_t变量赋值为当前数据
Time_t now;
Time(&now);
-
-
- Struct tm
-
这个tm结构体,里面放了year month day hour minute second
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
}
-
-
- Localtime
-
extern struct tm *localtime (const time_t *__timer) __THROW;
localtime可以把time_t(long)转换成一个结构体
Struct tm *t = localtime(&now);
Localtime(&now)转换的当前时间给tm year = 真实year-1900
Month = 真实month -1
-
-
- Mktime
-
extern time_t mktime (struct tm *__tp) __THROW;
Mktime把结构体 tm转换成 time_t
time_t convertToTime_t(int year,int month,int day,int hour,int minute,int seconds){
struct tm info = {0};
info.tm_year = year-1900;(需要-1900)
info.tm_mon = month-1; (需要月份-1)
info.tm_mday = day;
info.tm_hour = hour;
info.tm_min = minute;
info.tm_sec = seconds;
time_t t= mktime(&info);
return t;
}
-
-
- Gmtime
-
/* Return the `struct tm' representation of *TIMER
in Universal Coordinated Time (aka Greenwich Mean Time). */
extern struct tm *gmtime (const time_t *__timer) __THROW;
这个与localtime()区别就是它获取的时间hour需要-8
-
-
- Ctime asctime()
-
都是返回字符串
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <math.h> /* sqrt */
void time_to_long();
char *formate_current_date();
int diffday(time_t t1,time_t t2);
void time_to_long(){
time_t timep;
struct tm *p;
time(&timep);
p = gmtime(&timep);
long longsec = 1000;
long longmin = 60 *1000;
long longhour = 60 * 60 * 1000;
long day = 24 * 60 * 60 * 1000;
// long month =
// long ss = p->tm_sec * 1000+ p->tm_min*60*1000 + (8+p->tm_hour)*60*60*1000;
printf("aaa\n");
}
char *formate_current_date(){
time_t timep;
struct tm *p;
struct tm *info;
// char time1[28];
char * time1 = (char *)calloc(28,sizeof(char *));
time(&timep);
info = localtime(&timep);
p = gmtime(&timep);
/**年需要+1900 月需要+1 小时需要+8 注意日期需要选m_day*/
/**这个time1使用的是calloc,需要在调用的地方free(time1),释放内存*/
sprintf(time1,"%d-%d-%d %d:%d:%d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec);
printf("time1 %s\n",time1);
return time1;
}
// void getCurretTime(){
// struct timeval tv;
// gettimeofday(&tv,NULL);
// int aa = tv.tv_sec * 1000 + tv.tv_usec / 1000;
// printf("getCurretTime: %d\n",aa);
// return tv.tv_sec * 1000 + tv.tv_usec / 1000;
// }
long getCurrentTime2(){
time_t now;
time(&now);
printf("now:%ld\n",now);
printf("now:%ld\n",now);
long ss = now;
// 1673244657 1673244537
long sa = (ss*1000) -(1673244537 * 1000);
printf("ssss:%ld \n",ss);
printf("ssssaaaaa:%ld \n",sa);
return sa;
}
char * getName(){
char * name = "myname is name";
return name;
}
char *getName2(){
char name[] = "fefefe";
return name;
}
time_t convertToTime_t(int year,int month,int day,int hour,int minute,int seconds){
time_t now;
time(&now);
// struct tm *info = {0};
// info->tm_year = year-1900;
// info->tm_mon = month-1;
// info->tm_mday = day;
// info->tm_hour = hour-8;
// info->tm_min = minute;
// info->tm_sec = seconds;
// time_t t= mktime(info);
struct tm info = {0};
info.tm_year = year-1900;
info.tm_mon = month-1;
info.tm_mday = day;
info.tm_hour = hour;
info.tm_min = minute;
info.tm_sec = seconds;
time_t t= mktime(&info);
return t;
}
void testlocaltime(){
// time_t now;
// time(&now);
// time_t now2;
// struct tm * tm2 = localtime(&now2);
// struct tm * tm = localtime(&now);
// printf("aaaa %s \n",asctime(tm)) ;
// printf("bbbbb %s \n",asctime(tm2)) ;
// printf("aaa\n");
// double aa = difftime(convertToTime_t(1999,10,10,0,0,0),convertToTime_t(1980,3,3,0,0,0));
// printf("aaaadfdfdf:%d\n",aa);
// int diff = diffday(convertToTime_t(1999,10,10,0,0,0),convertToTime_t(1980,3,3,0,0,0));
// printf("bbbb diff %d\n",diff);
// time_t now;
// struct tm *tm = localtime(&now);
// printf("asctime(tm) :%s \n",asctime(tm)); //这个样子不对,需要给now赋值才行
time_t now;
time(&now);//赋值当前时间
struct tm *t = localtime(&now);
printf("asctime(tm) :%s \n",asctime(t));
time_t p = convertToTime_t(1998,10,10,13,14,15);
struct tm *info = localtime(&p);
printf("pppp: %ld\n",p);
printf("asctime(info) :%s \n",asctime(info));
time_t now2;
time(&now2);
printf("citme sss %s\n",ctime(&now2));
}
int diffday(time_t t1,time_t t2){
int diff = (int)(t1-t2);
return diff/(24*3600);
}
// int frequency_of_primes (int n) {
// int i,j;
// int freq=n-1;
// for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
// return freq;
// }
void testclock(){
clock_t t;
int f;
t = clock();
printf ("Calculating...\n");
// f = frequency_of_primes(99999);
// printf ("The number of primes lower than 100,000 is: %d\n",f);
t = clock()-t;
// printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);
}
int main(){
// https://blog.csdn.net/xuehuafeiwu123/article/details/77944893
// https://blog.csdn.net/weixin_29938067/article/details/117118688
printf("test2222222\n");
time_to_long();
formate_current_date();
// getCurretTime();
long sese = getCurrentTime2();
long sesqq = 10;
printf("sesqq :%ld\n",sesqq);
printf("sese :%d\n",sese);
char *myname = getName();
printf("myname %s\n",myname);
char *name2222 = getName2();
printf("name222 %s\n",name2222);
time_t t = convertToTime_t(1980,3,10,15,13,22);
printf("time_ttime_t %d\n",t);
testlocaltime();
testclock();
// frequency_of_primes(99999);
return 0;
}