vremya 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29979ba2726045307bea89ab12de4138bdced601
4
+ data.tar.gz: 352eae23db9f81cc1de06a91239b62d5ef5882ef
5
+ SHA512:
6
+ metadata.gz: 2c0efec2a2469615a1b3b697e45a3e3508680dc8e02ca3b5be4563e053b5091963a11b5ee57fbce4f04bc6c8af717616b2568e5ea442e02be304e1698bb69b2f
7
+ data.tar.gz: ebf2e5a5b56bdd5c439ac73ed4116da1616f7d75b6c0e99ec8e352b18adc98e353f356073b487a7c2329130f2769c059240cc1ca6c57a74f476d8ed9d1237862
data/Rakefile CHANGED
@@ -1,17 +1,19 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
- require 'rspec/core/rake_task'
3
+ require 'rake/testtask'
4
4
  require 'yard'
5
5
 
6
6
  Bundler.require
7
7
  Bundler::GemHelper.install_tasks
8
8
 
9
- desc "Run specs"
10
- RSpec::Core::RakeTask.new do |t|
11
- # nothing
9
+ desc "Run tests"
10
+ Rake::TestTask.new do |t|
11
+ t.test_files = FileList['spec/**/*_spec.rb']
12
12
  end
13
13
 
14
14
  desc "Generate docs"
15
15
  YARD::Rake::YardocTask.new do |t|
16
- t.files = ['lib/**/*.rb']
16
+ t.files = ['lib/**/*.rb']
17
17
  end
18
+
19
+ task :default => :test
@@ -0,0 +1,58 @@
1
+ class Vremya
2
+ module Locale
3
+ RU = {
4
+ "январь" => "January",
5
+ "января" => "January",
6
+ "янв" => "Jan",
7
+ "февраль" => "February",
8
+ "февраля" => "February",
9
+ "фев" => "Feb",
10
+ "февр" => "Feb",
11
+ "март" => "March",
12
+ "марта" => "March",
13
+ "мар" => "Mar",
14
+ "апрель" => "April",
15
+ "апреля" => "April",
16
+ "апр" => "Apr",
17
+ "май" => "May",
18
+ "мая" => "May",
19
+ "июнь" => "June",
20
+ "июня" => "June",
21
+ "июн" => "Jun",
22
+ "июль" => "July",
23
+ "июля" => "July",
24
+ "июл" => "Jul",
25
+ "август" => "August",
26
+ "августа" => "August",
27
+ "авг" => "Aug",
28
+ "сентябрь" => "September",
29
+ "сентября" => "September",
30
+ "сен" => "Sep",
31
+ "сент" => "Sep",
32
+ "октябрь" => "October",
33
+ "октября" => "October",
34
+ "окт" => "Oct",
35
+ "ноябрь" => "November",
36
+ "ноября" => "November",
37
+ "ноя" => "Nov",
38
+ "нояб" => "Nov",
39
+ "декабрь" => "December",
40
+ "декабря" => "December",
41
+ "дек" => "Dec",
42
+ "понедельник" => "Monday",
43
+ "пн" => "Mon",
44
+ "вторник" => "Tuesday",
45
+ "вт" => "Tue",
46
+ "среда" => "Wednesday",
47
+ "ср" => "Wed",
48
+ "четверг" => "Thursday",
49
+ "чт" => "Thu",
50
+ "пятница" => "Friday",
51
+ "пт" => "Fri",
52
+ "суббота" => "Saturday",
53
+ "сб" => "Sat",
54
+ "воскресенье" => "Sunday",
55
+ "вс" => "Sun"
56
+ }
57
+ end
58
+ end
@@ -0,0 +1,85 @@
1
+ class Vremya
2
+ class Timezone
3
+
4
+ def self.all
5
+ TZInfo::Timezone.all_identifiers
6
+ end
7
+
8
+ STR = /([+-])(\d{2}):(\d{2})/
9
+
10
+ attr_reader :value
11
+
12
+ def self.at(value, year, mon = nil, day = nil, hour = nil, min = nil, sec = nil)
13
+ stamp = Time.gm(year, mon, day, hour, min, sec)
14
+ new(value, stamp.to_i - new(value, stamp).offset)
15
+ end
16
+
17
+ def initialize(value, stamp = nil)
18
+ @value = value
19
+ @stamp = stamp
20
+ if block_given?
21
+ yield self
22
+ end
23
+ end
24
+
25
+ def name_or_offset
26
+ name || offset
27
+ end
28
+
29
+ def name
30
+ if value.nil?
31
+ return 'GMT'
32
+ end
33
+ unless value.is_a?(String)
34
+ return nil
35
+ end
36
+ if self.class.all.include?(value)
37
+ return value
38
+ end
39
+ nil
40
+ end
41
+
42
+ def offset
43
+ if value.nil?
44
+ return 0
45
+ end
46
+ if value.is_a?(Numeric)
47
+ return value
48
+ end
49
+ if value.is_a?(String) && value =~ STR
50
+ return ($2.to_i * 3600 + $3.to_i * 60) * ($1 == '-' ? -1 : 1)
51
+ end
52
+ unless name.nil?
53
+ return period.utc_offset
54
+ end
55
+ 0
56
+ end
57
+
58
+ def dst?
59
+ tz.nil? ? false : tz.dst?
60
+ end
61
+
62
+ def to_s
63
+ "%s%s" % [offset < 0 ? '-' : '+', "%02d:%02d" % (offset / 60).divmod(60)]
64
+ end
65
+
66
+ private
67
+
68
+ def tz
69
+ name.nil? ? nil : TZInfo::Timezone.get(value)
70
+ end
71
+
72
+ def period
73
+ if tz.nil?
74
+ return nil
75
+ end
76
+ if @stamp.nil?
77
+ return tz.current_period
78
+ end
79
+ tz.period_for_utc(@stamp) do |p|
80
+ p.first
81
+ end
82
+ end
83
+
84
+ end
85
+ end
data/lib/vremya.rb CHANGED
@@ -1,69 +1,139 @@
1
1
  require 'tzinfo'
2
+ require_relative 'vremya/locale/ru'
3
+ require_relative 'vremya/timezone'
2
4
 
3
5
  class Vremya
4
6
 
5
7
  SECONDS_IN_A_DAY = 60 * 60 * 24
6
8
 
9
+ def self.guess(options = {})
10
+ raise "not implemented"
11
+ end
12
+
13
+ # %Y - Year with century (can be negative, 4 digits at least)
14
+ # %C - year / 100 (round down. 20 in 2009)
15
+ # %y - year % 100 (00..99)
16
+ # %m - Month of the year, zero-padded (01..12)
17
+ # %-m - Month of the year, no-padded (1..12)
18
+ # %B - The full month name (``January'')
19
+ # %^B - Month name uppercased (``JANUARY'')
20
+ # %b - Abbreviated month name (``Jan'')
21
+ # %^b - Abbreviated uppercased month (``JAN'')
22
+ # %d - Day of the month, zero-padded (01..31)
23
+ # %-d - Day of the month, no-padded (1..31)
24
+ # %e - Day of the month, blank-padded ( 1..31)
25
+ # %j - Day of the year (001..366)
26
+ # %H - Hour of the day, 24-hour clock, zero-padded (00..23)
27
+ # %k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
28
+ # %I - Hour of the day, 12-hour clock, zero-padded (01..12)
29
+ # %l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
30
+ # %P - Meridian indicator, lowercase (``am'' or ``pm'')
31
+ # %p - Meridian indicator, uppercase (``AM'' or ``PM'')
32
+ # %M - Minute of the hour (00..59)
33
+ # %S - Second of the minute (00..59)
34
+ # %L - Millisecond of the second (000..999)
35
+ # %N - Fractional seconds digits, default is 9 digits (nanosecond)
36
+ # %z - Time zone as hour and minute offset from UTC (e.g. +0900)
37
+ # %:z - hour and minute offset from UTC with a colon (e.g. +09:00)
38
+ # %Z - Time zone abbreviation name
39
+ # %A - The full weekday name (``Sunday'')
40
+ # %^A - Weekday, uppercased (``SUNDAY'')
41
+ # %a - Weekday, abbreviated (``Sun'')
42
+ # %^a - Weekday, uppercased (``SUN'')
43
+ # %u - Day of the week (Monday is 1, 1..7)
44
+ # %w - Day of the week (Sunday is 0, 0..6)
45
+ # %s - Number of seconds since 1970-01-01 00:00:00 UTC.
46
+ # %Q - Number of milliseconds since 1970-01-01 00:00:00 UTC.
47
+ def self.parse(string, format, timezone = nil)
48
+ Locale::RU.each do |key, value|
49
+ string.gsub!(/\b#{key}\b/, value)
50
+ end
51
+ timestamp = DateTime.strptime(string, format)
52
+ self.at(timestamp).as_if_in(timezone)
53
+ end
54
+
55
+ # Returns a new Vremya object initialized to the current time.
56
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
7
57
  def self.now(zone = nil)
8
- self.new(Time.now.gmtime.to_i, zone)
58
+ from_time Time.now, zone
9
59
  end
10
60
 
11
- def self.parse(string, format, timezone = nil)
12
- self.new(DateTime.strptime(string, format)).as_if_in(timezone)
61
+ # Returns a new Vremya object initialized at specified UNIX timestamp.
62
+ # @param [Integer] timestamp
63
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
64
+ def self.at(timestamp, zone = nil)
65
+ from_time Time.at(timestamp + Timezone.new(zone, timestamp).offset), zone
13
66
  end
14
67
 
15
- def self.timezones
16
- TZInfo::Timezone.all_identifiers
68
+ # Creates a new Vremya object based on given values, interpreted as UTC (GMT).
69
+ def self.gm(*args)
70
+ from_time Time.gm(*args), "GMT"
17
71
  end
18
72
 
19
- # Returns a new Vremya object with the value given by time since the Epoch.
73
+ # Create a new Vremya object from a native Time instance.
74
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
75
+ def self.from_time(time, zone = nil)
76
+ new *time.utc.to_a[0..5].reverse, zone
77
+ end
78
+
79
+ # Creates a new Vremya object based on given values.
80
+ # @param [Integer] year
81
+ # @param [Integer] month
82
+ # @param [Integer] day
83
+ # @param [Integer] hour
84
+ # @param [Integer] min
85
+ # @param [Integer] sec
86
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
20
87
  # @return [Vremya]
21
- def initialize(timestamp, zone = nil)
22
- if timestamp.is_a?(DateTime)
23
- timestamp = timestamp.strftime("%s").to_i
24
- end
25
- unless timestamp.respond_to?(:to_i)
26
- raise 'Timestamp expected, %s' % timestamp.class
27
- end
28
- @zone = TZInfo::Timezone.get(zone || 'GMT')
29
- @stamp = timestamp.to_i
88
+ def initialize(year, month = nil, day = nil, hour = nil, min = nil, sec = nil, offset = nil)
89
+ @zone = Timezone.at(offset, year, month, day, hour, min, sec)
90
+ @time = Time.new(year, month, day, hour, min, sec, @zone.offset)
30
91
  end
31
92
 
32
93
  # Returns a new Vremya object where one or more of the elements have been
33
94
  # changed according to the options parameter. The following options are
34
95
  # available: :year, :month, :day, :hour, :minute, :second.
35
- def change(options)
36
- opts = to_hash.merge(options)
37
- date = Date.new(opts[:year], opts[:month], opts[:day])
38
- advance({
39
- :days => (date - to_date).to_i,
40
- :hours => opts[:hour] - hour,
41
- :minutes => opts[:minute] - minute,
42
- :seconds => opts[:second] - second
43
- })
96
+ def change(opts)
97
+ self.class.new(
98
+ opts[:year] || year,
99
+ opts[:month] || month,
100
+ opts[:day] || day,
101
+ opts[:hour] || hour,
102
+ opts[:minute] || minute,
103
+ opts[:second] || second,
104
+ @zone.name_or_offset
105
+ )
44
106
  end
45
107
 
46
108
  # Returns a new Vremya object by going forward or back in time according
47
109
  # to the options parameter. The following options are available:
48
110
  # :years, :months, :days, :hours, :minutes, :seconds.
49
- def advance(options)
50
- opts = {
51
- :years => 0,
52
- :months => 0,
53
- :days => 0,
54
- :hours => 0,
55
- :minutes => 0,
56
- :seconds => 0
57
- }.merge(options)
111
+ def advance(opts)
58
112
  date = to_date
59
- date = date >> opts[:years] * 12
60
- date = date >> opts[:months]
61
- date = date + opts[:days]
62
- diff = (date - to_date).to_i * 60 * 60 * 24
63
- diff += opts[:hours] * 3600
64
- diff += opts[:minutes] * 60
65
- diff += opts[:seconds]
66
- self + diff
113
+ diff = 0
114
+ unless opts[:years].nil?
115
+ date = date >> opts[:years] * 12
116
+ end
117
+ unless opts[:months].nil?
118
+ date = date >> opts[:months]
119
+ end
120
+ unless opts[:days].nil?
121
+ date = date + opts[:days]
122
+ end
123
+ unless opts[:hours].nil?
124
+ diff += opts[:hours] * 3600
125
+ end
126
+ unless opts[:minutes].nil?
127
+ diff += opts[:minutes] * 60
128
+ end
129
+ unless opts[:seconds].nil?
130
+ diff += opts[:seconds]
131
+ end
132
+ diff += (date - to_date) * 60 * 60 * 24
133
+ time = self + diff
134
+ shift = utc_offset - time.utc_offset
135
+ time += shift if opts[:hours].nil? and shift != 0
136
+ time
67
137
  end
68
138
 
69
139
  # Returns same time and date one month ago. In cases where previous month
@@ -125,7 +195,7 @@ class Vremya
125
195
  # Returns a new Time representing the "start" of this week (Monday, 0:00).
126
196
  # @return [Vremya]
127
197
  def beginning_of_week
128
- advance(:days => wday == 0 ? -6 : 1 - wday)
198
+ change(:day => day - wday_starting_on_monday, :hour => 0, :minute => 0, :second => 0)
129
199
  end
130
200
 
131
201
  alias_method :at_beginning_of_week, :beginning_of_week
@@ -149,7 +219,7 @@ class Vremya
149
219
  # Returns the second of the minute (0..60).
150
220
  # @return [Fixnum]
151
221
  def second
152
- time_in_utc.sec
222
+ to_time.sec
153
223
  end
154
224
 
155
225
  alias_method :sec, :second
@@ -157,7 +227,7 @@ class Vremya
157
227
  # Returns the minute of the hour (0..59) for time.
158
228
  # @return [Fixnum]
159
229
  def minute
160
- time_in_utc.min
230
+ to_time.min
161
231
  end
162
232
 
163
233
  alias_method :min, :minute
@@ -165,25 +235,25 @@ class Vremya
165
235
  # Returns the hour of the day (0..23).
166
236
  # @return [Fixnum]
167
237
  def hour
168
- time_in_utc.hour
238
+ to_time.hour
169
239
  end
170
240
 
171
241
  # Returns the day of the month (1..n).
172
242
  # @return [Fixnum]
173
243
  def day
174
- time_in_utc.day
244
+ to_time.day
175
245
  end
176
246
 
177
247
  # Returns the month of the year (1..12).
178
248
  # @return [Fixnum]
179
249
  def month
180
- time_in_utc.month
250
+ to_time.month
181
251
  end
182
252
 
183
253
  # Returns the year (including the century).
184
254
  # @return [Fixnum]
185
255
  def year
186
- time_in_utc.year
256
+ to_time.year
187
257
  end
188
258
 
189
259
  # Returns true if specified time lies in the future.
@@ -363,23 +433,30 @@ class Vremya
363
433
  # Returns an integer representing the day of the week, 0..6, with Sunday == 0.
364
434
  # @return [Fixnum]
365
435
  def wday
366
- time_in_utc.wday
436
+ to_time.wday
367
437
  end
368
438
 
369
439
  # Returns an integer representing the day of the week, 0..6. Similar to #wday, but assumes Monday == 0 and Sunday == 6.
440
+ # @return [Fixnum]
370
441
  def wday_starting_on_monday
371
442
  sunday? ? 6 : wday - 1
372
443
  end
373
444
 
445
+ # Returns an integer representing the day of the year, 1..366.
446
+ # @return [Fixnnum]
447
+ def yday
448
+ to_time.yday
449
+ end
450
+
374
451
  # Returns true if daylight savings are in effect at this timezone.
375
452
  # @return [Boolean]
376
453
  def daylight_saving?
377
- @zone.current_period.dst?
454
+ @zone.dst?
378
455
  end
379
456
 
380
457
  alias_method :dst?, :daylight_saving?
381
458
 
382
- # Returns timezone identifier, e.g. "Europe/Paris"
459
+ # Returns timezone identifier or GMT offset, e.g. "Europe/Paris", "+03:00"
383
460
  # @return [String]
384
461
  def timezone_name
385
462
  @zone.name
@@ -388,57 +465,69 @@ class Vremya
388
465
  # Returns the offset in seconds between the timezone of time and UTC.
389
466
  # @return [Fixnum]
390
467
  def utc_offset
391
- @zone.current_period.utc_offset
468
+ @zone.offset
392
469
  end
393
470
 
394
471
  # Returns a new Vremya object in GMT timezone.
395
472
  # @return [Vremya]
396
473
  def to_utc
397
- self.class.new(to_i)
474
+ self.class.at(to_i)
398
475
  end
399
476
 
400
477
  # Returns a new Vremya object in specified timezone.
478
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
401
479
  # @return [Vremya]
402
480
  def to_timezone(zone)
403
- self.class.new(to_i, zone)
481
+ self.class.at(to_i, zone)
404
482
  end
405
483
 
406
484
  # Returns the difference with specified timezone in seconds.
485
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
407
486
  # @return [Integer]
408
487
  def difference_with_zone(zone)
409
488
  to_timezone(zone).utc_offset - utc_offset
410
489
  end
411
490
 
412
491
  # Returns a new Vremya instance by advancing in time.
492
+ # @param [String, Numeric] offset Time zone name or GMT offset in seconds
493
+ # @return [Vremya]
413
494
  def as_if_in(zone)
414
495
  to_timezone(zone) - difference_with_zone(zone)
415
496
  end
416
497
 
417
498
  # Substracts some number of seconds to time and returns that value as a new Vremya object.
499
+ # @param [Integer] seconds
418
500
  # @return [Vremya]
419
- def -(value)
420
- self.+(-value)
501
+ def -(seconds)
502
+ self.+(-seconds)
421
503
  end
422
504
 
423
505
  # Adds some number of seconds to time and returns that value as a new Vremya object.
506
+ # @param [Integer] seconds
424
507
  # @return [Vremya]
425
- def +(value)
426
- self.class.new(to_i + value, timezone_name)
508
+ def +(seconds)
509
+ self.class.at(to_i + seconds, timezone_name)
427
510
  end
428
511
 
429
512
  # Returns the number of seconds since 1970-01-01 00:00:00 UTC.
430
513
  # @return [Fixnum]
431
514
  def to_i
432
- @stamp
515
+ @time.to_i
516
+ end
517
+
518
+ # Returns a duplicate instance.
519
+ # @return [Vremya]
520
+ def dup
521
+ self + 0
433
522
  end
434
523
 
435
- # Returns a native Ruby Time object in UTC timezone.
524
+ # Returns a native Ruby Time object in specified timezone.
436
525
  # @return [Time]
437
526
  def to_time
438
- Time.at(@stamp).getutc
527
+ @time.dup
439
528
  end
440
-
441
- # Returns a native Ruby Time object in UTC timezone.
529
+
530
+ # Returns a native Ruby Time object.
442
531
  # @return [Time]
443
532
  def to_date
444
533
  Date.new(year, month, day)
@@ -465,7 +554,13 @@ class Vremya
465
554
  # Returns a string representing time.
466
555
  # @return [String]
467
556
  def to_s
468
- strftime("%a %b %d %T %z %Y")
557
+ strftime("%Y-%m-%d %H:%M:%S %z")
558
+ end
559
+
560
+ # Returns a ten-element array of values for time: [sec, min, hour, day, month, year, wday, yday, dst?, timezone_name]
561
+ # @return [Array]
562
+ def to_a
563
+ [sec, min, hour, day, month, year, wday, yday, dst?, timezone_name]
469
564
  end
470
565
 
471
566
  # Returns a hash with time & date attributes.
@@ -489,21 +584,7 @@ class Vremya
489
584
  # Returns time as a string according to the given format. The formatting is identical to Time.strftime
490
585
  # @return [String]
491
586
  def strftime(string)
492
- @zone.strftime string.gsub(/%z/, utc_offset_as_string), to_time
493
- end
494
-
495
- private
496
-
497
- def utc_offset_as_string
498
- "%s%s" % [utc_offset < 0 ? '-' : '+', formatted_hours(utc_offset)]
499
- end
500
-
501
- def formatted_hours(seconds)
502
- "%02d:%02d" % (seconds / 60).divmod(60)
503
- end
504
-
505
- def time_in_utc
506
- to_time + utc_offset
587
+ to_time.strftime string.gsub(/%z/, @zone.to_s)
507
588
  end
508
589
 
509
590
  end
@@ -0,0 +1,132 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Vremya do
4
+
5
+ let(:moscow) do
6
+ Vremya.new(2014, 10, 26, 18, 10, 15, "Europe/Moscow")
7
+ end
8
+
9
+ describe ".new" do
10
+ it "returns valid Vremya when only one argument is specified" do
11
+ Vremya.new(2014).to_s.must_be :==, "2014-01-01 00:00:00 +00:00"
12
+ end
13
+ it "returns valid Vremya when only 2 arguments are specified" do
14
+ Vremya.new(2014, 10).to_s.must_be :==, "2014-10-01 00:00:00 +00:00"
15
+ end
16
+ it "returns valid Vremya when only 3 arguments are specified" do
17
+ Vremya.new(2014, 10, 26).to_s.must_be :==, "2014-10-26 00:00:00 +00:00"
18
+ end
19
+ it "returns valid Vremya when only 4 arguments are specified" do
20
+ Vremya.new(2014, 10, 26, 18).to_s.must_be :==, "2014-10-26 18:00:00 +00:00"
21
+ end
22
+ it "returns valid Vremya when only 5 arguments are specified" do
23
+ Vremya.new(2014, 10, 26, 18, 10).to_s.must_be :==, "2014-10-26 18:10:00 +00:00"
24
+ end
25
+ it "returns valid Vremya when offset is not specified" do
26
+ Vremya.new(2014, 10, 26, 18, 10, 15).to_s.must_be :==, "2014-10-26 18:10:15 +00:00"
27
+ end
28
+ it "returns valid Vremya when offset is specified as zone" do
29
+ Vremya.new(2014, 10, 26, 18, 10, 15, "Europe/Moscow").to_s.must_be :==, "2014-10-26 18:10:15 +03:00"
30
+ end
31
+ it "returns valid Vremya when offset is specified as a number" do
32
+ Vremya.new(2014, 10, 26, 18, 10, 15, 3600 * 3).to_s.must_be :==, "2014-10-26 18:10:15 +03:00"
33
+ end
34
+ it "returns valid Vremya when offset is specified as a string" do
35
+ Vremya.new(2014, 10, 26, 18, 10, 15, "+03:00").to_s.must_be :==, "2014-10-26 18:10:15 +03:00"
36
+ end
37
+ end
38
+
39
+ describe ".now" do
40
+ it "returns valid Vremya object" do
41
+ Vremya.now.to_i.must_be :==, Time.now.to_i
42
+ end
43
+ end
44
+
45
+ describe ".at" do
46
+ it "returns valid Vremya object when timezone is not specified" do
47
+ Vremya.at(1414336215).to_s.must_be :==, "2014-10-26 15:10:15 +00:00"
48
+ end
49
+ it "returns valid Vremya object with correct timezone" do
50
+ Vremya.at(1414336215, "Europe/Moscow").to_s.must_be :==, "2014-10-26 18:10:15 +03:00"
51
+ end
52
+ end
53
+
54
+ describe ".gm" do
55
+ it "returns valid Vremya object in UTC timezone" do
56
+ Vremya.gm(2014, 10, 26, 18, 10, 15).to_s.must_be :==, "2014-10-26 18:10:15 +00:00"
57
+ end
58
+ end
59
+
60
+ describe "#second" do
61
+ it "returns correct second of the minute" do
62
+ moscow.second.must_be :==, 15
63
+ end
64
+ end
65
+
66
+ describe "#minute" do
67
+ it "returns correct minute of the hour" do
68
+ moscow.minute.must_be :==, 10
69
+ end
70
+ end
71
+
72
+ describe "#hour" do
73
+ it "returns correct hour of the day" do
74
+ moscow.hour.must_be :==, 18
75
+ end
76
+ end
77
+
78
+ describe "#day" do
79
+ it "returns correct day of the month" do
80
+ moscow.day.must_be :==, 26
81
+ end
82
+ end
83
+
84
+ describe "#month" do
85
+ it "returns correct month of the year" do
86
+ moscow.month.must_be :==, 10
87
+ end
88
+ end
89
+
90
+ describe "#year" do
91
+ it "returns correct year" do
92
+ moscow.year.must_be :==, 2014
93
+ end
94
+ end
95
+
96
+ describe "#yday" do
97
+ it "returns correct day of the year" do
98
+ moscow.yday.must_be :==, 299
99
+ end
100
+ end
101
+
102
+ describe "#wday" do
103
+ it "returns correct working day index" do
104
+ moscow.wday.must_be :==, 0
105
+ end
106
+ end
107
+
108
+ describe "#wday_starting_on_monday" do
109
+ it "returns correct working day index" do
110
+ moscow.wday_starting_on_monday.must_be :==, 6
111
+ end
112
+ end
113
+
114
+ describe "#timezone_name" do
115
+ it "returns name of the timezone when specified" do
116
+ moscow.timezone_name.must_be :==, "Europe/Moscow"
117
+ end
118
+ it "returns nil when offset is supplied instead of zone" do
119
+ Vremya.new(2014, 10, 26, 18, 10, 15, "+01:00").timezone_name.must_be_nil
120
+ end
121
+ it "returns GMT by default" do
122
+ Vremya.new(2014, 10, 26, 18, 10, 15).timezone_name.must_be :==, "GMT"
123
+ end
124
+ end
125
+
126
+ describe "#utc_offset" do
127
+ it "returns valid UTC offset" do
128
+ moscow.utc_offset.must_be :==, 3600 * 3
129
+ end
130
+ end
131
+
132
+ end
@@ -0,0 +1,37 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Vremya do
4
+
5
+ describe "#to_utc" do
6
+ it
7
+ end
8
+
9
+ describe "#to_timezone" do
10
+ it
11
+ end
12
+
13
+ describe "#to_i" do
14
+ it
15
+ end
16
+
17
+ describe "#to_time" do
18
+ it
19
+ end
20
+
21
+ describe "#to_date" do
22
+ it
23
+ end
24
+
25
+ describe "#to_hash" do
26
+ it
27
+ end
28
+
29
+ describe "#as_yaml" do
30
+ it
31
+ end
32
+
33
+ describe "#to_yaml" do
34
+ it
35
+ end
36
+
37
+ end
@@ -0,0 +1,25 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Vremya do
4
+
5
+ describe "#rfc2616" do
6
+ it
7
+ end
8
+
9
+ describe "#rfc2822" do
10
+ it
11
+ end
12
+
13
+ describe "#iso8601" do
14
+ it
15
+ end
16
+
17
+ describe "#to_s" do
18
+ it
19
+ end
20
+
21
+ describe "#strftime" do
22
+ it
23
+ end
24
+
25
+ end
@@ -0,0 +1,101 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Vremya do
4
+
5
+ describe "#future?" do
6
+ it
7
+ end
8
+
9
+ describe "#past?" do
10
+ it
11
+ end
12
+
13
+ describe "#january?" do
14
+ it
15
+ end
16
+
17
+ describe "#february?" do
18
+ it
19
+ end
20
+
21
+ describe "#march?" do
22
+ it
23
+ end
24
+
25
+ describe "#april?" do
26
+ it
27
+ end
28
+
29
+ describe "#may?" do
30
+ it
31
+ end
32
+
33
+ describe "#june?" do
34
+ it
35
+ end
36
+
37
+ describe "#july?" do
38
+ it
39
+ end
40
+
41
+ describe "#august?" do
42
+ it
43
+ end
44
+
45
+ describe "#september?" do
46
+ it
47
+ end
48
+
49
+ describe "#october?" do
50
+ it
51
+ end
52
+
53
+ describe "#november?" do
54
+ it
55
+ end
56
+
57
+ describe "#december?" do
58
+ it
59
+ end
60
+
61
+ describe "#monday?" do
62
+ it
63
+ end
64
+
65
+ describe "#tuesday?" do
66
+ it
67
+ end
68
+
69
+ describe "#wednesday?" do
70
+ it
71
+ end
72
+
73
+ describe "#thursday?" do
74
+ it
75
+ end
76
+
77
+ describe "#friday?" do
78
+ it
79
+ end
80
+
81
+ describe "#saturday?" do
82
+ it
83
+ end
84
+
85
+ describe "#sunday?" do
86
+ it
87
+ end
88
+
89
+ describe "#weekday?" do
90
+ it
91
+ end
92
+
93
+ describe "#weekend?" do
94
+ it
95
+ end
96
+
97
+ describe "#daylight_saving?" do
98
+ it
99
+ end
100
+
101
+ end
@@ -0,0 +1,11 @@
1
+ require './spec/spec_helper'
2
+
3
+ # https://github.com/mojombo/chronic/blob/master/test/test_parsing.rb
4
+
5
+ describe Vremya do
6
+
7
+ it "parses dates with Russian locale" do
8
+ #Vremya.parse("01:15, 29 августа 2014", "%H:%M, %d %B %Y", "Europe/Moscow").to_i.must_equal 1409260500
9
+ end
10
+
11
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/vremya'
@@ -0,0 +1,145 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Vremya do
4
+
5
+ let(:gmt) do
6
+ Vremya.new(2014, 10, 26, 15, 10, 15, "GMT")
7
+ end
8
+
9
+ let(:moscow) do
10
+ Vremya.new(2014, 10, 26, 18, 10, 15, "Europe/Moscow")
11
+ end
12
+
13
+ describe "#change" do
14
+ it "supports :year option" do
15
+ gmt.change(:year => 2013).to_s.must_be :==, "2013-10-26 15:10:15 +00:00"
16
+ end
17
+ it "supports :month option" do
18
+ gmt.change(:month => 1).to_s.must_be :==, "2014-01-26 15:10:15 +00:00"
19
+ end
20
+ it "supports :day option" do
21
+ gmt.change(:day => 1).to_s.must_be :==, "2014-10-01 15:10:15 +00:00"
22
+ end
23
+ it "supports :hour option" do
24
+ gmt.change(:hour => 1).to_s.must_be :==, "2014-10-26 01:10:15 +00:00"
25
+ end
26
+ it "supports :minute option" do
27
+ gmt.change(:minute => 1).to_s.must_be :==, "2014-10-26 15:01:15 +00:00"
28
+ end
29
+ it "supports :second option" do
30
+ gmt.change(:second => 1).to_s.must_be :==, "2014-10-26 15:10:01 +00:00"
31
+ end
32
+ it "changes offset automatically" do
33
+ moscow.change(:day => 25).to_s.must_be :==, "2014-10-25 18:10:15 +04:00"
34
+ end
35
+ end
36
+
37
+ describe "#advance" do
38
+ it "supports :years option" do
39
+ gmt.advance(:years => 1).to_s.must_be :==, "2015-10-26 15:10:15 +00:00"
40
+ end
41
+ it "supports :months option" do
42
+ gmt.advance(:months => 1).to_s.must_be :==, "2014-11-26 15:10:15 +00:00"
43
+ end
44
+ it "supports :days option" do
45
+ gmt.advance(:days => 1).to_s.must_be :==, "2014-10-27 15:10:15 +00:00"
46
+ end
47
+ it "supports :hours option" do
48
+ gmt.advance(:hours => 1).to_s.must_be :==, "2014-10-26 16:10:15 +00:00"
49
+ end
50
+ it "supports :minutes option" do
51
+ gmt.advance(:minutes => 1).to_s.must_be :==, "2014-10-26 15:11:15 +00:00"
52
+ end
53
+ it "supports :seconds option" do
54
+ gmt.advance(:seconds => 1).to_s.must_be :==, "2014-10-26 15:10:16 +00:00"
55
+ end
56
+ it "changes offset automatically" do
57
+ moscow.advance(:days => -1).to_s.must_be :==, "2014-10-25 18:10:15 +04:00"
58
+ end
59
+ end
60
+
61
+ describe "#previous_month" do
62
+ it "returns same day and time last month" do
63
+ gmt.previous_month.to_s.must_be :==, "2014-09-26 15:10:15 +00:00"
64
+ end
65
+ end
66
+
67
+ describe "#previous_year" do
68
+ it "returns same day and time last year" do
69
+ gmt.previous_year.to_s.must_be :==, "2013-10-26 15:10:15 +00:00"
70
+ end
71
+ end
72
+
73
+ describe "#previous_day" do
74
+ it "returns same time yesterday" do
75
+ gmt.previous_day.to_s.must_be :==, "2014-10-25 15:10:15 +00:00"
76
+ end
77
+ end
78
+
79
+ describe "#next_year" do
80
+ it "returns same day and time next year" do
81
+ gmt.next_year.to_s.must_be :==, "2015-10-26 15:10:15 +00:00"
82
+ end
83
+ end
84
+
85
+ describe "#next_month" do
86
+ it "returns same day and time next month" do
87
+ gmt.next_month.to_s.must_be :==, "2014-11-26 15:10:15 +00:00"
88
+ end
89
+ end
90
+
91
+ describe "#next_day" do
92
+ it "returns same time tomorrow" do
93
+ gmt.next_day.to_s.must_be :==, "2014-10-27 15:10:15 +00:00"
94
+ end
95
+ end
96
+
97
+ describe "#beginning_of_day" do
98
+ it "returns midnight time same day" do
99
+ gmt.beginning_of_day.to_s.must_be :==, "2014-10-26 00:00:00 +00:00"
100
+ end
101
+ end
102
+
103
+ describe "#beginning_of_week" do
104
+ it "returns midnight on Monday the same week" do
105
+ gmt.beginning_of_week.to_s.must_be :==, "2014-10-20 00:00:00 +00:00"
106
+ end
107
+ end
108
+
109
+ describe "#beginning_of_month" do
110
+ it "returns midnight on the first day of the month" do
111
+ gmt.beginning_of_month.to_s.must_be :==, "2014-10-01 00:00:00 +00:00"
112
+ end
113
+ end
114
+
115
+ describe "#beginning_of_year" do
116
+ it "returns midnight on the first of January" do
117
+ gmt.beginning_of_year.to_s.must_be :==, "2014-01-01 00:00:00 +00:00"
118
+ end
119
+ end
120
+
121
+ describe "#difference_with_zone" do
122
+ it "returns current time difference in seconds with specified timezone" do
123
+ gmt.difference_with_zone("Europe/Moscow").must_be :==, 3600 * 3
124
+ end
125
+ end
126
+
127
+ describe "#as_if_in" do
128
+ it "treats HH:MM as if it was local time in specified timezone" do
129
+ gmt.as_if_in("Europe/Moscow").to_s.must_be :==, "2014-10-26 15:10:15 +03:00"
130
+ end
131
+ end
132
+
133
+ describe "#+" do
134
+ it "advances in time by specified number of seconds" do
135
+ (gmt + 1).to_s.must_be :==, "2014-10-26 15:10:16 +00:00"
136
+ end
137
+ end
138
+
139
+ describe "#-" do
140
+ it "goes back in time by specified number of seconds" do
141
+ (gmt - 1).to_s.must_be :==, "2014-10-26 15:10:14 +00:00"
142
+ end
143
+ end
144
+
145
+ end
data/spec/zone_spec.rb ADDED
@@ -0,0 +1,50 @@
1
+ require './spec/spec_helper'
2
+
3
+ describe Vremya::Timezone do
4
+
5
+ describe ".at" do
6
+ it "returns historical offset for valid timezones [1/2]" do
7
+ Vremya::Timezone.at("Europe/Moscow", 2014, 10, 26, 0, 0, 0).offset.must_be :==, 3600 * 4
8
+ end
9
+ it "returns historical offset for valid timezones [2/2]" do
10
+ Vremya::Timezone.at("Europe/Moscow", 2014, 10, 26, 1, 0, 0).offset.must_be :==, 3600 * 3
11
+ end
12
+ end
13
+
14
+ describe "#name" do
15
+ it "returns nil for Numeric values" do
16
+ Vremya::Timezone.new(3600).name.must_be_nil
17
+ end
18
+ it "returns nil for String values" do
19
+ Vremya::Timezone.new("+01:00").name.must_be_nil
20
+ end
21
+ it "returns name when valid timezone if specified" do
22
+ Vremya::Timezone.new("Europe/Moscow").name.must_be :==, "Europe/Moscow"
23
+ end
24
+ end
25
+
26
+ describe "#offset" do
27
+ it "returns offset in seconds for Numeric values" do
28
+ Vremya::Timezone.new(3600).offset.must_be :==, 3600
29
+ end
30
+ it "returns offset in seconds for String values" do
31
+ Vremya::Timezone.new("+01:00").offset.must_be :==, 3600
32
+ end
33
+ it "returns current offset in seconds for valid timezones" do
34
+ Vremya::Timezone.new("Europe/Moscow").offset.must_be :==, 3600 * 3
35
+ end
36
+ end
37
+
38
+ describe "#to_s" do
39
+ it "returns formatted offset for Numeric values" do
40
+ Vremya::Timezone.new(3600).to_s.must_be :==, "+01:00"
41
+ end
42
+ it "returns formatted offset for String values" do
43
+ Vremya::Timezone.new("+01:00").to_s.must_be :==, "+01:00"
44
+ end
45
+ it "returns current offset for valid timezones" do
46
+ Vremya::Timezone.new("Europe/Moscow").to_s.must_be :==, "+03:00"
47
+ end
48
+ end
49
+
50
+ end
metadata CHANGED
@@ -1,102 +1,123 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: vremya
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- version: "1.0"
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
9
5
  platform: ruby
10
- authors:
6
+ authors:
11
7
  - Alex Serebryakov
12
8
  autorequire:
13
9
  bindir: bin
14
10
  cert_chain: []
15
-
16
- date: 2013-10-08 00:00:00 +04:00
17
- default_executable:
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yard
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
20
  type: :development
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- segments:
26
- - 0
27
- version: "0"
28
- name: rspec
29
- requirement: *id001
30
21
  prerelease: false
31
- - !ruby/object:Gem::Dependency
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
32
34
  type: :development
33
- version_requirements: &id002 !ruby/object:Gem::Requirement
34
- requirements:
35
- - - ">="
36
- - !ruby/object:Gem::Version
37
- segments:
38
- - 0
39
- version: "0"
40
- name: yard
41
- requirement: *id002
42
35
  prerelease: false
43
- - !ruby/object:Gem::Dependency
44
- type: :runtime
45
- version_requirements: &id003 !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ~>
48
- - !ruby/object:Gem::Version
49
- segments:
50
- - 0
51
- - 3
52
- - 37
53
- version: 0.3.37
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
54
56
  name: tzinfo
55
- requirement: *id003
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
56
63
  prerelease: false
57
- description: "A lump of syntactic sugar around native Date and Time objects, that takes the pain out of timezones, traversal, formatting etc. "
58
- email:
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: 'A lump of syntactic sugar around native Date and Time objects, that
70
+ takes the pain out of timezones, traversal, formatting etc. '
71
+ email:
59
72
  - alex@merimond.com
60
73
  executables: []
61
-
62
74
  extensions: []
63
-
64
75
  extra_rdoc_files: []
65
-
66
- files:
76
+ files:
77
+ - lib/vremya/locale/ru.rb
78
+ - lib/vremya/timezone.rb
67
79
  - lib/vremya.rb
68
80
  - Rakefile
69
81
  - README.md
70
82
  - LICENSE
71
- has_rdoc: true
83
+ - spec/attributes_spec.rb
84
+ - spec/conversion_spec.rb
85
+ - spec/formatting_spec.rb
86
+ - spec/helpers_spec.rb
87
+ - spec/parse_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/traversal_spec.rb
90
+ - spec/zone_spec.rb
72
91
  homepage: https://github.com/merimond/vremya
73
92
  licenses: []
74
-
93
+ metadata: {}
75
94
  post_install_message:
76
95
  rdoc_options: []
77
-
78
- require_paths:
96
+ require_paths:
79
97
  - lib
80
- required_ruby_version: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- segments:
85
- - 0
86
- version: "0"
87
- required_rubygems_version: !ruby/object:Gem::Requirement
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- segments:
92
- - 0
93
- version: "0"
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
94
108
  requirements: []
95
-
96
109
  rubyforge_project: vremya
97
- rubygems_version: 1.3.6
110
+ rubygems_version: 2.0.14
98
111
  signing_key:
99
- specification_version: 3
112
+ specification_version: 4
100
113
  summary: Time & date library for Ruby
101
- test_files: []
102
-
114
+ test_files:
115
+ - spec/attributes_spec.rb
116
+ - spec/conversion_spec.rb
117
+ - spec/formatting_spec.rb
118
+ - spec/helpers_spec.rb
119
+ - spec/parse_spec.rb
120
+ - spec/spec_helper.rb
121
+ - spec/traversal_spec.rb
122
+ - spec/zone_spec.rb
123
+ has_rdoc: