-
Notifications
You must be signed in to change notification settings - Fork 914
/
RubyTime.java
94 lines (74 loc) · 3.11 KB
/
RubyTime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime.core;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.objects.Allocator;
import org.jruby.truffle.runtime.RubyContext;
import java.util.Date;
/**
* Represents the Ruby {@code Time} class. This is a very rough implementation and is only really
* enough to run benchmark harnesses.
*/
public class RubyTime extends RubyBasicObject {
private long seconds;
private long nanoseconds;
public RubyTime(RubyClass timeClass, long seconds, long nanoseconds) {
super(timeClass);
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}
public long getWholeSeconds() {
return seconds;
}
public double getRealSeconds() {
return seconds + nanosecondsToSecond(nanoseconds);
}
public static RubyTime fromDate(RubyClass timeClass, long timeMiliseconds) {
return new RubyTime(timeClass, milisecondsToSeconds(timeMiliseconds), milisecondsToNanoseconds(timeMiliseconds));
}
public static RubyTime fromArray(RubyClass timeClass,
int second,
int minute,
int hour,
int dayOfMonth,
int month,
int year,
int nanoOfSecond,
boolean isdst,
RubyString zone) {
throw new UnsupportedOperationException();
//ZonedDateTime zdt = ZonedDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond, ZoneId.of(zone.toString()));
//return new RubyTime(timeClass, zdt.toEpochSecond(), nanoOfSecond);
}
public Date toDate() {
return new Date(secondsToMiliseconds(seconds) + nanosecondsToMiliseconds(nanoseconds));
}
private static long milisecondsToSeconds(long miliseconds) {
return miliseconds / 1000;
}
private static long milisecondsToNanoseconds(long miliseconds) {
return (miliseconds % 1000) * 1000000;
}
private static long nanosecondsToMiliseconds(long nanoseconds) {
return nanoseconds / 1000000;
}
public static double nanosecondsToSecond(long nanoseconds) {
return nanoseconds / 1e9;
}
public static long secondsToMiliseconds(long seconds) {
return seconds * 1000;
}
public static class TimeAllocator implements Allocator {
@Override
public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, RubyNode currentNode) {
return new RubyTime(rubyClass, milisecondsToSeconds(System.currentTimeMillis()), milisecondsToNanoseconds(System.currentTimeMillis()));
}
}
}