Google Groups Home
Help | Sign in
Message from discussion yesterday's date under the shell
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tapani Tarvainen  
View profile  
 More options Feb 12 2002, 10:45 am
Newsgroups: comp.unix.shell
From: Tapani Tarvainen <t...@it.jyu.fi>
Date: 12 Feb 2002 17:33:11 +0200
Local: Tues, Feb 12 2002 10:33 am
Subject: Re: yesterday's date under the shell

"Jean-Noêl" <J...@freckles.de> writes:
> To determine the yesterday date i do it so:
> TZ=PST+24 date +%d
> it work well but my question is:
> does this work on all systems and all shells
> or should i do it otherwise ???

No, it does not work on all systems at all times.
In some it will work practically always,
on others never, on most it works sometimes
and sometimes not. I would recommend against it.

Unfortunately there is no short and sweet portable
solution. If you have or can install Gnu date it
will do it cleanly, otherwise you can find a number
of solutions posted in this group in the past.
For a general solution you could try the following,
which should work with POSIXy shells (I've only tested
it with HP's which is essentially ksh88, though):

#! /usr/bin/sh

# Date calculations using POSIX shell
# Tapani Tarvainen July 1998, February 2001 (POSIXified)
# This code is in the public domain.

# Julian Day Number from calendar date
date2julian()   #  day month year
{
  day=$1;  month=$2;  year=$3
  tmpmonth=$((12 * year + month - 3))
  tmpyear=$((tmpmonth / 12))
  print $(( (734 * tmpmonth + 15) / 24 -  2 * tmpyear + \
    tmpyear/4 - tmpyear/100 + tmpyear/400 + day + 1721119 ))

}

# Calendar date from Julian Day Number
julian2date()   # julianday
{
  tmpday=$(($1 - 1721119))            
  centuries=$(( (4 * tmpday - 1) / 146097))  
  tmpday=$((tmpday + centuries - centuries/4))      
  year=$(( (4 * tmpday - 1) / 1461))          
  tmpday=$((tmpday - (1461 * year) / 4))            
  month=$(( (10 * tmpday - 5) / 306))        
  day=$((tmpday - (306 * month + 5) / 10))  
  month=$((month + 2))                              
  year=$((year + month/12))                        
  month=$((month % 12 + 1))
  print $day $month $year

}

# Day of week, Monday=1...Sunday=7
dow()   # day month year
{
  print $(( $(date2julian $1 $2 $3) % 7 + 1))

}

################################# The End #################################

Those allow rather arbitrary date computations.
For example, yesterday's date can be computed like this:

julian2date $(( $(date2julian $(date +"%d %m %Y") ) - 1 ))

--
Tapani Tarvainen


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google