syc-task 0.3.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  require 'time'
2
+ require 'syctimeleap/time_leap'
2
3
 
3
4
  # Functions for time operations
4
5
  module Syctime
5
-
6
6
  # Translates seconds to years, months, weeks, days, hours, minutes and seconds
7
7
  # The return value is an array [seconds,...,years]
8
8
  def seconds_to_time(seconds)
@@ -20,9 +20,9 @@ module Syctime
20
20
  # Translates seconds into a time string like 1 year 2 weeks 5 days 10 minutes.
21
21
  def string_for_seconds(seconds)
22
22
  time = seconds_to_time(seconds)
23
- time_name = ['year','month','week','day','hour','minute','second']
24
- time_string = ""
25
- time.reverse.each_with_index do |part,index|
23
+ time_name = %w[year month week day hour minute second]
24
+ time_string = ''
25
+ time.reverse.each_with_index do |part, index|
26
26
  time_string << part.to_s + ' ' + time_name[index] + ' ' if part == 1
27
27
  time_string << part.to_s + ' ' + time_name[index] + 's ' if part > 1
28
28
  end
@@ -34,29 +34,30 @@ module Syctime
34
34
  def separated_time_string(seconds, separator)
35
35
  secs = seconds % 60
36
36
  mins = seconds / 60 % 60
37
- hours = seconds / 60 / 60
38
- time_string = sprintf("%02d#{separator}%02d#{separator}%02d", hours, mins, secs)
37
+ hours = seconds / 60 / 60
38
+ format("%02d#{separator}%02d#{separator}%02d", hours, mins, secs)
39
39
  end
40
40
 
41
41
  # Translates a time in the ISO 8601 schema to a time object.
42
42
  # 2013-04-09 21:45 -200
43
43
  def time_for_string(time)
44
- time = time.scan(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)[0].sub(' ','T')
44
+ time = time.scan(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)[0].sub(' ', 'T')
45
45
  Time.xmlschema(time)
46
46
  end
47
-
47
+
48
48
  # Tests whether the date is between from and to. Returns true then otherwise
49
49
  # false. Time, from and to are Time objects as retrieved from Time.now or
50
50
  # Time.local(2013,"apr",13,10,50,0). Alternatively time strings can be
51
51
  # provided in the form of "2013-04-13".
52
52
  def date_between?(date, from, to)
53
- date = date.strftime("%Y-%m-%d") if date.class == Time || date.class == Date
54
- from = from.strftime("%Y-%m-%d") if from.class == Time || from.class == Date
55
- to = to.strftime("%Y-%m-%d") if to.class == Time || to.class == Date
53
+ date = date.strftime('%Y-%m-%d') if date.class == Time || date.class == Date
54
+ from = from.strftime('%Y-%m-%d') if from.class == Time || from.class == Date
55
+ to = to.strftime('%Y-%m-%d') if to.class == Time || to.class == Date
56
56
  time_pattern = /\d{4}-\d{2}-\d{2}/
57
57
  raise ArgumentError if date.scan(time_pattern).empty?
58
58
  raise ArgumentError if from.scan(time_pattern).empty?
59
59
  raise ArgumentError if to.scan(time_pattern).empty?
60
+
60
61
  date >= from && date <= to
61
62
  end
62
63
 
@@ -66,4 +67,54 @@ module Syctime
66
67
  time >= from && time <= to
67
68
  end
68
69
 
70
+ # Checks whether the date is a valid date in the form of yyyy-mm-dd. If it's
71
+ # no valid date false is returned otherwise true
72
+ def valid_date?(date_string)
73
+ if date_string.match(/\d{4}-\d{2}-\d{2}/)
74
+ begin
75
+ Date.parse(date_string)
76
+ true
77
+ rescue ArgumentError
78
+ false
79
+ end
80
+ else
81
+ begin
82
+ SycTimeleap::TimeLeap.new.send(date_string)
83
+ true
84
+ rescue StandardError
85
+ false
86
+ end
87
+ end
88
+ end
89
+
90
+ # Extracts the time out of a time string. Accepts 'today', 'tomorrow',
91
+ # 'yesterday' or a date in the form 'YYYY-MM-DD'. Returns the date contained
92
+ # in the time_string or if time = true in a Time object
93
+ def extract_time(time_string, time = false)
94
+ time_string = 'today' if time_string.nil?
95
+
96
+ if time_string.match(/\d{4}-\d{2}-\d{2}/)
97
+ date = time_string
98
+ date = Time.xmlschema("#{time_string}T00:00:00") if time
99
+ else
100
+ timeleap = SycTimeleap::TimeLeap.new
101
+ begin
102
+ date = timeleap.send(time_string)
103
+ date = date.to_s unless time
104
+ rescue NoMethodError
105
+ help_now! "Arguments may be 'time distances', YYYY-MM-DD or <RETURN>\n" +
106
+ "\ntime distances are:\n" +
107
+ "* yesterday|today|tomorrow\n" +
108
+ "* next|previous_monday|tuesday|...|sunday\n" +
109
+ "* in|back_10_days|weeks|months|years\n" +
110
+ "* monday|tuesday|...|sunday_in|back_1_day|week|month|year\n" +
111
+ "Short forms are also possible:\n" +
112
+ "* y|tod|tom\n" +
113
+ "* n|pmo|tu|we|th|fr|sa|su\n" +
114
+ "* i|b10d|w|m|y\n" +
115
+ '* mo|tu|we|th|fr|sa|sui|b1d|w|m|y'
116
+ end
117
+ end
118
+ date
119
+ end
69
120
  end
@@ -14,7 +14,6 @@ require 'rainbow'
14
14
  # To stop the timer the semaphore has to be deleted
15
15
  # FileUtils.rm semaphore
16
16
  class ConsoleTimer
17
-
18
17
  # Create a new ConsoleTimer with the time to count down, the task's ID and a
19
18
  # semaphore. The semaphore is a file named id.track where id is equal to the
20
19
  # provided id. The semaphore is checked for existence. If the semaphore is
@@ -32,7 +31,7 @@ class ConsoleTimer
32
31
  while track
33
32
  sleep 1
34
33
  output
35
- track = File.exists? @semaphore
34
+ track = File.exist? @semaphore
36
35
  end
37
36
  exit 0
38
37
  end
@@ -49,16 +48,15 @@ class ConsoleTimer
49
48
  end
50
49
  seconds = difference % 60
51
50
  minutes = difference / 60 % 60
52
- hours = difference / 60 / 60 % 60
53
- count_down = sprintf("%d: %02d:%02d:%02d", @id, hours, minutes, seconds)
51
+ hours = difference / 60 / 60 % 60
52
+ count_down = format('%d: %02d:%02d:%02d', @id, hours, minutes, seconds)
54
53
  size = count_down.size
55
54
  count_down = count_down.color(color)
56
- command = "tput sc;"+
57
- "tput cup 0 $(($(tput cols) - #{size}));"+
55
+ command = 'tput sc;' +
56
+ "tput cup 0 $(($(tput cols) - #{size}));" +
58
57
  "echo #{count_down};tput rc"
59
58
  system command
60
59
  end
61
-
62
60
  end
63
61
 
64
62
  # Expects to receive parameters duration, id and semaphore
@@ -71,5 +69,3 @@ if ARGV.size == 3
71
69
  else
72
70
  exit -1
73
71
  end
74
-
75
-