Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > using localtime

Reply
Thread Tools

using localtime

 
 
Raj
Guest
Posts: n/a
 
      03-02-2005
Hi

I'm very new to C and am trying to get the time a month ago in the format
mmyy.

What I have tried so far is:

char *last_mth;
size_t maxsize = 4;
struct tm *timeptr = localtime(time(0));
strftime(last_mth, maxsize, "%m%y", timeptr);

to get the current mmyy, but I don't know how to access the tm struct so
that I can decrement the month.

I know this is the simplest of things to do, but I haven't found it in the
FAQ. If this is a silly request, please let me know. I'm off to look in
tutorials in case they can tell me there.

Thanks in advance for any help provided.

Regards,
--
Raj Kothary :: one|concept
http://www.oneconcept.net
http://www.velocityreviews.com/forums/(E-Mail Removed)
+ 44 (0)79 5647 2746

oneconcept limited :: 17 York Avenue, Stanmore, Middlesex HA7 2HT

Confidentiality notice:
The information transmitted in this email and/or any attached document(s) is
confidential and intended only for the person or entity to which it is
addressed and may contain privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon this
information by persons or entities other than the intended recipient is
prohibited. If you received this in error, please contact the sender and
delete the material from any computer.


 
Reply With Quote
 
 
 
 
Christian Kandeler
Guest
Posts: n/a
 
      03-02-2005
Raj wrote:

> I'm very new to C and am trying to get the time a month ago in the format
> mmyy.
>
> What I have tried so far is:
>
> char *last_mth;
> size_t maxsize = 4;
> struct tm *timeptr = localtime(time(0));
> strftime(last_mth, maxsize, "%m%y", timeptr);
>
> to get the current mmyy, but I don't know how to access the tm struct so
> that I can decrement the month.


These are the members of struct tm:

int tm_sec; // seconds after the minute -- [0, 60]
int tm_min; // minutes after the hour -- [0, 59]
int tm_hour; // hours since midnight -- [0, 23]
int tm_mday; // day of the month -- [1, 31]
int tm_mon; // months since January -- [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday -- [0, 6]
int tm_yday; // days since January 1 -- [0, 365]
int tm_isdst; // Daylight Saving Time flag

So to decrease the month value you simply do this:
timeptr->tm_mon -= 1;
The case where tm_mon is already zero is left as an exercise...


Christian
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-02-2005
Raj wrote:

> Hi
>
> I'm very new to C and am trying to get the time a month ago in the format
> mmyy.
>
> What I have tried so far is:
>
> char *last_mth;
> size_t maxsize = 4;
> struct tm *timeptr = localtime(time(0));


Why did you ignore the compiler's complaint about
this line? I was going to write you a nice explanation
of what was going wrong and how to fix it -- in fact, I
actually did write it -- but decided it would be wasted
on someone who won't even accept help from his compiler.

Please work on your code at least until the compiler
accepts it without complaint, or until you get a complaint
that you find baffling. Then post a minimal, complete
program demonstrating the problem at hand -- once you've
given evidence of having made an honest try, I for one
will be happy to give help.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
tigervamp
Guest
Posts: n/a
 
      03-02-2005
Raj wrote:
> Hi
>
> I'm very new to C and am trying to get the time a month ago in the

format
> mmyy.
>
> What I have tried so far is:
>
> char *last_mth;
> size_t maxsize = 4;
> struct tm *timeptr = localtime(time(0));
> strftime(last_mth, maxsize, "%m%y", timeptr);


There are many mistakes with your code. Firstly, you pass an
uninitialized pointer (last_mth) to strftime. Next, maxsize should be
at least 5, strings in C are terminated with a nul character, you need
to leave room for strftime to add this. The time function returns a
value of type time_t, localtime takes a pointer to time_t, you are
passing a time_t to localtime, not a pointer.

Here is a working version (without error checking):

#include <stdio.h>
#include <time.h>

int main (void) {
char last_month[10];
size_t length = sizeof(last_month)/sizeof(last_month[0]);
time_t mytime = time(NULL);
struct tm *timeptr = localtime(&mytime);

strftime(last_month, length, "%m%y", timeptr);
printf("%s\n", last_month);
return 0;
}

> to get the current mmyy, but I don't know how to access the tm struct

so
> that I can decrement the month.


The tm struct contains members tm_mday, tm_mon, and tm_year (among
others) which are integers representing day of month, month of year,
and year respectively, month is zero based. So, to answer your
question, just decrement the tm_mon member of an initialized struct tm:

timeptr->tm_mon--;

Of course you will want to check if the current month is 0 (January)
and if so you probably want to set the month to 11 (December) and
decrement the year. Then you have to decide what to do if the day in
the current month does not exist in the new month, etc.

> I know this is the simplest of things to do, but I haven't found it

in the
> FAQ. If this is a silly request, please let me know. I'm off to

look in
> tutorials in case they can tell me there.


You should try taking a look at your library documentation or pick up a
good C book.

> Thanks in advance for any help provided.
>
> Regards,
> --
> Raj Kothary :: one|concept
> http://www.oneconcept.net
> (E-Mail Removed)
> + 44 (0)79 5647 2746


Rob Gamble

 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      03-02-2005


Raj wrote:
> Hi
>
> I'm very new to C and am trying to get the time a month ago in the format
> mmyy.
>
> What I have tried so far is:
>
> char *last_mth;
> size_t maxsize = 4;
> struct tm *timeptr = localtime(time(0));
> strftime(last_mth, maxsize, "%m%y", timeptr);
>
> to get the current mmyy, but I don't know how to access the tm struct so
> that I can decrement the month.
>


Function localtime takes a pointer argument. Also, you
need to first check the value returned from function time
to make sure that the value represents time, then use a
pointer to the time_t value should it represent a valid time.
To reduce the month after the function localtime call, use:
timeptr->tm_mon--; or timeptr->tm_mon -= 1;
Then use function mktime to normalize the values in the struct
tm. You can put all this in a function. Example below.

#include <stdio.h>
#include <time.h>

char *MinusOneMonth(time_t tvalue)
{
struct tm *t;
static char stime[5];

if(tvalue != (time_t)-1)
{
t = localtime(&tvalue);
t->tm_mon--;
if((tvalue = mktime(t)) != (time_t)-1)
{
int tmp = t->tm_year%100;
sprintf(stime,"%02d%02d",t->tm_mon+1,tmp);
}
}
return tvalue==(time_t)-1?NULL:stime;
}

int main(void)
{
char *s;
struct tm *t;
time_t now = time(NULL);

if((s = MinusOneMonth(now)) != NULL)
{
printf("Now is: %sOne month ago in "
"format(mmyy) is: \"%s\" \n",ctime(&now), s);
puts("\nTest for the month Jan\n");
t = localtime(&now);
t->tm_mon = 0; /* Makes it month January */
now = mktime(t);
if((s = MinusOneMonth(now)) != NULL)
printf("Now is: %sOne month ago "
"in format(mmyy) is: \"%s\"\n",ctime(&now), s);
else puts("Time is not available");
}
else puts("Time is not available");
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: (E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
tigervamp
Guest
Posts: n/a
 
      03-02-2005

Al Bowers wrote:
> Raj wrote:
> > Hi
> >
> > I'm very new to C and am trying to get the time a month ago in the

format
> > mmyy.
> >
> > What I have tried so far is:
> >
> > char *last_mth;
> > size_t maxsize = 4;
> > struct tm *timeptr = localtime(time(0));
> > strftime(last_mth, maxsize, "%m%y", timeptr);
> >
> > to get the current mmyy, but I don't know how to access the tm

struct so
> > that I can decrement the month.
> >

>
> Function localtime takes a pointer argument. Also, you
> need to first check the value returned from function time
> to make sure that the value represents time, then use a
> pointer to the time_t value should it represent a valid time.
> To reduce the month after the function localtime call, use:
> timeptr->tm_mon--; or timeptr->tm_mon -= 1;
> Then use function mktime to normalize the values in the struct
> tm. You can put all this in a function. Example below.
>
> #include <stdio.h>
> #include <time.h>
>
> char *MinusOneMonth(time_t tvalue)
> {
> struct tm *t;
> static char stime[5];
>
> if(tvalue != (time_t)-1)
> {
> t = localtime(&tvalue);
> t->tm_mon--;
> if((tvalue = mktime(t)) != (time_t)-1)
> {
> int tmp = t->tm_year%100;
> sprintf(stime,"%02d%02d",t->tm_mon+1,tmp);
> }
> }
> return tvalue==(time_t)-1?NULL:stime;
> }
>
> int main(void)
> {
> char *s;
> struct tm *t;
> time_t now = time(NULL);
>
> if((s = MinusOneMonth(now)) != NULL)
> {
> printf("Now is: %sOne month ago in "
> "format(mmyy) is: \"%s\" \n",ctime(&now), s);
> puts("\nTest for the month Jan\n");
> t = localtime(&now);
> t->tm_mon = 0; /* Makes it month January */
> now = mktime(t);
> if((s = MinusOneMonth(now)) != NULL)
> printf("Now is: %sOne month ago "
> "in format(mmyy) is: \"%s\"\n",ctime(&now), s);
> else puts("Time is not available");
> }
> else puts("Time is not available");
> return 0;
> }


This will produce incorrect results if the current day of the month
does not exist in the previous month, for example if the current day is
Dec 31, or Mar 30, etc.

> --
> Al Bowers
> Tampa, Fl USA
> mailto: (E-Mail Removed) (remove the x to send email)
> http://www.geocities.com/abowers822/


Rob Gamble

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem using timelocal, localtime to discern d-o-w. John Perl Misc 2 03-22-2007 02:12 PM
Daylight Savings using timelocal() and localtime() Datamon Perl Misc 1 03-14-2007 10:06 PM
localtime problems with month Gerry George Perl 1 05-26-2004 08:26 PM
Time::localtime problems Gerry George Perl 2 05-26-2004 05:59 PM
Converting for localtime() or gmtime() to time() Stephen Perl 1 05-19-2004 03:51 PM



Advertisments