Skip to content

Commit

Permalink
Merge pull request #6662 from straight-shoota/jm/feature/time-unix-epoch
Browse files Browse the repository at this point in the history
Rename Time#epoch to #to_unix
  • Loading branch information
RX14 authored Oct 3, 2018
2 parents a2f52f0 + 092c205 commit 4e1fc11
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 82 deletions.
4 changes: 2 additions & 2 deletions spec/std/json/mapping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,15 @@ describe "JSON mapping" do
string = %({"value":1459859781})
json = JSONWithTimeEpoch.from_json(string)
json.value.should be_a(Time)
json.value.should eq(Time.epoch(1459859781))
json.value.should eq(Time.unix(1459859781))
json.to_json.should eq(string)
end

it "uses Time::EpochMillisConverter" do
string = %({"value":1459860483856})
json = JSONWithTimeEpochMillis.from_json(string)
json.value.should be_a(Time)
json.value.should eq(Time.epoch_ms(1459860483856))
json.value.should eq(Time.unix_ms(1459860483856))
json.to_json.should eq(string)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/json/serializable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -627,15 +627,15 @@ describe "JSON mapping" do
string = %({"value":1459859781})
json = JSONAttrWithTimeEpoch.from_json(string)
json.value.should be_a(Time)
json.value.should eq(Time.epoch(1459859781))
json.value.should eq(Time.unix(1459859781))
json.to_json.should eq(string)
end

it "uses Time::EpochMillisConverter" do
string = %({"value":1459860483856})
json = JSONAttrWithTimeEpochMillis.from_json(string)
json.value.should be_a(Time)
json.value.should eq(Time.epoch_ms(1459860483856))
json.value.should eq(Time.unix_ms(1459860483856))
json.to_json.should eq(string)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/time/format_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ describe Time::Format do
parse_time("11:12:13", "%T").to_s.should eq("0001-01-01 11:12:13 UTC")
parse_time("This was done on Friday, October 31, 2014", "This was done on %A, %B %d, %Y").to_s.should eq("2014-10-31 00:00:00 UTC")
parse_time("今は Friday, October 31, 2014", "今は %A, %B %d, %Y").to_s.should eq("2014-10-31 00:00:00 UTC")
parse_time("epoch: 1459864667", "epoch: %s").epoch.should eq(1459864667)
parse_time("epoch: -1459864667", "epoch: %s").epoch.should eq(-1459864667)
parse_time("epoch: 1459864667", "epoch: %s").to_unix.should eq(1459864667)
parse_time("epoch: -1459864667", "epoch: %s").to_unix.should eq(-1459864667)
end

it "parses timezone" do
Expand Down
2 changes: 1 addition & 1 deletion spec/std/time/location_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class Time::Location
it "looks up" do
with_zoneinfo do
location = Location.load("Europe/Berlin")
zone, range = location.lookup_with_boundaries(Time.utc(2017, 11, 23, 22, 6, 12).epoch)
zone, range = location.lookup_with_boundaries(Time.utc(2017, 11, 23, 22, 6, 12).to_unix)
zone.should eq Zone.new("CET", 3600, false)
range.should eq({1509238800_i64, 1521939600_i64})
end
Expand Down
26 changes: 13 additions & 13 deletions spec/std/time/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ describe Time do
end
end

it ".epoch" do
it ".unix" do
seconds = 1439404155
time = Time.epoch(seconds)
time = Time.unix(seconds)
time.should eq(Time.utc(2015, 8, 12, 18, 29, 15))
time.epoch.should eq(seconds)
time.to_unix.should eq(seconds)
time.utc?.should be_true
end

it ".epoch_ms" do
it ".unix_ms" do
milliseconds = 1439404155000
time = Time.epoch_ms(milliseconds)
time = Time.unix_ms(milliseconds)
time.should eq(Time.utc(2015, 8, 12, 18, 29, 15))
time.epoch_ms.should eq(milliseconds)
time.to_unix_ms.should eq(milliseconds)
time.utc?.should be_true
end

Expand Down Expand Up @@ -320,17 +320,17 @@ describe Time do
end
end

describe "#epoch" do
it "gets unix epoch seconds" do
describe "#to_unix" do
it "gets unix seconds" do
t1 = Time.utc 2014, 10, 30, 21, 18, 13, nanosecond: 0
t1.epoch.should eq(1414703893)
t1.epoch_f.should be_close(1414703893, 1e-01)
t1.to_unix.should eq(1414703893)
t1.to_unix_f.should be_close(1414703893, 1e-01)
end

it "gets unix epoch seconds at GMT" do
it "gets unix seconds at GMT" do
t1 = Time.now
t1.epoch.should eq(t1.to_utc.epoch)
t1.epoch_f.should be_close(t1.to_utc.epoch_f, 1e-01)
t1.to_unix.should eq(t1.to_utc.to_unix)
t1.to_unix_f.should be_close(t1.to_utc.to_unix_f, 1e-01)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/yaml/mapping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,15 @@ describe "YAML mapping" do
string = %({"value":1459859781})
yaml = YAMLWithTimeEpoch.from_yaml(string)
yaml.value.should be_a(Time)
yaml.value.should eq(Time.epoch(1459859781))
yaml.value.should eq(Time.unix(1459859781))
yaml.to_yaml.should eq("---\nvalue: 1459859781\n")
end

it "uses Time::EpochMillisConverter" do
string = %({"value":1459860483856})
yaml = YAMLWithTimeEpochMillis.from_yaml(string)
yaml.value.should be_a(Time)
yaml.value.should eq(Time.epoch_ms(1459860483856))
yaml.value.should eq(Time.unix_ms(1459860483856))
yaml.to_yaml.should eq("---\nvalue: 1459860483856\n")
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/yaml/serializable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -689,15 +689,15 @@ describe "YAML::Serializable" do
string = %({"value":1459859781})
yaml = YAMLAttrWithTimeEpoch.from_yaml(string)
yaml.value.should be_a(Time)
yaml.value.should eq(Time.epoch(1459859781))
yaml.value.should eq(Time.unix(1459859781))
yaml.to_yaml.should eq("---\nvalue: 1459859781\n")
end

it "uses Time::EpochMillisConverter" do
string = %({"value":1459860483856})
yaml = YAMLAttrWithTimeEpochMillis.from_yaml(string)
yaml.value.should be_a(Time)
yaml.value.should eq(Time.epoch_ms(1459860483856))
yaml.value.should eq(Time.unix_ms(1459860483856))
yaml.to_yaml.should eq("---\nvalue: 1459860483856\n")
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/cache_dir.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module Crystal
private def cleanup_dirs(entries)
entries
.select { |dir| Dir.exists?(dir) }
.sort_by! { |dir| File.info?(dir).try(&.modification_time) || Time.epoch(0) }
.sort_by! { |dir| File.info?(dir).try(&.modification_time) || Time.unix(0) }
.reverse!
.skip(10)
.each { |name| `rm -rf "#{name}"` rescue nil }
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/macros/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Crystal::Program
# Together with their timestamp
# (this is the list of all effective files that were required)
requires_with_timestamps = result.program.requires.map do |required_file|
epoch = File.info(required_file).modification_time.epoch
epoch = File.info(required_file).modification_time.to_unix
RequireWithTimestamp.new(required_file, epoch)
end

Expand Down Expand Up @@ -203,7 +203,7 @@ class Crystal::Program
end

new_requires_with_timestamps = required_files.map do |required_file|
epoch = File.info(required_file).modification_time.epoch
epoch = File.info(required_file).modification_time.to_unix
RequireWithTimestamp.new(required_file, epoch)
end

Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module Crystal::System::File

private def self.to_timeval(time : ::Time)
t = uninitialized LibC::Timeval
t.tv_sec = typeof(t.tv_sec).new(time.to_local.epoch)
t.tv_sec = typeof(t.tv_sec).new(time.to_unix)
t.tv_usec = typeof(t.tv_usec).new(0)
t
end
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ module Crystal::System::File

def self.utime(access_time : ::Time, modification_time : ::Time, path : String) : Nil
times = LibC::Utimbuf64.new
times.actime = access_time.epoch
times.modtime = modification_time.epoch
times.actime = access_time.to_unix
times.modtime = modification_time.to_unix

