[질문] 날짜에 대한 연산

blhole의 이미지

2003/04/01 - 2003/03/01
과 같은 연산 혹은
2003/04/01 - 1day
와 같은 연산을 어떻게 하면 할 수 있을까요?

제가 생각한 것은 date 명령을 이용하는 것이었는데...

$ date +%s
1047367069

이런식으로 현재 시간을 구한후 해당 시간을 뺀후 (여기까지는 되는데..)
남은 시간을 다시 사람이 인식할 수 있는 형식으로 바꾸는 명령이
없네요. 그런 명령 있습니까?

무한포옹의 이미지

$ date -d '1 day ago' +%Y%m%d

하면 하루 전의 날짜가 나오죠. 미래의 경우는 ago 를 뺍니다.

year, month, day, hour, sec 다 됩니다.

자세한 건 $ info date

(일단은 gnu 의 date 외에는 몰겠습니다. 그 경우에는 unix time 으로 바꾼후 적당히 계산해도 되겠죠.)

추신: 다시 보니 그 문제가 아니군요. 죄송합니다. 문제가 애매하네요.
2003/04/01 - 2003/03/01 은 한달 또는 삽십일 ..또는 단위가 애매하군요.

2003/04/01 - 1day 는 위와 같은 표현을 참고 해서
$ date -d '2003-4-1 1 day ago' +%Y%m%d
와 같이 하면 아마 원하시는 답 같습니다

-------------------------------
== warning 대부분 틀린 얘기입니다 warning ===

Necromancer의 이미지

예전 학교 다녔을때 배웠었는데

생각이 안나네요.

libc함수 중에 님이 원하는 변환을 하는 함수 있습니다.

한번 찾아보세요. 아래 링크는 libc 매뉴얼 다운받는데입니다.

http://www.gnu.org/manual/glibc-2.2.5/libc.html

그리고 유닉스에서는 시간을 표시할 때는

1970년 1월 1일 0시 0분 0초를 기준으로 초 단위로 카운트된 시간이

들어갑니다.

만일 70년 1월 2일 0시 0분 0초라면 1월 1일과 딱 24시간 차이나므로

24*3600+0*60+0*0 = 86400 이 들어가는 거지요 거지요.

이거 아시면 님이 원하는 거 만드실 수도 있습니다. 대신 복잡할겁니다.

Written By the Black Knight of Destruction

cedar의 이미지

blhole님 글에 있는 것 처럼 libc 만으로 구현하기는 좀 까다롭습니다.

시간 여유가 없으시다면 이미 구현된 라이브러리를 갖다 쓰시는 게 좋을 겁니다.

제가 추천하는 것은 Boost C++ Library에 포함된, Boost Date-Time Library입니다.
날짜/시간 연산과 관련된 거의 모든 것이 구현되어 있습니다.

펑키의 이미지

예전에 은행일 할때 만들어 놓은 날짜 관련된 함수들이 좀 있습니다. 좀 오래된것이라서 약간 수정해서 사용해 보세요. 즐거운 하루 되세요.

int is_leap( int year )
{
    return year % 400? ( year % 100 ? ( year % 4 ? FALSE : TRUE ) : FALSE ) : TRUE;
}

int days_in_month(int months, int leap)
{
	if (months < 1 || months > 12)
		return -1;

	switch (months) {
		case  2:
			return leap ? 29 : 28;
		case  1:
		case  3:
		case  5:
		case  7:
		case  8:
				return 31;
		case 10:
		case 12:
		case  4:
		case  6:
		case  9:
		case 11:
				return 30;
		default:
				return 31;
	}
}

int roll_date(struct tm *tm_p, int years, int months, int days)
{

  int days_of_mon;

	days_of_mon = days_in_month(tm_p->tm_mon, is_leap(tm_p->tm_year));

	days   += tm_p->tm_mday;
	months += tm_p->tm_mon + (days - (days < 0 ? days_of_mon : 1)) / days_of_mon;
	if (is_leap(tm_p->tm_year))
		years += tm_p->tm_year + (months - (months < 0 ? 366 : 0)) / 366;
	years += tm_p->tm_year + (months - (months < 0 ? 365 : 0)) / 365;

	if ((days   %= days_of_mon) < 0) days += days_of_mon;
	if ((months %= 			12) < 0) days += 12;

	tm_p->tm_mday = days;
	tm_p->tm_mon  = months;
	tm_p->tm_year = years;

	return 0;
}
blhole의 이미지

무한포옹님께서 해 주신 답변이 저의 질문에 대한 정확한 답변(제가 원하는 답변)이었습니다.
특히 감사드리고요. 다른 분들의 답변도 너무 감사합니다.
man date
로만 일을 하려다 보니 질문을 하게 되었네요.
다음부터는 꼭 info도 확인해보고 질문 올리겠습니다.

무한포옹 wrote:
$ date -d '1 day ago' +%Y%m%d

하면 하루 전의 날짜가 나오죠. 미래의 경우는 ago 를 뺍니다.

year, month, day, hour, sec 다 됩니다.

자세한 건 $ info date

(일단은 gnu 의 date 외에는 몰겠습니다. 그 경우에는 unix time 으로 바꾼후 적당히 계산해도 되겠죠.)

추신: 다시 보니 그 문제가 아니군요. 죄송합니다. 문제가 애매하네요.
2003/04/01 - 2003/03/01 은 한달 또는 삽십일 ..또는 단위가 애매하군요.

2003/04/01 - 1day 는 위와 같은 표현을 참고 해서
$ date -d '2003-4-1 1 day ago' +%Y%m%d
와 같이 하면 아마 원하시는 답 같습니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.