Translate date value to normal date and backwards.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Translate date value to normal date and backwards.
# 1  
Old 12-05-2003
Question 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)Smilie
# 2  
Old 12-06-2003
Here's one I wrote a while ago. Converts epoch<->date.

Code:
/* 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][i])) {
                        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[i], 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);
}

# 3  
Old 12-08-2003
Code:
#! /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

# 4  
Old 12-10-2003
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")
# 5  
Old 12-10-2003
Thanks a lot for de codes.
In all the codes where usefull parts.
my script is working.

Tanks,Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date 19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

4. Shell Programming and Scripting

Date Time Zone Conversion (backwards of what I want)

I am trying to convert local time to time in Ireland. Instead it is going the opposite direction (taking the local time as it if were in Ireland and displaying that the time would be here). $ echo "$TZ"; date; date --date='TZ="Europe/Dublin" '"$(date)" America/Phoenix Mon, Apr 13, 2015... (7 Replies)
Discussion started by: Michael Stora
7 Replies

5. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

6. Homework & Coursework Questions

expr to translate the date command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. Write a script called "tod" that will display the time of day in am or pm notation rather then the 24 hour clock time. Use expr to convert from 24-hour clock time. Use... (13 Replies)
Discussion started by: linuxtraining
13 Replies

7. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. Shell Programming and Scripting

Translate decimal into date

Hello, what can I do to convert a decimal number (001-366) into the day of the year it represent in the format of mm-dd-yyyy ? I know about the date +%j, it gives me the number for the current date, what about a reverse of this, is there such a thing? My number resides in a var, what can I do to... (3 Replies)
Discussion started by: gio001
3 Replies

9. UNIX for Dummies Questions & Answers

translate to normal english

lnode * head = temp; (1 Reply)
Discussion started by: rickym2626
1 Replies

10. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies
Login or Register to Ask a Question

Featured Tech Videos