473,856 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding a day to time_t

Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
I'm using visual c++ 2k5.
Regards,

...ab
Jun 27 '08 #1
8 4195
On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote :
>Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #2
yes but how can it help me add days to my own specified date ?

...ab

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:c7******** *************** *********@4ax.c om...
On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote :
>>Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.

The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

--
Doug Harrison
Visual C++ MVP

Jun 27 '08 #3
On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
>The mktime function takes a broken-down local time value represented as a
struct tm, normalizes the struct fields, and returns a time_t.

yes but how can it help me add days to my own specified date ?
For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original post
talked about a broken down time in your own format, which you can easily
express as a struct tm for use with mktime. If you actually have time_t
values, you can convert them to struct tm with localtime or gmtime.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #4
Abubakar wrote:
Hi,
Lets say I have three integers (date, month, and year) as part of a struct
lets say like
struct mydate{ int day, month, year};
I want to add x number of days to this date. And it should be a real date
calculation thing. Like if the date is 31st december 2008, when i add 1 to
this, it should become 1st feb 2009.
This of course may require converting the above struct values to some data
type that crt's date functons can understand. I want to do that but havent
been able to yet. Thanks for help.
I'm using visual c++ 2k5.
Regards,
ab:

time_t measures time in seconds, so to add a day you add 86400.

--
David Wilkinson
Visual C++ MVP
Jun 27 '08 #5
I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
printf("%s", buf);

}

If there are any problems with the code please let me know.

Regards,

...ab

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:or******** *************** *********@4ax.c om...
On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
>>The mktime function takes a broken-down local time value represented as
a
struct tm, normalizes the struct fields, and returns a time_t.

yes but how can it help me add days to my own specified date ?

For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original post
talked about a broken down time in your own format, which you can easily
express as a struct tm for use with mktime. If you actually have time_t
values, you can convert them to struct tm with localtime or gmtime.

--
Doug Harrison
Visual C++ MVP

Jun 27 '08 #6
On Tue, 20 May 2008 19:34:39 +0500, "Abubakar" <q@y.comwrote :
>I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
printf("%s", buf);

}

If there are any problems with the code please let me know.
There's no point in setting tm_wday, because mktime calculates its value
from the other fields and overwrites it. Also, you need to set tm_isdst to
one of the values documented as meaningful for it.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #7
Ok cool. Thanks for the help.

...ab
There's no point in setting tm_wday, because mktime calculates its value
from the other fields and overwrites it. Also, you need to set tm_isdst to
one of the values documented as meaningful for it.

--
Doug Harrison
Visual C++ MVP
Jun 27 '08 #8
Abubakar wrote:
I have written something like:

void AddDate (int day, int month, int year, int daystoadd)
{
tm mt;
mt.tm_sec = 1;
mt.tm_min = 1;
mt.tm_hour = 1;
mt.tm_wday = day;
mt.tm_mon = month - 1;
mt.tm_year = year - 1900;
mt.tm_mday = day;

time_t tmptime = mktime (&mt);
So now you have a time_t. If you aren't worried about leap seconds, then
just add 24*60*60 to the time_t (which is measured in seconds) for each day.
tm * srctime = localtime (&tmptime);

char buf[100];
strftime (buf, 100, "%d/%m/%Y", srctime);
printf("%s + %d = ", buf, daystoadd);
// add days ...
srctime->tm_mday += daystoadd;
time_t finaldate = mktime (srctime);

strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
printf("%s", buf);

}

If there are any problems with the code please let me know.

Regards,

..ab

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:or******** *************** *********@4ax.c om...
>On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
>>>The mktime function takes a broken-down local time value
represente d as a
struct tm, normalizes the struct fields, and returns a time_t.

yes but how can it help me add days to my own specified date ?

For example, add x number of days to the tm_mday field before calling
mktime. Although your thread subject mentions time_t, your original
post talked about a broken down time in your own format, which you
can easily express as a struct tm for use with mktime. If you
actually have time_t values, you can convert them to struct tm with
localtime or gmtime. --
Doug Harrison
Visual C++ MVP

Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
25754
by: Anand CS | last post by:
Hi All I have question regarding time data structures... I have 64 bit unsigned microsecond resolution (unsigned __int64 for windows/and unsigned long long for others) variable. It stores the microsoconds elapsed since the Epoch... I want to convert this time into a string form.... AFAIK normally the procedure from a time_t structure to string format is something like...convert time_ to struct tm using localtime method......
4
2518
by: Dave Sinkula | last post by:
I have been told conflicting advice. Please offer comments regarding the following. ===== I visited Ben Pfaff's "When are casts appropriate?" page. http://www.msu.edu/~pfaffben/writings/clc/casts.html I mentioned to Ben Pfaff that a potential addition of an appropriate use of a cast was when comparing to the result of the time() function (and another
7
20485
by: John J. Hughes II | last post by:
Is there a better way of doing this? DateTime startTime = new DateTime(1970,1,1,0,0,0,0); TimeSpan currTime = DateTime.Now - startTime; UInt32 time_t = Convert.ToUInt32(Math.Abs(currTime.TotalSeconds)); Regards, John
0
1280
by: Zwyatt | last post by:
I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum; time_t aTime; }
0
1456
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
8
12346
by: Henrik Goldman | last post by:
I have strings like "2006-03-26 21.51" which I would like to convert into time_t. I know that the string is in localtime. So far I've written the following code: time_t LocalTimeFromString(string str) { struct tm t; memset(&t, 0, sizeof(t));
18
3931
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be the time when the program was started the first time. My platform is a DEC machine with Tru64 onboard. A possible explanation could be, that the time() function is called
45
9493
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error, ((time_t)-1) is returned, and errno is set
7
6037
by: Nick Keighley | last post by:
Hi, this is probably quite easy... How do I convert a UTC string into a time_t? eg. "2007-12-18 13:37:26" - a time_t. Now FAQ 13.3 says use mktime() to convert a struct tm into a time_t or you have to use some non-standard thing to parse the
0
9908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9760
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10777
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7930
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7090
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3198
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.