1970년 1월 1일 0시 0분 0초 이래로...

neobug의 이미지

#!/bin/sh

######################################################################
# bug군은 본의 아니게 SUN사의 Solaris OE를 관리하고 있다.
# Soalris는 이 업계의 모든 UNIX는 SUN을 중심으로 돌고 있다는 그 name 답게
# 군대에서도 그 뿌리를 뻗고 있었던 것이다.
# 하루는 bug 군이 너무 심심해서 coreadm을 이용하여 core 파일이 생겨날 때
# 생성시간이 파일 이름 뒤에 append 되게 끔 설정을 해 놓았다.
# 몇일 후, core 파일이 없나 find 하던 bug군은 core 뒤에 붙은 어마어마한 숫자를
# 보게된다. 알고보니 그 숫자는 1970년 1월 1일 0시 0분 0초 부터 현재까지를 초
# 단위로 나타낸 숫자였던 것이다.
# 한 두개라면 그냥 계산기 들고 계산해 보겠지만 bug군의 서버는 name 답게 bug
# 덩어리여서 하루에 만도 core 파일이 수 십개 씩 생성 된다고 한다.
# bug군은 그간 배운 지식을 쥐어짜며 쉘 스크립트를 만들기 시작했다.
######################################################################
default_year=1970
default_month=0
default_day=0
default_hour=0
default_minute=0
default_second=0
###################
# 시간을 계산 해보자
###################
	second=`expr $1 % 60`
		temp=`expr $1 / 60 + 1`
	minute=`expr $temp % 60`
		temp=`expr $temp / 60 + 1`
	hour=`expr $temp % 24`
		temp=`expr $temp / 24 + 1`
###################
# 날짜를 계산 해보자
###################


	### 년과 일(per 365)을 계산 해보자 ###
	day=`expr $temp % 365`
	year=`expr $temp / 365`

		### 만약 day 값이 음수가 된다면? ###
	day=`expr $day - \( $year + 2 \) / 4`
		while [ $day -le 0 ]
		do
			year=`expr $year - 1`

			if [ `expr \( $year + 2 \) % 4` -eq 0 ]
			then
				day=`expr $day + 366`
			else
				day=`expr $day + 365`
			fi
		done

		### 요건 나중에 월 계산 할 때 써먹을 꺼다 ###
		if [ `expr \( $year + 2 \) % 4` -eq 0 ]
		then
			flag=0
		else
			flag=1
		fi

	### 월을 계산 해보자 ###
	month=`expr $day / 30 + 1`
	day=`expr $day % 30`


	if [ $month -lt 2 ]
	then
		day=`expr $day - 1`
	elif [ $month -ge 2 -a $month -le 7 ]
	then
		day=`expr $day - \( $month + 1 \) / 2`
		if [ flag -eq 0 ]
		then
			day=`expr $day + 1`
		else
			day=`expr $day + 2`
		fi
	elif [ $month -ge 8 ]
	then
		day=`expr $day - \( $month / 2 + 1 \)`
		if [ flag -eq 0 ]
		then
			day=`expr $day + 1`
		else
			day=`expr $day + 2`
		fi
	fi

	if [ $day -le 0 ]
	then
		month=`expr $month - 1`
		if [ $month -le 7 -a $month -ne 2 ]
		then
			if [ `expr $month % 2` -eq 0 ]
			then
				day=`expr $day + 30`
			else
				day=`expr $day + 31`
			fi
		elif [ $month -eq 2 ]
		then
			if [ flag -eq 0 ]
			then
			        day=`expr $day + 29`
			else
			        day=`expr $day + 28`
			fi
		elif [ $month -ge 8 ]
		then
			if [ `expr $month % 2` -eq 0 ]
			then
				day=`expr $day + 31`
			else
				day=`expr $day + 30`
			fi
		fi
	fi
##########################################
# 지나온 시간이 아닌 현재 시간으로 설정 해보자
##########################################
crruent_year=`expr $default_year + $year`
crruent_month=`expr $default_month + $month`
crruent_day=`expr $default_day + $day`
crruent_hour=`expr $default_hour + $hour`
crruent_minute=`expr $default_minute + $minute`
crruent_second=`expr $default_second + $second`

### 출력이다 ###
echo "$crruent_year-$crruent_month-$crruent_day($crruent_hour:$crruent_minute:$crruent_second)"

산술 연산에 최악이라는 Bourne 아저씨의 쉘로 스크립트를 작성 해봤습니다.
근데 아무래도 알고리즘이 너무 조잡한 것 같네요.
KLDP의 여러분들은 이런 경우에 어떤 알고리즘을 적용 하실 건가요?
저는 일부로 본 쉘을 이용했지만 효율적인 코드 생성을 위해 어떤 언어를 이용 하실 런지요?
일종의 Code Canival 정도가 될까요?
아래는 도움이 될 데이터 입니다.

# ls
caldate
core.homs.1002.1090454086
core.homs.1002.1090454132
core.homs.1002.1090456173
core.homs.1002.1090456226
core.homs.1002.1090456375
core.homs.1002.1090462395
core.homs.1002.1090462495
core.homs.1002.1090462584
core.homs.1002.1090462617
core.homs.1002.1090462711
core.homs.1002.1090463255
core.homs.1002.1090463589
core.homs.1002.1090463617
core.homs.1002.1090463659
core.homs.1002.1090463683
core.homs.1002.1090463751
core.homs.1002.1090463836
core.homs.1002.1090463952
core.homs.1002.1090463994
core.homs.1002.1090464010
core.homs.1002.1090464065
core.homs.1002.1090464079
core.homs.1002.1090464106
core.homs.1002.1090464139
core.homs.1002.1090464421
core.homs.1002.1090464477
core.homs.1002.1090464511
core.homs.1002.1090465096
core.homs.1002.1090468442
core.homs.1002.1090468678
core.homs.1002.1090468768
core.homs.1002.1090468944
core.homs.1002.1090469071
core.homs.1002.1090469120
core.httpd.201.1090895899
core.httpd.201.1090934449
core.httpd.201.1090934487
core.isc.1002.1090455050
core.isc.1002.1090455435
core.isc.1002.1090458993
core.isc.1002.1090459060
core.isc.1002.1090484224
core.isc.1002.1090542072
core.isc.1002.1090542131
core.isc.1002.1090542968
core.isc.1002.1090556079
core.isc.1002.1090571107
core.isc.1002.1090582595

# for bugbug in `ls | nawk -F. '{print $4}'
> do
> caldate $bugbug
> done
2004-7-20(0:55:46)
2004-7-20(0:56:32)
2004-7-20(1:30:33)
2004-7-20(1:31:26)
2004-7-20(1:33:55)
2004-7-20(3:14:15)
2004-7-20(3:15:55)
2004-7-20(3:17:24)
2004-7-20(3:17:57)
2004-7-20(3:19:31)
2004-7-20(3:28:35)
2004-7-20(3:34:9)
2004-7-20(3:34:37)
2004-7-20(3:35:19)
2004-7-20(3:35:43)
2004-7-20(3:36:51)
2004-7-20(3:38:16)
2004-7-20(3:40:12)
2004-7-20(3:40:54)
2004-7-20(3:41:10)
2004-7-20(3:42:5)
2004-7-20(3:42:19)
2004-7-20(3:42:46)
2004-7-20(3:43:19)
2004-7-20(3:48:1)
2004-7-20(3:48:57)
2004-7-20(3:49:31)
2004-7-20(3:59:16)
2004-7-20(4:55:2)
2004-7-20(4:58:58)
2004-7-20(5:0:28)
2004-7-20(5:3:24)
2004-7-20(5:5:31)
2004-7-20(5:6:20)
2004-7-25(3:39:19)
2004-7-25(14:21:49)
2004-7-25(14:22:27)
2004-7-20(1:11:50)
2004-7-20(1:18:15)
2004-7-20(2:17:33)
2004-7-20(2:18:40)
2004-7-20(9:18:4)
2004-7-21(1:22:12)
2004-7-21(1:23:11)
2004-7-21(1:37:8)
2004-7-21(5:15:39)
2004-7-21(9:26:7)
2004-7-21(12:37:35)

File attachments: 
첨부파일 크기
Image icon visualization.png5.48 KB
hanbak의 이미지

저라면 이렇게..

#!/usr/bin/perl
use POSIX ;

foreach ( `ls core.*`) {
    @file = split  /\./ , $_ , 4  ;
    print POSIX::strftime "%Y-%m-%d(%H:%M:%S)\n" ,  localtime(@file[3]) ;
}
lifthrasiir의 이미지

0y6k$5*4+k$k$>_0\:!|!:\+*<
=DADCDCDDCDCD======>'0-\a^
==========================
UNIX TIMESTAMP TRANSLATOR!
Kang Seonghoon aka Tokigun
[v0.01] zenith@tokigun.net
==========================
@@@q,a,a,)'.g00, ',:'.g01<
>,' ,30g.'(,' ,20g.':,' ,^
^>60g1+40p50g.'-,' ,40g.'-
<]`# _60g\-60;vp05+1g05p;#
<^g06:-%'g1g04<p041<  <p12
^;>'B#;<>a*%!50g4%#^_#^_'B
%#^_50ga^>60g\-60p50g4aa**
/aag05+/<|>#`0#g<vaa4g05-/
*5+*50g4^$^6:+///<>50g46f*
b+f*4+50p>50g1+50p^>60p8f*
*1+f*9+f*1+f*6+f*1+^>f*3+f
>*/:6a*%10p6a*/:2c*% 20p2v
^6ap00%*6a:g00np00$<^e/*c<

;) Funge 버전입니다. (Funge-98을 지원하는 인터프리터가 필요합니다.) 제대로 동작할 지는 모르겠습니다만 (테스트를 안 해서;;;) 이 프로그램의 원래 코드는 이랬습니다.

## hour/minute/second
stamp = int(raw_input())
sec = stamp % 60; stamp /= 60 
min = stamp % 60; stamp /= 60
hour = stamp % 24; stamp /= 24

## year
stamp += 719162 # 719162 = f*3+f*1+f*9+f*1+f*6+f*2+
year = 1969 # 1969 = 8f*b+f*4+
while True:
	# 365 = 46f**5+; 100 = aa*; 400 = 4aa**
	days = year * 365 + year / 4 - year / 100 + year / 400
	year += 1
	if days >= stamp: break
stamp -= days

## month/day
dayofmonths = [3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3]
if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0):
	dayofmonths[1] = 1
month = 1
while True:
	dayofmonth = 28 + dayofmonths[month - 1]
	if stamp < dayofmonth: break
	stamp -= dayofmonth
	month += 1
day += 1

## print
print '%d - %d - %d ( %d : %d : %d )' % (year, month, day, hour, min, sec)

첨부된 파일은 Funge 코드의 visualization입니다. (좀 잘못 나왔습니다. -_-;)

- 토끼군

댓글 첨부 파일: 
첨부파일 크기
Image icon 0바이트