473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

using localtime

Raj
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
ra*@oneconcept.net
+ 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.
Nov 14 '05 #1
5 2613
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
Nov 14 '05 #2
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
es*****@acm-dot-org.invalid
Nov 14 '05 #3
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
ra*@oneconcept.net
+ 44 (0)79 5647 2746


Rob Gamble

Nov 14 '05 #4


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: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5

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: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


Rob Gamble

Nov 14 '05 #6

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

Similar topics

4
by: Dariusz | last post by:
Can anyone help me out on how to get this time dependent code to work. I want it to display a greeting depending on the hour of the day (I'm trying to convert my own Perl code to PHP with no luck...
1
by: Udo Melis | last post by:
udo:/usr/lib/cgi-bin# python Python 2.3.4 (#2, Jul 28 2004, 09:39:34) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from time import localtime Traceback...
2
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
3
by: david | last post by:
I used the localtime() function in the following code snippet: .... tm = localtime(&ps->date); printf("---------------------\n"); printf("Date: %d/%02d/%02d %02d:%02d:%02d\n", 1900 +...
1
by: KW | last post by:
Hi all, Appreciate if someone can help me out on this. Currently, I have a tm structure holding information of the UTC time, which is very likely to be in the past, meaning not the current...
7
by: Peter Jansson | last post by:
Dear group, I have been struggling to get a simple program for inserting and extracting std::tm objects to/from streams to work. The code below tries to read a std::tm object from a...
2
by: janama | last post by:
Hi all, Using wx When adding a second timer as i have the first, the second timer adding stops the first timer (updating or stops?) . In this example im updating a uptime and localtime label....
16
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want to my code to print the timestamps and each one to include...
1
by: maruk2 | last post by:
I have some old data files with old timestamps, where timestmap=time(NULL), some of them date back to the year 1999. I want my code to print the timestamps and each one to include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.