if LibC._wutime64(to_windows_path(path), pointerof(times)) != 0
raise Errno.new("Error setting time on file #{path.inspect}")
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ module Crystal::System::Time

time += (day - 1).days

time.epoch
time.to_unix
end

# Normalizes the names of the standard and dst zones.
Expand Down
4 changes: 2 additions & 2 deletions src/gzip/header.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Gzip::Header
flg = Flg.new(header[3])

seconds = IO::ByteFormat::LittleEndian.decode(Int32, header.to_slice[4, 4])
@modification_time = Time.epoch(seconds).to_local
@modification_time = Time.unix(seconds).to_local

xfl = header[8]
@os = header[9]
Expand Down Expand Up @@ -77,7 +77,7 @@ class Gzip::Header
io.write_byte flg.value

# time
io.write_bytes(modification_time.epoch.to_u32, IO::ByteFormat::LittleEndian)
io.write_bytes(modification_time.to_unix.to_u32, IO::ByteFormat::LittleEndian)

# xfl
io.write_byte 0_u8
Expand Down
2 changes: 1 addition & 1 deletion src/http/server/handlers/static_file_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class HTTP::StaticFileHandler
end

private def etag(modification_time)
%{W/"#{modification_time.epoch}"}
%{W/"#{modification_time.to_unix}"}
end

private def modification_time(file_path)
Expand Down
4 changes: 2 additions & 2 deletions src/json/from_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ end

module Time::EpochConverter
def self.from_json(value : JSON::PullParser) : Time
Time.epoch(value.read_int)
Time.unix(value.read_int)
end
end

module Time::EpochMillisConverter
def self.from_json(value : JSON::PullParser) : Time
Time.epoch_ms(value.read_int)
Time.unix_ms(value.read_int)
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/json/to_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ end

# Converter to be used with `JSON.mapping` and `YAML.mapping`
# to serialize a `Time` instance as the number of seconds
# since the unix epoch. See `Time.epoch`.
# since the unix epoch. See `Time#to_unix`.
#
# ```
# require "json"
Expand All @@ -155,13 +155,13 @@ end
# ```
module Time::EpochConverter
def self.to_json(value : Time, json : JSON::Builder)
json.number(value.epoch)
json.number(value.to_unix)
end
end

# Converter to be used with `JSON.mapping` and `YAML.mapping`
# to serialize a `Time` instance as the number of milliseconds
# since the unix epoch. See `Time.epoch_ms`.
# since the unix epoch. See `Time#to_unix_ms`.
#
# ```
# require "json"
Expand All @@ -178,7 +178,7 @@ end
# ```
module Time::EpochMillisConverter
def self.to_json(value : Time, json : JSON::Builder)
json.number(value.epoch_ms)
json.number(value.to_unix_ms)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/oauth/oauth.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module OAuth
end

private def self.oauth_header(client, request, token, token_secret, consumer_key, consumer_secret, extra_params)
ts = Time.now.epoch.to_s
ts = Time.utc_now.to_unix.to_s
nonce = Random::Secure.hex

signature = Signature.new consumer_key, consumer_secret, token, token_secret, extra_params
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2/access_token/access_token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class OAuth2::AccessToken
when "bearer"
Bearer.new(access_token, expires_in, refresh_token, scope, extra)
when "mac"
Mac.new(access_token, expires_in, mac_algorithm.not_nil!, mac_key.not_nil!, refresh_token, scope, Time.now.epoch, extra)
Mac.new(access_token, expires_in, mac_algorithm.not_nil!, mac_key.not_nil!, refresh_token, scope, Time.utc_now.to_unix, extra)
else
raise "Unknown token_type in access token json: #{token_type}"
end
Expand Down
4 changes: 2 additions & 2 deletions src/oauth2/access_token/mac.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OAuth2::AccessToken::Mac < OAuth2::AccessToken
property mac_key : String
property issued_at : Int64

def initialize(access_token, expires_in, @mac_algorithm, @mac_key, refresh_token = nil, scope = nil, @issued_at = Time.now.epoch, extra = nil)
def initialize(access_token, expires_in, @mac_algorithm, @mac_key, refresh_token = nil, scope = nil, @issued_at = Time.utc_now.to_unix, extra = nil)
super(access_token, expires_in, refresh_token, scope, extra)
end

