Solved

convert unix seconds to date in other format

Posted on 1999-01-06
13
414 Views
Last Modified: 2010-04-21
Is there a straight forward use of `date` to change
"seconds since the epoch" to some other date format?
I've worked on this on the command line but the
following lines best illustrate the problem.

@fir$ date +%s >time_tmp
@fir$ date  >>time_tmp
@fir$ cat time_tmp
915665157
Wed Jan  6 15:26:05 PST 1999
@fir$ date  +%D%t%r -f time_tmp
date: invalid date ` 915665157'
01/06/99        03:26:05 PM


0
Comment
Question by:2ndgrade
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2008652
perl -e 'print scalar localtime shift' 915665157
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008653
which OS? (-f option  is used to parse another format string, not a file, usually)

How about using
   date -u
or
  env TZ=PST date    # or the timezone you prefer

0
 
LVL 22

Expert Comment

by:blu
ID: 2008654
how about:

echo 0t915665157=Y | adb
      1999 Jan  6 18:25:57
0
 

Author Comment

by:2ndgrade
ID: 2008655
I'm running linux and bash.
@fir$ perl --version
This is perl, version 5.005_02 built for i586-linux
@fir$ perl -e 'print scalar localtime shift' 915741046
yields this:
@fir$ n  7 12:30:46 1999@fir$
 
@fir$ echo 0t915665157=Y | adb
yields:
bash: adb: command not found
What is the command `adb` on what OS and shell? Does "0t" designate that the digits following are to be interpreted as a date in unix seconds?

@fir$ date --version
date (GNU sh-utils) 1.16
excerpt from date --help  is

  -f, --file=DATEFILE      like --date once for each line of DATEFILE

I thought accessing the date string from a file made the problem clearer than all the quote produced obfuscation on the command line when using date --date=`some command`.
I have seen logging done which uses unix seconds as the timestamp and expected that the date command would be capable of transforming a timestamp in whatever format to whatever other format. This seems to be true with the exception of going from unix seconds to any other format.

0
 
LVL 84

Expert Comment

by:ozo
ID: 2008656
You seem to have printed over the "Thu Ja".
Try:
perl -e 'print "\n".(localtime shift)."\n"' 915741046
0
 

Expert Comment

by:tato
ID: 2008657
if u need a string like timestamp use
date +"%Y%m%d%H%M%S"
19990111211355
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:2ndgrade
ID: 2008658
There are any number of different date formats besides %Y%m%d%H%M%S  which could be called string-like. They aren't what's needed.
 What I want is date to do unix seconds->some other format.
I'm willing to be corrected, but I see nothing in the src for `date` which provides for doing this.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2008659
> some other format
which format?
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2008660
Let's see, the requirement is that 'date' take seconds since the epoch as an argument and that you can use the already-there 'date' formatting directives to output a new date.  We also know that the source for the date command is available.

I'm fairly certain that no widely-available utility currently does this (though you'd think that someone would have modified date to do this long ago).

I'd say the easiest thing would be to modify the source to the date command.  Underneeth it all, date gets the current time from the time() system call.  Modify the code to add a new command-line option (say '-s seconds-since-epoch') and use the supplied argument instead of the result from the call to time().

You can do this even if you don't have permissions to install your version of date as the official/system version, since you can always modify your PATH to point at your version first.
0
 

Author Comment

by:2ndgrade
ID: 2008661
Thanks for the comments.  Three weeks ago I wrote a wrapper for ctime() to do this job, deciding that it was more trouble than it was worth to maintain a local non-standard version of 'date'.
This question might as well get squashed.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2008662
Thought of the ctime wrapper too, but decided that was more trouble than modifying date.   Oh well, either way.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2008663
I suggest 2ndgrade submit his own answer and get the points :-)
0
 
LVL 14

Accepted Solution

by:
chris_calabrese earned 100 total points
ID: 2008664
OK, since he hasn't done it yet, I'll do it.  The most appropriate answer for 2ngrade's problem is to write a ctime() wrapper :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem Imaging Computers With Clonezilla 2 83
Skill Development 15 163
UNIX SCP 5 70
Wrap Oraccle SQL*Plus executable Command 4 70
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now