Sponsored Content
Full Discussion: compile a c program
Top Forums Programming compile a c program Post 302169746 by jim mcnamara on Friday 22nd of February 2008 10:05:39 AM
Old 02-22-2008
It was easier to rewrite this than to explain the problems.
Code:
/*
Listing 6 get_date_from_secs.c
get_date_from_secs.c
pass the number of seconds since
the Epoch, and return a Gregorian
date argument
use "%02d" date format.

*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv)
{
	struct tm *rettm;  /* use a pointer */
	time_t now=time(NULL);  /* add a value to now */
	char timebuf[128]={0x0}; /* place to park date time values */

	/* grab the first argument, time in seconds Note:  will segfault the old way*/
	if(argc < 2)   /* argc has to be > 1 */
	{ /* error */
		fprintf(stderr, "bad arguments -1\n"); /* you can also user perror() here */
		exit(1);
	}
	/* zero is a valid number and is valid for epoch seconds */
	errno=0;  /* set errno to a known state */
	if((now=atol(argv[1])) == 0 && errno !=0) /* atol never returns a NULL use argv */
	{ /* error */
	  /* you get to add errno checking here */
		fprintf(stderr,"invalid conversion -2\n"); /* you can also user perror() here */
		exit(1);
	}
	rettm=localtime(&now);  /* you can check for a NULL return here if you want */
	/* check your man page - some implementations only allow struct tm > Dec 13 20:45:52 UTC 1901. */
	/* use std library for time/date  use strftime */

	printf("%02d%02d%02d%02d\n", rettm->tm_mon+1, rettm->tm_mday, rettm->tm_hour, rettm->tm_min);
	strftime(timebuf, sizeof(timebuf),"%m %d %H %M %S", rettm);
	printf("%s\n", timebuf);

    return 0;
}

 

10 More Discussions You Might Find Interesting

1. Programming

how to compile a program

how do i go about compiling a simple hello world script in mandrake linux? this is something i have had no luck in finding on the main site, please help? thank you (2 Replies)
Discussion started by: CmpKillr
2 Replies

2. Programming

How to compile a c program by using gcc

Hi all, Yeasterday I try to compile c program by using cygwin. I just find an errors the fist one is concerinig about the end of the line. To summit my Assignment which is the day after tommorow I have to compile my c program by using just gcc. If any one know what do I have to... (5 Replies)
Discussion started by: Bell
5 Replies

3. Programming

how to compile a program statically

how can i do static compilation in cc and -lldap i have system defined and user defined header file. Can any one suggest any site where from i can get some information about static and dynamic compilation. Thankx (1 Reply)
Discussion started by: bhakti
1 Replies

4. BSD

How to compile a c program in freeBSD

Hi , I have freeBSD installed. I need to compile a c program which has embedded informix sql statement in it. Can you please help me to to do the same. I need to know what I should I do to make BSD compatable to compile the c program ? Thanks in advance Jisha (3 Replies)
Discussion started by: jisha
3 Replies

5. Programming

Compile c program on Sun Solaris OS

Can anyone tell me how to compile a c programs on SunSolaris OS 5.1 Version as gcc and cc are not working Thanks!!!! (4 Replies)
Discussion started by: shivu
4 Replies

6. Programming

compile a program in C with teradata sentences using cc

hi, I want to compile a program in C. It have a multiple calls to teradata. I have no idea how to compile in Aix. The compiler that I should be use is cc. I tried cc -G -KPIC tdsfbd0358.c this generates a tdsfbd0358.i and after I have no Idea what I have to make, a link? how?... (3 Replies)
Discussion started by: kajum
3 Replies

7. UNIX for Advanced & Expert Users

compile a c program in a encrypted way

Hi Guys, I wonder I had have a look to the cc compile options but I could be missing one but basically I'm compliling a c program where I will storing a command to connect to a database and also userid and password. The issue is that after the module is generated using a command like strings I... (14 Replies)
Discussion started by: arizah
14 Replies

8. Programming

Not able to compile C program on z/OS (USS)

Hi, I having an issue while compiling a C program in USS (z/OS) machine. I was able to create objest files (.o) from source (.c) files but when I try to create a binary file from the object files I am getting the below error. $ cc util.o sock.o app.o -lnsl -o ptf FSUM3067 The archive... (7 Replies)
Discussion started by: madhu84
7 Replies

9. UNIX and Linux Applications

Cannot compile/install gnu program

Hello, I'm trying to install ansifilter which is a program that translates ansi text to HTML. The idea is to install it on AIX 7.1 but is failing. All de appropriate gcc libraries and requirements for this software were installed successfully, but when I do the gmake I get the following... (4 Replies)
Discussion started by: bazajav
4 Replies

10. Programming

How to compile and run the ProC (*.pc) program?

Hi Team, I am very new to this forum and hope someone will help me in resolving the issue. I am new to Pro C also. I made some changes to the existing Pro C program and want to run the program with the changes. But I am unable to neither compile nor run the program. Please do the... (2 Replies)
Discussion started by: prakashs1218
2 Replies
GETDATE(3)						     Linux Programmer's Manual							GETDATE(3)

