简体   繁体   中英

linux c get system date format yyyymmdd

I want ask some questions 1) I want get string yyyymmdd of system date using linux c how should I do it ? And I want use this string do some arithmetic, like I have "20160317" I want do 20160317 - 90 get a new date,pls help

2)

   int n;
   int m;
   n = (m%10 = 0)?(m/10):(m/10+1);

is there some cleaner way to do this? like if m = 11 n will get 2, m = 10 n will get 1.

You can convert from a string to a time structure using strptime (Linux only). This will set the time on a tm struct that is passed to it.

You can then use the ctime library. It will let you use difftime which will give you get the difference between two times.

Consider c standard function. strftime

#include <time.h>

char datename[32];
struct tm* to;
time_t t;
t = time(NULL);
to = localtime(&t);
strftime(datename, sizeof(datename), "%Y%m%d", to);

if you want date of 90 days before. just

t -= 90 * 24 * 3600;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM