Hi guys,

I'm writing a program that can read the date and the time separately. The only function I knew is ctime but it reads both of them, so is there any functions that can reed the date as:
month day, year
and time as: hour::minute::second

Thanks!

Recommended Answers

All 6 Replies

Read? Sound like from a file or user input. Or do you mean get the time from the system?

I really don't see the need to get both separately. Get them together and move the DATE into the date variable, move the TIME into the time variable.

I'm actually not sure if there are separate functions.

first thanks for replying but I im doing a TCP/IP time server and when the user (client) ask for the time, I need to read them separately. how can I move them? can u please clarify it?

you mean something like this:

const char* gettime()
{
    static char curtime[20];
    time_t now = time(0);
    struct tm* tm = localtime(&now);
    sprintf(curtime,"%02d:%02d:%02d",
          tm->tm_hour, tm->tm_min, tm->tm_sec);
    return curtime;
}

Thank you Ancient, but how about the date? can I use the same codes?
I need to reed it as: month day, year

Thanks!

Yes, do the same thing for date. There is another way to write that function to avoid the static declaration of the buffer.

const char* gettime(char *inputbuf)
{
   // other code here
    return inputbuf;
}

You could also use strftime() but I never use that function because doing it myself is just as easy.

Look at struct tm in time.h and you will see the names of the date and time members.

Can someone please tell me whats wrong with my codes?
I run it using a command
gcc -o output yourfile.c -lsocket -lnsl -lresolv
however, when I run the output file I don't see any output?
This codes for a server side I want to test it so I can do the client side.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>

#define SERVERPORT "21708" 
#define MAXLEN 100


void *get_in_addr(struct sockaddr *sa)
{
  if (sa->sa_family == AF_INET) {
    return &(((struct sockaddr_in*)sa)->sin_addr);
  }

  return &(((struct sockaddr_in6*)sa)->sin6_addr);
}


main() 
{ 
	int sockfd, newfd;    // listen on sockfd, new connection on newfd
	unsigned int len;
	socklen_t sin_size;
	char msg[80];
	char buf[MAXLEN];
	int st, rv;
	struct addrinfo hints, *serverinfo, *p; 
	struct sockaddr_storage client;
	char s[INET6_ADDRSTRLEN];
	time_t rawtime;

	
	
	
  //zero struct	
  memset(&hints,0,sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;

  if((rv = getaddrinfo(NULL, SERVERPORT, &hints, &serverinfo ) != 0)){
    perror("getaddrinfo");
    exit(-1);
  }

   // loop through all the results and bind to the first we can
  for( p = serverinfo; p != NULL; p = p->ai_next){
    if( (sockfd = socket( p->ai_family, p->ai_socktype, p->ai_protocol )) == -1 ) {
      perror("socket");
      continue;
    }
    
    if(bind(sockfd, p->ai_addr, p->ai_addrlen) == -1){
      close(sockfd);
      perror("bind");
      continue;
    }
    
    break;
  }

  if( p == NULL ){
    perror("Fail to bind");
	  
  }
	

  // all done with this structure, free the memory
  freeaddrinfo(serverinfo);
  
  if( listen(sockfd,5) == -1 ){
    perror("Listen");
    exit(-1);
  }

  len = sizeof client;

  while(1){
    if( ( newfd = accept(sockfd, (struct sockaddr *)&client, &len) ) == -1 ){
      perror("Accept");
      exit(-1);
    }

    if( (rv = recv(newfd, buf, MAXLEN-1, 0 )) == -1) {
      perror("Recv");
      exit(-1);
    }
	  struct sockaddr_in *clientAddr = ( struct sockaddr_in *) get_in_addr((struct sockaddr *)&client);
	  inet_ntop(client.ss_family, clientAddr, s, sizeof s);
	  printf("Calender server has TCP Port %d and IP address %d ", SERVERPORT);
	  printf("Query type is %s\n", buf);  // Time or Date
	  
	  struct tm * timeinfo; // get the time
	  time ( &rawtime );
	  timeinfo = localtime ( &rawtime );
	  
	  strftime (msg,80,"Time is: %H::%M::%S",timeinfo);
	  
    if( ( st = send(newfd, msg, strlen(msg), 0)) == -1 ) {
      perror("Send");
      exit(-1);
    }
    printf("Send Time/Date to client with IP %s and port %d\n", s, ntohs(clientAddr->sin_port) );

	  
	

	  
	 return 0;
	
    close(newfd);
  }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.