Sponsored Content
Top Forums Shell Programming and Scripting Display previous days dates in ksh Post 302880312 by Chubler_XL on Wednesday 18th of December 2013 07:38:11 PM
Old 12-18-2013
Try this perl prog:

Code:
#!/usr/bin/env perl
use Time::Local;
use POSIX qw(strftime);

while (my $ln = <STDIN>) {
    my ($d, $smt, $sdy, $sy, $sh, $sm, $ss, $emt, $edy, $ey, $eh, $em, $es) = split(/[ \/:]/, $ln);
        my $t = abs(timelocal($es,$em,$eh,$edy,$emt-1,$ey) -
                timelocal($ss,$sm,$sh,$sdy,$smt-1,$sy));

    printf "%02d:%02d:%02d\n", $t / 3600, ($t / 60) % 60, $t % 60;
}

---------- Post updated at 10:38 AM ---------- Previous update was at 10:14 AM ----------

Call it like this

Code:
$ ./datediff.pl < infile
00:02:59
30:56:38


Last edited by Chubler_XL; 12-18-2013 at 10:21 PM.. Reason: Use abs() to avoid negative difference
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

days elapsed between 2 dates

does anybody know how to find out the number of days elapsed between 2 dates e.g. days elapsed between 020212 and 020110 (YYMMDD format) Thanking you in advance. Ravi. (1 Reply)
Discussion started by: rkkiran
1 Replies

2. Shell Programming and Scripting

Difference between two dates in no of days ???

Hi All How to get the difference between two dates in no of days ??? My date format is like this YYYY/MM/DD. I have to get the no of days between two dates in the given format. I tried to search the forum but nothing came up similar to my requitement. Your help will be appreciated. ... (1 Reply)
Discussion started by: csaha
1 Replies

3. UNIX for Dummies Questions & Answers

Dates of previous years

Is there any way to use date with previous dates such as "2 23 2000" in order to see what day of the week it was? I tried changing the current date to "date 022300452000" but then it told me that I could not do this because I was "Not the owner". Any other ways of getting the day result? (2 Replies)
Discussion started by: terms5
2 Replies

4. Shell Programming and Scripting

Days difference between two dates

Hello, I would like to find out the number of days between two dates of the format yyyy-mm-dd. Any help on this is highly appreciated. Thanks. (6 Replies)
Discussion started by: Data469
6 Replies

5. UNIX for Dummies Questions & Answers

using 'date' to get previous days' dates

I am familiar with using the 'date' command to get the current date but I have a situation where I need to get the previous day's date as well as the date two days prior. Theoretically I could use 'expr' to compute these values but I need it to work in instances where the previous month's dates... (2 Replies)
Discussion started by: slant-40
2 Replies

6. UNIX for Dummies Questions & Answers

display all dates 200 days back

i need help! can someone help me please? i try to calculate date under unix (ksh)...AIX operating system. I have to find the date 200 days from today's date. then the script should loop 200 times and display on command line every day's date until the current date. example: todays date:... (1 Reply)
Discussion started by: pavan_test
1 Replies

7. Shell Programming and Scripting

display all dates 200 days back

i need help! can someone help me please? i try to calculate date under unix (ksh)...AIX operating system. I have to find the date 200 days from today's date. then the script should loop 200 times and display on command line every day's date until the current date. example: todays date:... (4 Replies)
Discussion started by: pavan_test
4 Replies

8. Shell Programming and Scripting

days are between the two dates?

I have two times in the format of YYMMDD. Does anyone know an easy way in ksh for me to display how many days are between the two dates? Example1: X=101202 Y=101205 There are 3 days between X & Y Example2: X=101202 Y=111202 There are 365 days between X & Y Example3: X=101205... (3 Replies)
Discussion started by: oldman2
3 Replies

9. Shell Programming and Scripting

Difference in dates in days (ksh shell)

Hi Guys, Need a small help, how do i get the difference between two dates (in days) in KSH shell My date is in mm/dd/YYYY format, Is there a function to get time stamp from the respective date and time ( mm/dd/yyyy HH:MM:SS) (1 Reply)
Discussion started by: selvankj
1 Replies

10. Shell Programming and Scripting

Get number of days between 2 dates

Gents. Please can u help. I would like to calculate the days between two dates. Example file1 ( previous date) file1 - Input file 9/29/2010 10195 9/29/2010 1057 2/2/2016 10 2/2/2016 10169 2/2/2016 1057 2/3/2016 10005 2/3/2016 10014 In file2 I add the actual date using this code.... (9 Replies)
Discussion started by: jiam912
9 Replies
TIMEGM(3)						     Linux Programmer's Manual							 TIMEGM(3)

NAME
timegm, timelocal - inverses of gmtime and localtime SYNOPSIS
#include <time.h> time_t timelocal(struct tm *tm); time_t timegm(struct tm *tm); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): timelocal(), timegm(): Since glibc 2.19: _DEFAULT_SOURCE Glibc 2.19 and earlier: _BSD_SOURCE || _SVID_SOURCE DESCRIPTION
The functions timelocal() and timegm() are the inverses of localtime(3) and gmtime(3). Both functions take a broken-down time and convert it to calendar time (seconds since the Epoch, 1970-01-01 00:00:00 +0000, UTC). The difference between the two functions is that timelo- cal() takes the local timezone into account when doing the conversion, while timegm() takes the input value to be Coordinated Universal Time (UTC). RETURN VALUE
On success, these functions return the calendar time (seconds since the Epoch), expressed as a value of type time_t. On error, they return the value (time_t) -1 and set errno to indicate the cause of the error. ERRORS
EOVERFLOW The result cannot be represented. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +----------------------+---------------+--------------------+ |Interface | Attribute | Value | +----------------------+---------------+--------------------+ |timelocal(), timegm() | Thread safety | MT-Safe env locale | +----------------------+---------------+--------------------+ CONFORMING TO
These functions are nonstandard GNU extensions that are also present on the BSDs. Avoid their use. NOTES
The timelocal() function is equivalent to the POSIX standard function mktime(3). There is no reason to ever use it. SEE ALSO
gmtime(3), localtime(3), mktime(3), tzset(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2016-12-12 TIMEGM(3)
All times are GMT -4. The time now is 01:01 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy