In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
LINUX C system programming and PYTHON in the time module contrast is how, for this problem, this article details the corresponding analysis and solution, hope to help more want to solve this problem of small partners to find a simpler and easier way.
Today, when I look at python time module time, I find that it is basically the same as the time call function in LINUX system programming. I just didn't learn the time module of LINUX C programming well before.
1. The way time exists
In fact, whether it is C or PYTHON, there are three different ways of existence of time.
Calendar Time: Seconds since Epoch and seconds since New Era Time (January 1, 1970 00:00:00)
Decomposition time: in fact, time exists in the internal structure of the program, but also an intermediate medium, C uses structures, PYTHON uses tuples
LINUX C is tm structure:
struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ /* >0 DST is in effect =0 DST is not effect; File Name: testtime.c > Author: gaopeng QQ:22389860 all right reserved > Mail: gaopp_200217@163.com > Created Time: Wed 01 Nov 2017 02:28:25 AM CST************************************************************************/#include#include#include#define MAXSIZE 99int rtime2ctime(/*in*/const char* r_time,/*out*/time_t* ctime){ time_t i_ctime; struct tm tm_st; //here converted to decomposition time tm_st according to fixed test if ( ! strptime(r_time,"%Y-%m-%d %X(%p) %z",&tm_st)) { return -1; } //Convert decomposition time tm_st to calendar time ctime here if( (*ctime = mktime(&tm_st)) == -1) { return -1; } return 0;}int ctime2rtime(/*in*/time_t c_time){ struct tm* tm_st = NULL; char time[MAXSIZE] ; //Convert calendar time to split time here if((tm_st = localtime(&c_time)) == NULL) { return -1; } //convert decomposition time to readable time here if(strftime(time,MAXSIZE-1,"%Y-%m-%d %X(%p) %z",tm_st) == 0) { return -1; } printf("%s\n",time); return 0;}int main(void){ char r_time[100] = "2017-10-12 07:52:03(AM) +0800"; time_t ctime; if( rtime2ctime(r_time,&ctime) == -1 ) { printf("rtime2ctime error\n"); return -1; } printf("2017-10-12 07:52:03(AM) +0800 to calenlar time is:%ld\n",ctime); ctime = ctime - (time_t)3600; printf("after sub 1 hours time is:"); if(ctime2rtime(ctime) == -1) { printf("ctime2rtime error\n"); }}
Python programs:
import timedef rtime2ctime(r_time): #Here converted to decomposition time based on fixed test readable time p_time = time.strptime(r_time,"%Y-%m-%d %X(%p) %z") #Convert decomposition time tm_st to calendar time here return time.mktime(p_time)def ctime2rtime(ctime): #Convert calendar time to split time here p_time = time.localtime(ctime) #Convert decomposition time to readable time here r_time = time.strftime("%Y-%m-%d %X(%p) %z",p_time) return r_timer_time="2017-10-12 06:52:03(AM) +0800"ctime = rtime2ctime(r_time)print("2017-10-12 06:52:03(AM) +0800 to calenlar time is:%d" %(ctime))ctime = ctime - 3600print("after sub 1 hours time is:%s" %(ctime2rtime(ctime)))
Their results are:
2017-10-12 06:52:03(AM) +0800 to calenlar time is:1507762323after sub 1 hours time is:2017-10-12 05:52:03(AM) +0800 About LINUX C system programming and PYTHON in the time module comparison is what kind of problem to share here, I hope the above content can have some help for everyone, if you still have a lot of doubts, you can pay attention to the industry information channel to learn more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.