Expand All @@ -21,7 +21,7 @@ class OAuth2::AccessToken::Mac < OAuth2::AccessToken
end

def authenticate(request : HTTP::Request, tls)
ts = Time.now.epoch
ts = Time.utc_now.to_unix
nonce = "#{ts - @issued_at}:#{Random::Secure.hex}"
method = request.method
uri = request.resource
Expand Down
26 changes: 13 additions & 13 deletions src/time.cr
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ struct Time
# The time zone is always UTC.
#
# ```
# Time.epoch(981173106) # => 2001-02-03 04:05:06 UTC
# Time.unix(981173106) # => 2001-02-03 04:05:06 UTC
# ```
def self.epoch(seconds : Int) : Time
def self.unix(seconds : Int) : Time
utc(seconds: UNIX_SECONDS + seconds, nanoseconds: 0)
end

Expand All @@ -497,10 +497,10 @@ struct Time
# The time zone is always UTC.
#
# ```
# time = Time.epoch_ms(981173106789) # => 2001-02-03 04:05:06.789 UTC
# time.millisecond # => 789
# time = Time.unix_ms(981173106789) # => 2001-02-03 04:05:06.789 UTC
# time.millisecond # => 789
# ```
def self.epoch_ms(milliseconds : Int) : Time
def self.unix_ms(milliseconds : Int) : Time
milliseconds = milliseconds.to_i64
seconds = UNIX_SECONDS + (milliseconds / 1_000)
nanoseconds = (milliseconds % 1000) * NANOSECONDS_PER_MILLISECOND
Expand Down Expand Up @@ -1023,9 +1023,9 @@ struct Time
#
# ```
# time = Time.utc(2016, 1, 12, 3, 4, 5)
# time.epoch # => 1452567845
# time.to_unix # => 1452567845
# ```
def epoch : Int64
def to_unix : Int64
(total_seconds - UNIX_SECONDS).to_i64
end

Expand All @@ -1034,21 +1034,21 @@ struct Time
#
# ```
# time = Time.utc(2016, 1, 12, 3, 4, 5, nanosecond: 678_000_000)
# time.epoch_ms # => 1452567845678
# time.to_unix_ms # => 1452567845678
# ```
def epoch_ms : Int64
epoch * 1_000 + (nanosecond / NANOSECONDS_PER_MILLISECOND)
def to_unix_ms : Int64
to_unix * 1_000 + (nanosecond / NANOSECONDS_PER_MILLISECOND)
end

# Returns the number of seconds since the Unix epoch
# (`1970-01-01 00:00:00 UTC`) as `Float64` with nanosecond precision.
#
# ```
# time = Time.utc(2016, 1, 12, 3, 4, 5, nanosecond: 678_000_000)
# time.epoch_f # => 1452567845.678
# time.to_unix_f # => 1452567845.678
# ```
def epoch_f : Float64
epoch.to_f + nanosecond.to_f / 1e9
def to_unix_f : Float64
to_unix.to_f + nanosecond.to_f / 1e9
end

# Returns a copy of this `Time` representing the same instant observed in
Expand Down
2 changes: 1 addition & 1 deletion src/time/format.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require "./format/parser"
# * **%P**: AM-PM (uppercase)
# * **%r**: 12-hour time (03:04:05 AM)
# * **%R**: 24-hour time (13:04)
# * **%s**: seconds since unix epoch (see `Time.epoch`)
# * **%s**: seconds since unix epoch (see `Time#to_unix`)
# * **%S**: seconds, zero padded (00, 01, ..., 59)
# * **%T**: 24-hour time (13:04:05)
# * **%u**: day of week (Monday is 1, 1..7)
Expand Down
4 changes: 2 additions & 2 deletions src/time/format/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ struct Time::Format
io << time.day_of_week.value
end

def epoch
io << time.epoch
def unix_seconds
io << time.to_unix
end

def time_zone(with_seconds = false)
Expand Down
Loading

0 comments on commit 4e1fc11

Please sign in to comment.