LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2011, 06:02 PM   #1
PopEDX
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Rep: Reputation: 0
Looping #date -d 'last Thursday'


I need to loop a perl script backwards in time through the dates of all Thursdays for the past few years.

I can easily get the date of last Thursday with
`date -d 'last Thursday'`

How can I get the date of the Thursday before that and so on?

I can't change the date of the System.
 
Old 04-19-2011, 06:42 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
For calculations like this, change to Unix timestamp, or seconds.

First get the TS of last Thursday in seconds:
last_th=$(date -d "last Thursday" "+%s")
When you have that, substract 7 * 86400 seconds from it
before_last_th=$last_th-7*86400
Get the date of that day:
date -d "1970-01-01 +$before_last_th seconds" "+%F %Ts"

BTW, date is a shell command, does it exist in Perl as well? The substraction is pseudo code, adjust to your language.

jlinkels
 
Old 04-19-2011, 06:59 PM   #3
PopEDX
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
I'm not sure if this is standard, but in my perl environment I can run shell commands like:
@array_of_directory_contents = `ls .`;

With the ` ticks.

Thank you for the info, I had the idea of doing it all in epoch time, but it seemed like a hassle.
 
Old 04-19-2011, 08:10 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Perl does have its date/time functions, don't know if you can get something fancy like "last thursday" without complicated loops. Using the shell command seems a good idea.

Whatever, for calculations like this, using epoch time is definitely not a hassle. I wrote a complicated project in PHP which involved nothing but schedules and time calculations. I started out using datetime formats, but later I used used exclusively epoch time in calculations. Only at the presentation end I did a conversion to or from datetime.

jlinkels
 
Old 04-26-2011, 10:24 AM   #5
whk
Member
 
Registered: Jun 2005
Posts: 202

Rep: Reputation: 37
In bash you can do this

This should give you a head start.
If you want years before 1970 try this.

yr=$(( 1969+28 ))
subdate=1969
yr=$(( $subdate+28 ))
mo=0$(( 1 ))
da=0$(( 2 ))

date -d $yr$mo$da +%u' '%a' '%b' '%d' '$subdate
4 Thu Jan 02 1969

The solar cycle is usually every 28 years.
Well, you know what to do when days amd months are larger than 9.
heh
a=$( echo $( date -d $yr$mo$da +%u ))
if [ $a == 4 ]
then
echo $( date -d $yr$mo$da +%u' '%a' '%b' '%d' '$subdate )
fi

hth
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Happy Thursday Everyone! ginger7childs LinuxQuestions.org Member Intro 1 07-09-2009 11:53 AM
LXer: Thursday is Google Developer Day LXer Syndicated Linux News 0 05-31-2007 09:31 AM
Dell Linux machines to debut Thursday DragonSlayer48DX Linux - News 2 05-23-2007 04:31 PM
LXer: Ubuntu 7.04 arrives Thursday LXer Syndicated Linux News 0 04-16-2007 01:46 AM
Third Thursday Cron Job stancol Linux - General 6 02-14-2007 03:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration