LinuxQuestions.org
Visit Jeremy's Blog.
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 08-25-2009, 02:11 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Time Issue.


Hi, bro, I have a problem here.

How to get any single day's the previous week's calendar range?

For example, suppose today is 2009-8-13, so the previous weekdays' range is 2009-8-3 to 2009-8-7.

Thanks you in advance.
 
Old 08-25-2009, 02:26 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What about this?
Code:
{ date -d 'last monday -1 week'; date -d 'last friday'; }
Kevin Barry

edit:
This might be better:
Code:
{ if [ `date +%u` -le 1 ]; then
    date -d 'last monday'
  else
    date -d 'last monday -1 week'
  fi;
  if [ `date +%u` -gt 5 ]; then
    date -d 'last friday -1 week'
  else
    date -d 'last friday'
  fi; }

Last edited by ta0kira; 08-25-2009 at 02:36 PM.
 
Old 08-25-2009, 02:31 PM   #3
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
Uh...very cool..but how to write it in C codes?


Quote:
Originally Posted by ta0kira View Post
What about this?
Code:
{ date -d 'last monday -1 week'; date -d 'last friday'; }
Kevin Barry
 
Old 08-25-2009, 03:34 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
Quote:
Originally Posted by ArthurHuang View Post
Uh...very cool..but how to write it in C codes?
Yes, it is cool. However, you have not even put in the effort to tell us in what language you want to implement that problem. So the time spent by ta0kira solving your problem was completely lost because you didn't want to spend the time to elaborate on your question. That doesn't sound fair, does it?

For that matter, while you could have forgotten to specify your programming language while concentrating on your question, you also withold is information about wat distro you use and what hardware.

Well, let me assume that you use GNU Linux on a PC and not Berkely Unix on a Sparc, then the way to implement this is to use the time() function, and pass that result (the number in seconds since 1970-01-01) to localtime() function. It returns a structure of type struct tm which contains fields indicating the number of the weekday.

Once you know the day number, you can calculate how many days you have to go back to the last Monday, multiply by 86400, substract this from your earlier obtained time() value, feed this into localtime() again. The time in seconds to the next Friday is calculated likewise.

A general advice, as soon as you start calculation on calendar data, go back to unix timestamp, do your calculations and convert back to human readable date. Only the date function is an exception so you can escape to that sometimes, as shown. But even then it is often better to go back to unix timestamps.

Info on these functions:
http://www.cs.utah.edu/dept/old/texi...19.html#SEC317

jlinkels
 
  


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
Time Issue ddzc Linux - Software 2 03-29-2007 01:43 PM
time issue... koyi Linux - Newbie 4 04-02-2005 06:39 AM
time issue chrisknight Linux - Software 2 06-02-2004 06:44 PM
Time issue thetwin Linux - Newbie 5 01-29-2004 03:38 PM
Samba time issue olegarr Linux - Networking 1 09-15-2003 04:45 PM

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

All times are GMT -5. The time now is 03:06 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