Convert Unix Timestame to real timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Unix Timestame to real timestamp
# 1  
Old 06-26-2009
Convert Unix Timestame to real timestamp

Hello,

Did anyone know how to use script (e.g. perl) to conver Unix Timestame to real timestame in GMT+8 ?
Code:
1245900787 file:/tmp/a/Test/.txt.swp has created
1245900988 file:/tmp/a/Test/.txt.swp has changed

Code:
Thu, 25 Jun 2009 11:33:07 GMT+8 file:/tmp/a/Test/.txt.swp has created
Thu, 25 Jun 2009 03:36:28 GMT+8 file:/tmp/a/Test/.txt.swp has changed

Thank You

HappyDay

---------- Post updated at 03:16 PM ---------- Previous update was at 02:42 PM ----------

#!/usr/bin/perl

$num_args = $#ARGV + 1;
die "Usage: this-program epochtime (something like '1219822177')" if
($num_args != 1);

$epoch_time = $ARGV[0];

($sec,$min,$hour,$day,$month,$year) = localtime($epoch_time);

# correct the date and month for humans
$year = 1900 + $year;
$month++;

printf "%02d/%02d/%02d %02d:%02d:%02d\n", $year, $month, $day, $hour, $min, $sec;


I found the about script, it can help me to convert 1245900789 to 2009/06/25 11:33:09
, but can't help me to covert a file. Did any one know how to change the above perl script to handle the file

Thank You

HappyDay

Last edited by Yogesh Sawant; 06-28-2009 at 04:51 AM.. Reason: added code tags
# 2  
Old 06-26-2009
if shell scripts are an option for you:

Code:
>date -u -d @1246018466
Fri Jun 26 12:14:26 UTC 2009

just working with linux date

---------- Post updated at 14:53 ---------- Previous update was at 14:29 ----------

if shell scripts are an option for you:

Code:
>date -u -d @1246018466
Fri Jun 26 12:14:26 UTC 2009

just working with linux date
# 3  
Old 06-26-2009
If you have gawk you can do something like:

Code:
gawk '{$1=strftime("%c" , $1)}1' file > outfile

Regards
# 4  
Old 06-26-2009
By UNIX timestamp do you mean filetimes - the epoch seconds kept in a the filesystem by the kernel?
Code:
perl -e '
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($ARGV[0]);

$mtime is filetime in epoch seconds

Last edited by jim mcnamara; 06-26-2009 at 10:16 AM..
# 5  
Old 06-27-2009
I this what the OP is looking for is a way to print out file timestamps as GMT+8 hrs.

Assuming I am at GMT-4 hrs (East Coast USA), here are two ways which are locale independant.

Code:
#!/bin/ksh93

(( $# != 1 )) && {
   echo "usage: $0 epochtime"
   exit 1
}

printf "%(%a, %e %b %Y %T)T GMT+8\n" "#${1} + 12 hours"

exit 0

Example output
Code:
$ ./testksh93 1245900787
Thu, 25 Jun 2009 11:33:07 GMT+8

Or if you want to use Perl
Code:
#!/bin/perl

use POSIX qw(strftime);

$num_args = $#ARGV + 1;
die "usage: $0 epochtime" if ($num_args != 1);

$offset8 = 8 * 60 * 60;
$epoch_time = $ARGV[0];
$now_string = strftime "%a, %e %b %Y %T GMT+8", gmtime($epoch_time + $offset8);

printf "%s\n", $now_string;

Example output:
Code:
$ ./testperl 1245900787
Thu, 25 Jun 2009 11:33:07 GMT+8

~
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert date in dd mm yyyy format to UNIX timestamp

Hello All, I have a date in DD/MM/YYYY format. I am trying to convert this into unix timestamp. I have tried following: date -d $mydate +%s where mydate = 23/12/2016 00:00:00 I am getting following error: date: extra operand `+%s' Try `date --help' for more information. ... (1 Reply)
Discussion started by: angshuman
1 Replies

2. UNIX for Dummies Questions & Answers

Convert Epoch to timestamp in a file

Hi Team, Could you please let me know ,how to convert Epoch column to timestamp in a flat file. "57894"|"1454247163111"|"""HH""" "57897"|"1454247163111"|"""HH""" "7906"|"1454247163111"|"""ss""" I want second field as timestamp. This thread has been moved from a non-technical forum to... (6 Replies)
Discussion started by: shabeena
6 Replies

3. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

4. Shell Programming and Scripting

Convert UNIX timestamp to readable format in the file

Hello I have a file : file1.txt with the below contents : 237176 test1 test2 1442149024 237138 test3 test4 1442121300 237171 test5 test7 1442112823 237145 test9 test10 1442109600 In the above file fourth field represents the timestamp in Unix format. I found a command which converts... (6 Replies)
Discussion started by: rahul2662
6 Replies

5. UNIX for Dummies Questions & Answers

How to convert R$Timestamp in Sql*Plus within a UNIX Shell Script?

I need to compare a R$Timestamp field sql within a Unix Shell Script. In straight SQL the following code works fine: Table Name: LL_UNIT_TRANSACTION UT Field: R$Timestamp Where TRUNC(UT.R$Timestamp) >= TRUNC(SYSDATE -7) the following returns no data within the Unix Shell Script... (2 Replies)
Discussion started by: Dapconsult
2 Replies

6. AIX

convert a specific date to a unix timestamp

hello, i have an AIX5.3 machine and i am writing a script to display some processes. inside the script i want to get the time that the process starts and convert it to a unix timestamp. is there a command that i can use to do that? i search the web but all i found is long scripts and it does... (4 Replies)
Discussion started by: omonoiatis9
4 Replies

7. Shell Programming and Scripting

How to convert date and timestamp?

Hi, I have a file file1 having data as below 20110501,070742, ,012345678909,09999999999,68.5, 20110501,070236, ,089375855455,09376383333,374.3, 20110501,070525, ,090345895555,08444233444,206.2, 20110501,230051, ,000934744433,07624262223,480.1, First field is date(YYYYMMDD) and second... (5 Replies)
Discussion started by: vsachan
5 Replies

8. AIX

Convert unix timestamp to year month day format ?

Hello, How do I convert unix timestamp value to 'normal' date format - to get year month and day values ? Looks like it's easy to do using GNU date (linux systems). But how do I do tthis on AIX ? I don't want to write C program, any ways to do that using unix shells ? thanks (1 Reply)
Discussion started by: vilius
1 Replies

9. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

10. Shell Programming and Scripting

how to convert from timestamp to date format in tcsh

hello all im looking for fast way to convert timestamp format to date format and vaiseversa in tcsh , can it be done? thanks allot (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question