NAME
getdate, getdate_r - convert a date-plus-time string to broken-down time SYNOPSIS
#include <time.h> struct tm *getdate(const char *string); extern int getdate_err; #include <time.h> int getdate_r(const char *string, struct tm *res); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): getdate(): _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED getdate_r(): _GNU_SOURCE DESCRIPTION
The function getdate() converts a string representation of a date and time, contained in the buffer pointed to by string, into a broken- down time. The broken-down time is stored in a tm structure, and a pointer to this structure is returned as the function result. This tm structure is allocated in static storage, and consequently it will be overwritten by further calls to getdate(). In contrast to strptime(3), (which has a format argument), getdate() uses the formats found in the file whose full pathname is given in the environment variable DATEMSK. The first line in the file that matches the given input string is used for the conversion. The matching is done case insensitively. Superfluous whitespace, either in the pattern or in the string to be converted, is ignored. The conversion specifications that a pattern can contain are those given for strptime(3). One more conversion specification is specified in POSIX.1-2001: %Z Timezone name. This is not implemented in glibc. When %Z is given, the structure containing the broken-down time is initialized with values corresponding to the current time in the given timezone. Otherwise, the structure is initialized to the broken-down time corresponding to the current local time (as by a call to local- time(3)). When only the weekday is given, the day is taken to be the first such day on or after today. When only the month is given (and no year), the month is taken to be the first such month equal to or after the current month. If no day is given, it is the first day of the month. When no hour, minute and second are given, the current hour, minute and second are taken. If no date is given, but we know the hour, then that hour is taken to be the first such hour equal to or after the current hour. getdate_r() is a GNU extension that provides a reentrant version of getdate(). Rather than using a global variable to report errors and a static buffer to return the broken down time, it returns errors via the function result value, and returns the resulting broken-down time in the caller-allocated buffer pointed to by the argument res. RETURN VALUE
When successful, getdate() returns a pointer to a struct tm. Otherwise, it returns NULL and sets the global variable getdate_err to one of the error numbers shown below. Changes to errno are unspecified. On success getdate_r() returns 0; on error it returns one of the error numbers shown below. ERRORS
The following errors are returned via getdate_err (for getdate()) or as the function result (for getdate_r()): 1 The DATEMSK environment variable is not defined, or its value is an empty string. 2 The template file specified by DATEMSK cannot be opened for reading. 3 Failed to get file status information. 4 The template file is not a regular file. 5 An error was encountered while reading the template file. 6 Memory allocation failed (not enough memory available). 7 There is no line in the file that matches the input. 8 Invalid input specification. ENVIRONMENT
DATEMSK File containing format patterns. TZ, LC_TIME Variables used by strptime(3). CONFORMING TO
POSIX.1-2001. NOTES
The POSIX.1-2001 specification for strptime(3) contains conversion specifications using the %E or %O modifier, while such specifications are not given for getdate(). In glibc, getdate() is implemented using strptime(3), so that precisely the same conversions are supported by both. EXAMPLE
The program below calls getdate() for each of its command-line arguments, and for each call displays the values in the fields of the returned tm structure. The following shell session demonstrates the operation of the program: $ TFILE=$PWD/tfile $ echo '%A' > $TFILE # Full weekday name $ echo '%T' >> $TFILE # ISO date (YYYY-MM-DD) $ echo '%F' >> $TFILE # Time (HH:MM:SS) $ date $ export DATEMSK=$TFILE $ ./a.out Tuesday '2009-12-28' '12:22:33' Sun Sep 7 06:03:36 CEST 2008 Call 1 ("Tuesday") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 9 tm_mon = 8 tm_year = 108 tm_wday = 2 tm_yday = 252 tm_isdst = 1 Call 2 ("2009-12-28") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 28 tm_mon = 11 tm_year = 109 tm_wday = 1 tm_yday = 361 tm_isdst = 0 Call 3 ("12:22:33") succeeded: tm_sec = 33 tm_min = 22 tm_hour = 12 tm_mday = 7 tm_mon = 8 tm_year = 108 tm_wday = 0 tm_yday = 250 tm_isdst = 1 Program source #define _GNU_SOURCE 500 #include <time.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct tm *tmp; int j; for (j = 1; j < argc; j++) { tmp = getdate(argv[j]); if (tmp == NULL) { printf("Call %d failed; getdate_err = %d ", j, getdate_err); continue; } printf("Call %d ("%s") succeeded: ", j, argv[j]); printf(" tm_sec = %d ", tmp->tm_sec); printf(" tm_min = %d ", tmp->tm_min); printf(" tm_hour = %d ", tmp->tm_hour); printf(" tm_mday = %d ", tmp->tm_mday); printf(" tm_mon = %d ", tmp->tm_mon); printf(" tm_year = %d ", tmp->tm_year); printf(" tm_wday = %d ", tmp->tm_wday); printf(" tm_yday = %d ", tmp->tm_yday); printf(" tm_isdst = %d ", tmp->tm_isdst); } exit(EXIT_SUCCESS); } SEE ALSO
time(2), localtime(3), setlocale(3), strftime(3), strptime(3), feature_test_macros(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2010-09-20 GETDATE(3)
All times are GMT -4. The time now is 09:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy