Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

increase one day in a date string 3

Status
Not open for further replies.

ty1975

Programmer
Nov 5, 2002
12
GB
I have a file name which has the format as "year_month_day.alm", for example "98_04_23.alm". I need to increase it by one day, and get a new file name. So for this file name "98_04_23.alm", should return "98_04_24.alm".
I do not know what is the easiest way to convert string to integer, and increase it, and convert it back to string. Please help me.
 


#include <time.h>

char back[32];
int add = 1;
long now = 0;
struct tm *xx;

time(&now);
now += (add * 24 * 60 * 60);
xx = localtime(&now);
sprintf(back,&quot;%d%02d%02d\n&quot;
,xx->tm_year+1900
,xx->tm_mon+1
,xx->tm_mday
);

-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
All right, but I belive that this is just 1/2 solution
of the problem.

ty1975 does not need know which day is tomorrow, but needs
know name-file a day after a-one.

Instead load now with time() time( &now ), use


time_t t;
int s=m=h=0 ;

int d,M,y ;

d=
M=
y= // load them from your filename

struct tm TM ;
TM.tm_sec = (int)s ; // Seconds after the minute - [0,59]
TM.tm_min = (int)m ; // Minutes after the hour - [0,59]
TM.tm_hour= (int)h ; // Hours since midnight - [0,23]
TM.tm_mday= (int)d ; // Day of the month - [1,31]
TM.tm_mon= (int)M-1 ; // Months since January - [0,11]
TM.tm_year= (int)y-1900 ; // Years since 1900
TM.tm_wday= 0 ; // Days since Sunday - [0,6]
TM.tm_yday= 0 ; // Days since January 1 - [0,365]
TM.tm_isdst=_daylight ; // Daylight-saving-time flag

t = mktime( &TM ) ;

Now you can use, as 2nd part of program, what jamisar wrote.

t+= 1 day
.....

bye

PS
If your C implementation has not mktime,
compute how many seconds are passed from 1-Jan-1970 to
your file day.

 
Thanks for both of you. You are real top experts, I am so lucky. :)

Have a nice day!
 
Hello, victorv.

There was one uncertain point. Does this code check leap year?

Thanks
 
sure victorv you are 100% right, i did not really read ty1975's question, a star for you. -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
hi,
our code is stupid: we add 60*60*24 to a number given by
the system, and it re-converts it in &quot;human&quot; date;

Who manages leap year, 30/31 days, ecc are the system-call:

mktime and localtime

and I am sure that they are Ok: I use them from 20 years,
in production-systems, used by hundreds of people: at
change of millenium something was not going. But it was
a my error: in a form the year was of 2 digits, and writing
the year from localtime I wrote ( in 1987 )

form_year = TM.tm_year

In the first days of year 2000, the customer called me
saying that the form reported 07-01-100 instead of 07-01-00

Rather, be carefull with your system: look at TZ variable
and see if it is the case to use gmtime instead of localtime

c i a o
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top