Tagged: timestamp
ISO 8601 Date/Time
RFC-3339 / 5.6. Internet Date/Time Format에 ISO-8601 Date/Time 에서 대해 설명하고 있다.
년년년년-월월-일일T시시:분분:초초
까지는 대충 쓰면 되는데 그 뒤의 부분들이 문제다. (초초
부분도 0~59
만 사용되는 것이 아니지만 일단 넘어가자.)
time-secfrac
time-secfrac = "." 1*DIGIT
초초
뒤에 .
을 붙이고 1개 이상의 초 단위 이하의 값을 사용할 수가 있다.
1976-10-31T10:00:00.2134213213213213213213Z
“Z” / time-numoffset
보통의 경우에 다음과 같이 오프셋을 사용할 수 있다.
1976-10-31T10:00:00+09:00 1976-10-31T10:00:00-08:00
근데 00:00
앞에는 +
를 붙여야 하는가 -
를 붙여야 하는가?
머리를 어떻게 굴렸는지는 잘 모르겠지만 Z
를 사용하기로 했나보다.
1976-10-31T10:00:00Z
4.3. Unknown Local Offset Convention 에 보면 Z
는 +00:00
과 같고 -00:00
은 TimeZone 을 모를(unknown) 때 사용한다고 한다.
simple method for timestamp and nonce
Some internet protocols such as OAuth need timestamp
s and nonce
s.
Here comes what 8. Nonce and Timestamp saids in OAuth Core 1.0 Revision A,
Unless otherwise specified by the Service Provider, the timestamp is expressed in the number of seconds since January 1, 1970 00:00:00 GMT. The timestamp value MUST be a positive integer and MUST be equal or greater than the timestamp used in previous requests.
The Consumer SHALL then generate a Nonce value that is unique for all requests with that timestamp. A nonce is a random string, uniquely generated for each request. The nonce allows the Service Provider to verify that a request has never been made before and helps prevent replay attacks when requests are made over a non-secure channel (such as HTTP).
Some just use System.nanoTime().
Let’s do something. As of writing this entry, System.currentTimeMillis()
gave me 1354169632072
which has 23 high zero bits.
current millis 1354169632072 0x13B4ACCC548 Thu Nov 29 15:13:52 KST 2012 0000 0000 0000 0000 0000 0001 0011 1011 0100 1010 1100 1100 1100 0101 0100 1000 left shift by 3 10833357056576 0x9DA56662A40 Sat Apr 19 10:50:56 KST 2313 0000 0000 0000 0000 0000 1001 1101 1010 0101 0110 0110 0110 0010 1010 0100 0000 left shift by 20 1419949776119529472 0x13B4ACCC54800000 Tue Feb 24 15:32:09 KST 44998387 0001 0011 1011 0100 1010 1100 1100 1100 0101 0100 1000 0000 0000 0000 0000 0000
Note that shifting 3 bits serves about 300 years.
We can have, by shifting 20 bits, free 20 bits for random number with given timestamp
s.
// <20-bit shifted current millis> | <20-bit random> // 000T TTTT TTTT TTTT TTTT TTTT TTTT TTTT // TTTT TTTT TTTT NNNN NNNN NNNN NNNN NNNN // this method will be valid for about 300 years // possibility of collisions: one in a million in the very same millisecond final long nonce = (System.currentTimeMillis() << 20) | ThreadLocalRandom.current().nextInt(1048576);
Here are some methods.
public static long nonce(final long timestamp, final Random random) { return (timestamp << 20) | random.nextInt(1048576); } public static long nonce(final long timestamp) { return nonce(timestamp, ThreadLocalRandom.current()); } public static long nonce(final Random random) { return nonce(System.currentTimeMillis(), random); } public static long nonce() { return nonce(System.currentTimeMillis(), ThreadLocalRandom.current()); }