Module: Syctime

Defined in:
lib/syctime/time_util.rb

Overview

Functions for time operations

Instance Method Summary collapse

Instance Method Details

#date_between?(date, from, to) ⇒ Boolean

Tests whether the date is between from and to. Returns true then otherwise false. Time, from and to are Time objects as retrieved from Time.now or Time.local(2013,“apr”,13,10,50,0). Alternatively time strings can be provided in the form of “2013-04-13”.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/syctime/time_util.rb', line 52

def date_between?(date, from, to)
  date = date.strftime('%Y-%m-%d') if date.class == Time || date.class == Date
  from = from.strftime('%Y-%m-%d') if from.class == Time || from.class == Date
  to = to.strftime('%Y-%m-%d') if to.class == Time || to.class == Date
  time_pattern = /\d{4}-\d{2}-\d{2}/
  raise ArgumentError if date.scan(time_pattern).empty?
  raise ArgumentError if from.scan(time_pattern).empty?
  raise ArgumentError if to.scan(time_pattern).empty?

  date >= from && date <= to
end

#extract_time(time_string, time = false) ⇒ Object

Extracts the time out of a time string. Accepts ‘today’, ‘tomorrow’, ‘yesterday’ or a date in the form ‘YYYY-MM-DD’. Returns the date contained in the time_string or if time = true in a Time object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/syctime/time_util.rb', line 93

def extract_time(time_string, time = false)
  time_string = 'today' if time_string.nil?

  if time_string.match(/\d{4}-\d{2}-\d{2}/)
    date = time_string
    date = Time.xmlschema("#{time_string}T00:00:00") if time
  else
    timeleap = SycTimeleap::TimeLeap.new
    begin
      date = timeleap.send(time_string)
      date = date.to_s unless time
    rescue NoMethodError
      help_now! "Arguments may be 'time distances', YYYY-MM-DD or <RETURN>\n" +
                "\ntime distances are:\n" +
                "* yesterday|today|tomorrow\n" +
                "* next|previous_monday|tuesday|...|sunday\n" +
                "* in|back_10_days|weeks|months|years\n" +
                "* monday|tuesday|...|sunday_in|back_1_day|week|month|year\n" +
                "Short forms are also possible:\n" +
                "* y|tod|tom\n" +
                "* n|pmo|tu|we|th|fr|sa|su\n" +
                "* i|b10d|w|m|y\n" +
                '* mo|tu|we|th|fr|sa|sui|b1d|w|m|y'
    end
  end
  date
end

#seconds_to_time(seconds) ⇒ Object

Translates seconds to years, months, weeks, days, hours, minutes and seconds The return value is an array [seconds,…,years]



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/syctime/time_util.rb', line 8

def seconds_to_time(seconds)
  seconds = seconds.round
  duration = []
  duration << seconds % 60                         # seconds
  duration << seconds / 60 % 60                    # minutes
  duration << seconds / 60 / 60 % 24               # hours
  duration << seconds / 60 / 60 / 24 % 7           # days
  duration << seconds / 60 / 60 / 24 / 7 % 4       # weeks
  duration << seconds / 60 / 60 / 24 / 7 / 4 % 12  # months
  duration << seconds / 60 / 60 / 24 / 7 / 4 / 12  # years
end

#separated_time_string(seconds, separator) ⇒ Object

Creates a time string separating hours, minutes and seconds with the provided separator like 12:50:33



34
35
36
37
38
39
# File 'lib/syctime/time_util.rb', line 34

def separated_time_string(seconds, separator)
  secs  = seconds % 60
  mins  = seconds / 60 % 60
  hours = seconds / 60 / 60
  format("%02d#{separator}%02d#{separator}%02d", hours, mins, secs)
end

#string_for_seconds(seconds) ⇒ Object

Translates seconds into a time string like 1 year 2 weeks 5 days 10 minutes.



21
22
23
24
25
26
27
28
29
30
# File 'lib/syctime/time_util.rb', line 21

def string_for_seconds(seconds)
  time = seconds_to_time(seconds)
  time_name = %w[year month week day hour minute second]
  time_string = ''
  time.reverse.each_with_index do |part, index|
    time_string << part.to_s + ' ' + time_name[index] + ' ' if part == 1
    time_string << part.to_s + ' ' + time_name[index] + 's ' if part > 1
  end
  time_string
end

#time_between?(time, from, to) ⇒ Boolean

Checks whether the time is between from and to. Returns true then otherwise false. time, from and to have to be Time objects.

Returns:

  • (Boolean)


66
67
68
# File 'lib/syctime/time_util.rb', line 66

def time_between?(time, from, to)
  time >= from && time <= to
end

#time_for_string(time) ⇒ Object

Translates a time in the ISO 8601 schema to a time object.

2013-04-09 21:45 -200


43
44
45
46
# File 'lib/syctime/time_util.rb', line 43

def time_for_string(time)
  time = time.scan(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)[0].sub(' ', 'T')
  Time.xmlschema(time)
end

#valid_date?(date_string) ⇒ Boolean

Checks whether the date is a valid date in the form of yyyy-mm-dd. If it’s no valid date false is returned otherwise true

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/syctime/time_util.rb', line 72

def valid_date?(date_string)
  if date_string.match(/\d{4}-\d{2}-\d{2}/)
    begin
      Date.parse(date_string)
      true
    rescue ArgumentError
      false
    end
  else
    begin
      SycTimeleap::TimeLeap.new.send(date_string)
      true
    rescue StandardError
      false
    end
  end
end