Get Formatted Date from Unix Epoch


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Formatted Date from Unix Epoch
# 1  
Get Formatted Date from Unix Epoch

Could someone please explain how to get a formatted date from the unix epoch (the number of seconds since Jan 1, 1970)

For example.,
If the input is 1297969816, then the output should be 2011-02-17 in YYYY-MM-DD formatted manner.

I am using AIX sh shell. Tried date -d and date --date, these didnt work . Cant use perl, python etc, only sh,ksh shells.

Thanks in anticipation.
# 2  
datecalc is a ksh script that I posted to the site. Using datecalc most date arithmetic problems become easy.
Code:
$ cat unixdate
#! /usr/bin/ksh
epoch=1297969816
((uday=epoch/86400))   # 86400 seconds in a day
((uday=uday+40587))    # 40587 is the modified julian day number of Jan 1, 1970
./datecalc -j $uday
$
$ ./unixdate
2011 2 17
$

This User Gave Thanks to Perderabo For This Post:
# 3  
C program

Here is a C program I got back in 2004. You should be able to compile it on any UNIX system with the standard C compiler. You run it and provide your number of seconds since EPOCH value and it will print out the date and time. You could modify the print statement or use some other shell mechanism to change the output to what you are looking for.
Enjoy,
-Dave
Code:
/*******************************************************************************
 * File:        convtime.c
 *
 * Description: Convert OpCdatetime (seconds since EPOCH) to calendar datetime.
 *              Calendar datetime is written to stdout.
 *
 * Use:        convtime <OpCdatetime>
 *
 * Revision History
 * ================
 *
 * Name        Date    Description
 * J. Peereboom 960722    Creation
 * 
 ******************************************************************************/

/*
 * System includes
 */
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>

/*******************************************************************************
 * Function:    main()
 * Description: Main entry point.
 * Called by:   None
 * In:          argc: Argument count.
 *              argv: The arguments. argv[1] is the datetime to be converted.
 * Out:         None
 * InOut:       None
 * Return:      0.
 *              Prints the calendar datetime on stdout.
 ******************************************************************************/

extern int main(argc, argv)
 int   argc;
 char *argv[];
{
    struct tm *t;
    time_t seconds;

    if (argc != 2)
    {
    fprintf(stderr,"usage: doit time\n");
    exit(1);
    }

    sscanf (argv[1], "%ld", &seconds);
    t = localtime(&seconds);
    printf("%4d%02d%02d %02d:%02d:%02d\n"
            , t->tm_year+1900
            , t->tm_mon+1
            , t->tm_mday
                        , t->tm_hour
            , t->tm_min
            , t->tm_sec
            );
    return 0;
}

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-17-2011 at 01:56 PM.. Reason: code tags, please!
This User Gave Thanks to DaveJohnson For This Post:
# 4  
On modern versions of AIX the /usr/bin/ksh93 shell is also available. If this is the case, then the following will work:
Code:
$ printf "%(%Y-%m-%d)T\n" #1297969816
2011-02-17

This User Gave Thanks to fpmurphy For This Post:
# 5  
Guys, Thank you All for your replies.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #827
Difficulty: Medium
Douglas Crockwell first specified and popularized the JSON format.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

2. Shell Programming and Scripting

Epoch time to produce exact date everytime

so i have to perform a certain task at set times. for instance, i need to run a job at 12:30am every night, and other jobs, i only need to have them run on saturdays. how do i manipulate the date command to give me the epoch equivalence of what 12:30am would be every day? im looking for a... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Convert epoch time to Julian date

Need assistance in converting an epoch time to Julian date To get epoch perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. Shell Programming and Scripting

Turning given date to epoch

i can probably script this in bash, but, i was wondering, does anyone know of a simple way to translate a given time to epoch? date -d@"29/Oct/2013:17:53:11" the user would specify the date: 29/Oct/2013:17:53:11 and the script will simply interpret that to epoch: 1348838383 (this is just... (4 Replies)
Discussion started by: SkySmart
4 Replies

5. OS X (Apple)

Date to epoch problem

Hi all, In terminal when I enter: date -j -f date -j -f "%Y/%m/%d %T" "2011/09/30 13:00:00" +"%s" The output is: When I put 2011/09/30 in var A, and I subsequently enter: date -j -f date -j -f "%Y/%m/%d %T" "${A} 13:00:00" +"%s" The output is: (10 Replies)
Discussion started by: mosthated
10 Replies

6. Programming

Converting a user inputted date to epoch time

Hi all , I need to know how to convert a time stamp entered by the user to be converted to GMT/UTC(epoch time) using mktime() and gmtime() for exapample the input will be put in the form ptm.tm_sec = 0; ptm.tm_min = 59; ptm.tm_hour = 11; ptm.tm_mday = 20;... (2 Replies)
Discussion started by: ada
2 Replies

7. Shell Programming and Scripting

unixtime to formatted date time

Hi, I need to take the unix time and format it to a date/time string like this yyyymmdd,hhmmss I'm wrting in shell but have tried calling perl, but all the perl options I found on here puts output to Thu Jan 1 00:00:00 1970 format. Any help? Cheers Neil (4 Replies)
Discussion started by: nhatch
4 Replies

8. Shell Programming and Scripting

Need to convert an epoch date to MMDDYYHHmm format

System: HP-UX Kornshell Perl is installed, but not POSIX Hello, I am calculating a future date/time. To do this I take the system date in epoch format and add to it. I now need to take the new epoch date and convert it to MMDDYYHHmm format. Any help with this is greatly appreciated. (4 Replies)
Discussion started by: LetsGoPens
4 Replies

9. Shell Programming and Scripting

Date to epoch problem

Hi all! I have a "simple" problem: I want to convert a date and time string (YYYYMMDDhhmmss) to epoch (unix time) in a shellscript. I want to use the "date/time" string as an input to the script, eg: scriptname.sh 20090918231000 and get the epoch format echoed out. Is there an... (3 Replies)
Discussion started by: condmaster
3 Replies

10. Shell Programming and Scripting

Convert date into epoch in Perl on Solaris

Solaris 10 doesn't seem to like me a lot. I am trying to run a simple script to accept date and return epoch of that date: #!/usr/bin/perl -w use strict; use Time::ParseDate; my($date1)="Mon Mar 27 05:54:08 CDT 2009"; chomp $date1; #Convert to seconds since start of epoch my $time1 =... (3 Replies)
Discussion started by: pavanlimo
3 Replies

Featured Tech Videos