The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare date from db2 table to yesterday's Unix system date sasaliasim Shell Programming and Scripting 9 12-01-2008 11:37 PM
Perl: Extracting date from file name and comparing with current date MKNENI Shell Programming and Scripting 4 03-26-2008 04:01 PM
date issue-find prevoius date in a patricular format bsandeep_80 UNIX for Advanced & Expert Users 3 11-15-2007 08:42 PM
Changing Creation Date to a Prespecified Date of a File In Unix monkfan UNIX for Dummies Questions & Answers 4 11-28-2006 07:15 AM
a simple way of converting a date in seconds to normal date travian HP-UX 2 11-23-2006 12:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-05-2003
Frederik Frederik is offline
Registered User
  
 

Join Date: Dec 2003
Location: Houten, NL
Posts: 2
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)
  #2 (permalink)  
Old 12-06-2003
PxT's Avatar
PxT PxT is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2000
Location: Sacramento, CA
Posts: 909
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 (permalink)  
Old 12-08-2003
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006

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 (permalink)  
Old 12-10-2003
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,941
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 (permalink)  
Old 12-10-2003
Frederik Frederik is offline
Registered User
  
 

Join Date: Dec 2003
Location: Houten, NL
Posts: 2
Thanks a lot for de codes.
In all the codes where usefull parts.
my script is working.

Tanks,
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0