How do i translate datevalues in unix to normal dates.
and how do i translate normal dates in to datevalues.
I'm using the unix-date.
Sample:
1067949360 to 4-11-03 12:36
and
4-11-03 12:36 to 1067949360
I want to built a script with a question to the user: give in date
the input will be 4-11-03 . I have to translate it to 1067940000 and run a script.
Afther that i'll want to print a rapport.
Quest what ??
A translation from 1067949360 to 4-11-03 12:36
Help.
Sorry for the bad englisch (just someone from holland):rolleyes:
Here's one I wrote a while ago. Converts epoch<->date.
/* convert time values: epoch<->human readable -- some code borrowed from comp.unix.programmer
*/
#include <time.h>
#include <ctype.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
time_t time, time_since_epoch;
int month_nr, hours, minutes, seconds, year;
struct tm time_str;
char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
if (argc > 7 || argc < 2) {
print_usage(argv[0]);
return(2);
}
for(i=0;i<strlen(argv[1]);i++) {
if(!isdigit(argv[1])) {
break; /* any non-digits means its string->epoch */
} else { /* epoch->string */
time = atol(argv[1]);
printf("%s\n", ctime(&time));
return(0);
}
month_nr = -1;
for (i = 0; i <= 11; i++)
if (strcmp(months, argv[2]) == 0) {
month_nr = i;
break;
}
if (month_nr == -1) {
printf("\n\nERROR: Read in non-month string \"%s\", when\n");
printf(" expected to get a month string such as \"Dec\".\n\n", &argv[2]);
print_usage(&argv[0]);
return(2);
}
time_str.tm_mon = month_nr;
time_str.tm_mday = atoi(argv[3]);
/* argv[4] is a string with the format hh:mm:ss E.g. 21:05:32 */
sscanf(argv[4], "%d:%d:%d",
&time_str.tm_hour, &time_str.tm_min, &time_str.tm_sec);
/* We don't use argv[5], the time zone. E.g. "EDT" */
year = atoi(argv[6]);
if ( (year < 1900) || (year > 2038) ) {
print_usage(argv[0]);
return(2);
}
else
year -= 1900;
/* "year" should now contain the nr. of years since 1900 ! E.g. 95 */
time_str.tm_year = year;
/* Don't know if daylight savings time is in effect or not. By setting
* this variable to -1, 'mktime()' will figure it out and adjust for it. */
time_str.tm_isdst = -1;
time_since_epoch = mktime(&time_str);
if ( time_since_epoch == -1) {
printf("\nThe calendar time cannot be represented\n\n");
print_usage(argv[0]);
return(2);
}
printf("The time_since_epoch is %d sec.\n\n", time_since_epoch);
}
print_usage(char *program_name)
{
printf("\nUsage: %s day_of_week month_name month_date, time_string time_zone year\n", program_name);
printf(" or: %s epoch\n",program_name);
printf(" Ex: %s Thu Oct 9 14:38:46 PDT 2003\n\n",program_name);
}