Calculate the calendar date since Jan 1, 2000


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the calendar date since Jan 1, 2000
# 1  
Old 03-26-2012
Calculate the calendar date since Jan 1, 2000

Does anyone know how to calculate a calendar date since Jan 1, 2000 (this is day 1). I am using CSH with a Solaris system (no GNU products installed).

Example:
Input from the user (number of days):
4444

Output (dd mmm yy)
02 mar 12

---------- Post updated at 09:40 PM ---------- Previous update was at 09:35 PM ----------

actually I need to the output to have the whole year so 2012 instead of 12.
# 2  
Old 03-26-2012
You are better off using Perl instead of csh. Perl is available in Solaris
Code:
$ cat a.pl
#! /usr/bin/perl

@M=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
($x,$x,$x,$mday,$mon,$yr)=localtime(946656000+86400*$ARGV[0]);
printf("%02d %s %4d",$mday,$M[$mon],$yr+1900);


$ ./a.pl 4444
02 Mar 2012

# 3  
Old 03-27-2012
Thanks so much...I'll give it a try tomorrow. I am not familar with perl...could you explain the code.
# 4  
Old 03-27-2012
If you are on Solaris 10, the ksh93 (Korn) shell is available to you.

In this shell, date arithmetic is fully supported
Code:
$ printf "%(%D)T\n" "Jan 1 2000 + 4444 days"
03/02/12
$

This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 03-27-2012
Thanks for the info I didn't realize. How do you call this Korn shell? Is it #! /usr/bin/korn
Also, how do you get the date in dd mmm yyyy format? For example 26 Mar 2012?
# 6  
Old 03-27-2012
This list provides index to the abbr month name. The 4th column returns from the 'localtime' will give the index of the month. Eg. 2 means Mar. List index starts from 0
Code:
@M=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

localtime is a Perl built-in function that returns second, minute, hour, ..... In our case, we only need the day of the month ($mday), month ($mon), and year ($yr). 946656000 is the epoch time for 2000-01-01, we just need to add the additional seconds for the input days
Code:
($x,$x,$x,$mday,$mon,$yr)=localtime(946656000+86400*$ARGV[0]);

$yr returns 100 for year 2000, therefore we need to add 1900. Also, we need the month in abbr form and therefore we need to refer to the value based on the position in the list @M
Code:
printf("%02d %s %4d",$mday,$M[$mon],$yr+1900);

This User Gave Thanks to chihung For This Post:
# 7  
Old 03-27-2012
Quote:
Originally Posted by thibodc
Thanks for the info I didn't realize. How do you call this Korn shell? Is it #! /usr/bin/korn
Also, how do you get the date in dd mmm yyyy format? For example 26 Mar 2012?
My preference is #!/usr/bin/env ksh which will invoke it provided the binary is in a directory specified in your PATH. I use this method because I believe it is more portable and on most of my systems I build the latest version of Kshell and install it outside of the system default (/usr/bin/ksh or /usr/local/bin/ksh); I want to ensure that I'm invoking my build rather than the version that was included in the distribution.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. UNIX for Dummies Questions & Answers

Date format from Jan 01 2015 11:00:00 PM to 01/01/2015 23.00.00

I need to change Date and time stamp format from Jan 01 2015 11:00:00 PM to 01/01/2015 23.00.00 Existing Format : Mon DD YYYY hh:mi:ss AM/PM (Jan 01 2015 11:00:00 PM) Expected Format: MM/DD/YYYY hh.mi.ss 24 hours (01/01/2015 23.00.00) I need to update enitire file where... (3 Replies)
Discussion started by: esivaprasad
3 Replies

3. Shell Programming and Scripting

perl one-liner to get yesterday's date in format dd-MMM-yy (i.e. 01-JAN-12)

I have the following perl one-liner to get yesterday's date, but I would like it in the form of dd-MMM-yy (for example: 01-JAN-12). Can someone alter the below code so I get the format I want? Also, could someone also give me a line for dd-Mmm-yy (for example 01-Jan-12)? Code: YEST=`perl -w... (3 Replies)
Discussion started by: thibodc
3 Replies

4. Shell Programming and Scripting

How do i compare two dates with format Jan 01, 2012 and Jan 00 2012

I need to be able to compare dates in the format of Jan 10, 2012 and Jan 10 2012. (Notice one has a comma). Then I need to find the date that is 7 days before those dates if they are equal. How can I do this in Bash. Thank ahead (4 Replies)
Discussion started by: ojthejuice
4 Replies

5. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for... (5 Replies)
Discussion started by: thibodc
5 Replies

6. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

7. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

8. Shell Programming and Scripting

Derive date from a calendar

I'm using AutoSys as scheduler for my application. I maintain a calendar in AutoSys which specifies when a job should run. A unix shell scripts runs on the days spceified by calendar and processes incoming files. These incoming files contain dates embedded in the filenames. My job needs to... (2 Replies)
Discussion started by: autouser123
2 Replies

9. Shell Programming and Scripting

Calendar date to Julian and Back

I need Unix, ksh scripts that will convert dates - Gregorian to Julian and Julian to Gregorian. Input for converting Gregorian to Julian would be in the form of CCYYMMDD with the output being CCYYDDD. Input for converting Julian to Gregorian would be CCYYDDD with the output being CCYYMMDD. ... (4 Replies)
Discussion started by: BCarlson
4 Replies
Login or Register to Ask a Question