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 02-10-2011, 05:41 PM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
How to print out "n" days ago's year, month and day?


Interesting uh?
For example, 2 days ago's date is 2011 Feb 8th.
This is not a homework...

thanks!
 
Old 02-10-2011, 06:51 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
date -d '2 days ago'
Kevin Barry
 
2 members found this post helpful.
Old 02-10-2011, 07:22 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
I was just going to write what ta0kira wrote. The GNU date command is awesome. Anyway, in C, you can do
Code:
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t     now, then;
    struct tm *split;

    /* Get the current time */
    now = time(NULL);

    /* Convert the time to broken-down format */
    split = localtime(&now);

    /* Substract two days -- it is okay to go out of valid range */
    split->tm_mday -= 2;

    /* Renormalize the broken-down format, and return the time then. */
    then = mktime(split);
    
    printf("Two days ago was %04d-%02d-%02d, or %s\n",
           split->tm_year + 1900, split->tm_mon + 1, split->tm_mday,
           ctime(&then));

    return 0;
}
This uses the local time as a basis. If you need UTC, use gmtime instead of locatime.

The call to mktime both normalizes the split date, and returns the resulting time in the base time_t format, seconds since epoch. See the ctime, localtime, and mktime man page for details. This is in the POSIX standard, and should work as-is on all sane operating systems.

It shouldn't be too difficult to add parameter parsing to the above, to get exactly what you need.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 08:33 AM.
 
1 members found this post helpful.
Old 02-10-2011, 08:33 PM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -r'date' -e 'print (DateTime.now - 48/24).strftime("%Y/%m/%d")'
 
  


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
[SOLVED] Perl: Condition?print "Is true":print "Is false"; angel115 Programming 2 09-17-2010 06:28 AM
how to get the first and the last day in the month with "date" command myhnet Linux - Newbie 9 06-16-2009 03:16 AM
LQ-er of the day/week/month/year? alan_ri LQ Suggestions & Feedback 0 01-24-2009 05:11 PM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 02:50 AM

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

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