LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2004, 02:13 PM   #1
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Rep: Reputation: 15
C++ time problems


Hi
Im learning c++ but am having problems with time. I just want the simplest method (still learning and all the methods i have seen are using pointers) to represent todays date as (int date, int month, int year).

The book i have mentions using <ctime> which as i understand contains the int tm_year. I just can't work out how to initialise it . . .

ICan anyone help please ?
 
Old 11-22-2004, 02:56 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
How about:
Code:
time_t now;
struct tm *t;

now = time(0);
t = localtime(&now);
Then you can use members such as t->tm_mon, t->tm_hour, etc.
 
Old 11-23-2004, 08:32 AM   #3
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
i got it working

int day = t->tm_yday;

No idea how it works or about '->' but ill guess ill figure that out as i go along

Thanks
 
Old 11-23-2004, 10:10 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Let me explain. t is a pointer to struct tm. Usually, to dereference a pointer you use the * operator. So you think you'd be able to do *t.tm_yday, but the . operator has a higher precedence that the * operator so what happens is it takes the tm_yday member of the t struct and then tries to dereference that. Clearly not what you actually want to do.

So in order to force the * operator to happen before the . operator you could do (*t).tm_yday. And that's a perfectly valid way to do it. But some wise C guru decided that dereferencing a pointer to a struct was too common and too ugly that way to not have a better solution. That's where the -> operator comes in. (*t).tm_yday is the same as doing t->tm_yday.

I hope that clears things up a bit
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
X86-64 problems, BIG TIME. mickeyboa Fedora 4 11-27-2005 02:17 PM
ok more problems, this time with X Lleb_KCir Linux - Software 19 07-03-2005 05:08 PM
Time problems downinthemine Linux - Software 1 01-15-2004 09:23 AM
Time Problems eastj1974 Linux - Newbie 3 04-08-2003 06:50 PM
First Time Run Problems smart Linux - General 5 01-15-2003 07:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration