10 Replies - 3890 Views - Last Post: 21 January 2010 - 08:46 AM Rate Topic: -----

#1 Miklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-December 09

DATE and TIME using C

Post icon  Posted 10 January 2010 - 04:12 PM

Once again here I am with my questions...!!

I have the following piece of code working great in my DEVC++, but I am trying to make a program for linux OS.
As you may have already understood I get some errors when compiling so I need an expert's eye to guide me!!

Here is the snipet
#include <stdio.h>
#include <time.h>

/*** LOOKS STUPID BUT I DID THIS TO LOOK AS A READY TO BE USED FUNCTION ***/
void timeNdate();

void main()
{
   timeNdate();
}
/*** THIS FUNCTION SHOWS LOCAL DATE AND TIME ***/
void timeNdate()
{
   char dateStr [9];
   char timeStr [9];
   _strdate( dateStr );
   printf( "The current date is %s \n", dateStr);
   _strtime( timeStr );
   printf( "The current time is %s \n", timeStr);
}

and here are the errors:

snowy:~$ gcc time.c -o time
/tmp/ccQl7G44.o: In function `timeNdate':
time.c:(.text+0x27): undefined reference to `_strdate'
time.c:(.text+0x48): undefined reference to `_strtime'
collect2: ld returned 1 exit status
snowy:~$

This post has been edited by Miklo: 10 January 2010 - 04:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: DATE and TIME using C

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: DATE and TIME using C

Posted 10 January 2010 - 04:41 PM

Use the tm structure.


time_t timer;
	struct tm *ptm;
	 char date[15];
	 // Get the time in seconds since Jan 1, 1970
	time(&timer);
	 
	 // Get the time into the struct tm
	ptm = localtime(&timer);
	sprintf(date, "%2d/%02d/%02d", ptm->tm_mon+1, ptm->tm_mday, ptm->tm_year%100);
	 
	printf("%s\n\n", date);


Was This Post Helpful? 0
  • +
  • -

#3 Miklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-December 09

Re: DATE and TIME using C

Posted 10 January 2010 - 05:40 PM

View PostKYA, on 10 Jan, 2010 - 03:41 PM, said:

Use the tm structure.


time_t timer;
	struct tm *ptm;
	 char date[15];
	 // Get the time in seconds since Jan 1, 1970
	time(&timer);
	 
	 // Get the time into the struct tm
	ptm = localtime(&timer);
	sprintf(date, "%2d/%02d/%02d", ptm->tm_mon+1, ptm->tm_mday, ptm->tm_year%100);
	 
	printf("%s\n\n", date);



When Compiling this particular piece of code.... i get a bunch of errors.. :stupid:
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: DATE and TIME using C

Posted 10 January 2010 - 06:05 PM

You need to include the c time library #include <time.h> if you're using C and #include <ctime> in C++.

Posting what you're trying to compile with errors would be helpful.
Was This Post Helpful? 0
  • +
  • -

#5 Miklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-December 09

Re: DATE and TIME using C

Posted 11 January 2010 - 06:25 AM

View PostKYA, on 10 Jan, 2010 - 05:05 PM, said:

You need to include the c time library #include <time.h> if you're using C and #include <ctime> in C++.

Posting what you're trying to compile with errors would be helpful.



without touching anything i get these:
snowy:~$ gcc time.c -o time
time.c:23: error: expected declaration specifiers or ‚...‚ before ‚&‚ token
time.c:23: warning: data definition has no type or storage class
time.c:23: error: conflicting types for ‚time‚
/usr/include/time.h:187: error: previous declaration of ‚time‚ was here
time.c:26: warning: data definition has no type or storage class
time.c:26: error: conflicting types for ‚ptm‚
time.c:20: error: previous declaration of ‚ptm‚ was here
time.c:26: warning: initialization makes integer from pointer without a cast
time.c:26: error: initializer element is not constant
time.c:27: error: expected ‚)‚ before string constant
time.c:29: error: expected declaration specifiers or ‚...‚ before string constant
time.c:29: error: expected declaration specifiers or ‚...‚ before ‚date‚
time.c:29: warning: data definition has no type or storage class
time.c:29: error: conflicting types for ‚printf‚
time.c:29: note: a parameter list with an ellipsis can‚t match an empty parameter name list declaration
snowy:~$

i did include:
#include <stdio.h>
#include <time.h>
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: DATE and TIME using C

Posted 11 January 2010 - 06:50 AM

Show your current code.
Was This Post Helpful? 0
  • +
  • -

#7 Miklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-December 09

Re: DATE and TIME using C

Posted 11 January 2010 - 06:58 AM

View PostJackOfAllTrades, on 11 Jan, 2010 - 05:50 AM, said:

Show your current code.



I am currently working on it so give me a couple of hours to practice a little bit. I am trying to learn how to code in C so i don't want to ask for further help unless I am desperate by trying and getting no results.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: DATE and TIME using C

Posted 11 January 2010 - 08:23 AM

All it takes is a copy paste of what you have in your editor and we can have the issue fixed immediately.

Based on the errors it doesn't seem to have any thing to do with what I posted.
Was This Post Helpful? 0
  • +
  • -

#9 Miklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-December 09

Re: DATE and TIME using C

Posted 11 January 2010 - 11:57 AM

View PostKYA, on 11 Jan, 2010 - 07:23 AM, said:

All it takes is a copy paste of what you have in your editor and we can have the issue fixed immediately.

Based on the errors it doesn't seem to have any thing to do with what I posted.



Well actually it had.... it was my fault forgetting to add all this into a function... I suppose I was too dizzy to think that there should be a function doing all these... :blink: I really feel stupid sometimes...

Now I am trying to figure out how can I actually use those info in action.. I mean how can I compare them to user's input. The good thing is that I am dealing with integers which easier to compare than strings right??? But i still need some accuracy on calculating dates because the major idea is to use them for reservations etc.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: DATE and TIME using C

Posted 11 January 2010 - 12:45 PM

A compare is a compare. Complexity wise a string compare is O(n) [where n is the number of characters in the string] whereas any other primitive data type is O(1).


Asking random questions without any context is going to get you nowhere fast. I highly suggest you post a snippet of what you are referring to when you ask.
Was This Post Helpful? 0
  • +
  • -

#11 pbrown386  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-January 10

Re: DATE and TIME using C

Posted 21 January 2010 - 08:46 AM

I am trying to add 3 days to a current date and am having trouble defining the date, I don't have any issues with getting 3 days added just having issues trying to pull the current date of any file and adding 3 days:

Here is my code, any suggestions?

#include "stdafx.h"
#include <iostream>
#include time.h

using namespace std;

int AddDays(int date, int nDays)
{
   
		int newDate = 0;

		return newDate;
}


int main(int argc, char* argv[])
{
	time_t date;
	
	int test = 0;

	test = AddDays(20100131, 3);

	cout << test << "\n";
	return 0;
}



Thanks for the help.

This post has been edited by pbrown386: 21 January 2010 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1