Translate date value to normal date and backwards.

Hello,

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);
}

#! /usr/bin/perl -w
use Time::Local;
$sec=0;
$min=36;
$hour=12;
$mday=5;
$mon=11;
$year=103;
#4-11-03 12:36 ### use this date and time for example

$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
print "DATE:$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst\n";
print "TIME: $time\n";
~
"test.pl" 73 lines, 1946 characters
[qgatu003]/export/home/mxdooley/mbi_script$./test.pl
DATE:0,36,12,5,11,103,5,338,0
TIME: 1070649360

You can also use Python to do the conversion as
shown in the following example.

  • Finnbarr

import os, sys, datetime
import time

usage = "Usage: %s [s | d] value" %os.path.basename(sys.argv[0])

try:
mode = sys.argv[1]
value = sys.argv[2]
except IndexError:
print usage;
sys.exit(0)

if (mode != "s" and mode != "d"):
print usage;
sys.exit(1);

if (mode == "s"):
fpm=time.strptime(value, "%d-%m-%y %H:%M")
print "Date --> Epoch: ", int(time.mktime(fpm))
else:
print "Epoch --> Date: ", datetime.datetime.fromtimestamp(float(value)).strftime("%d-%m-%y %H:%M")

Thanks a lot for de codes.
In all the codes where usefull parts.
my script is working.

Tanks,:smiley: