class DateNamedFile::Template
A Template
is a model of a filename with a (restricted but) valid strftime formatting template embedded within.
Basically, you can use
-
%Y four=digit year
-
%m two-digit month
-
%d two-digit day
-
%H two-digit hour 00-23
-
%M minute
-
%S second
-
%s unix epoch time to the second
-
%Q unix epoch time to the millisecond
-
%1 arbitrary integer (for, e.g., log files. Not part of strftime)
Examples:
-
daily_update_%Y-%m-%d.txt
-
mydaemon_%Y_%m_%d_%H%M.log
-
updates%Y%m%d_dev.ndj.gz
-
mylog%s.log
In all cases date/time parts must be in order (year, month, day, hour, minute, second).
NO support for mixing unix epoch with anything else. Why would you do that?
Constants
- SUBSTITUTION_REGEXP
Attributes
@return [Regexp] A regular expression that does its best to correctly match
filenames that follow the template. Also used to try to extract the embedded date in a filename
@return [String] the initial template string
Public Class Methods
@param [String] template Template
string with embedded strftime format string @example
tmpl = DateNamedFile::Template.new("mystuff_daily%Y%m%d.tsv")
# File lib/date_named_file/template.rb, line 54 def initialize(template_string) @template_string = template_string @matcher = template_matcher(template_string) end
Public Instance Methods
Get a DateNamedFile::File for the given date/datetime @param [<anything date_ish>] date_ish (see forgiving_dateify
) @return [DateNamedFile::File] DateNamedFile::File for the given date in this template
# File lib/date_named_file/template.rb, line 85 def at(date_ish) DatedFile.new(self, date_ish) end
Like daily_since
, but don't include the start date @see daily_since
# File lib/date_named_file/template.rb, line 127 def daily_after(date_ish) daily_since(date_ish)[1..-1] end
Get a list of computed files based on all the dates from the given start through today, _including both ends_. @param [<anything date_ish>] start_date_ish (see forgiving_dateify
) @return [DateNamedFile::File] for tomorrow (+24 hours)
# File lib/date_named_file/template.rb, line 110 def daily_since(start_date_ish) dt = forgiving_dateify(start_date_ish) if dt.to_date > DateTime.now.to_date [] else daily_since(dt + 1).unshift(self.at(dt)) end end
Like daily_since
, but don't include today @see daily_since
# File lib/date_named_file/template.rb, line 121 def daily_through_yesterday(date_ish) daily_since(date_ish)[0..-2] end
Compute the filename from plugging the given date_ish string/integer into the template @param [<anything date_ish>] date_ish (see forgiving_dateify
) @return [String] the expanded filename
# File lib/date_named_file/template.rb, line 78 def filename_for(date_ish) forgiving_dateify(date_ish).strftime(template_string) end
# File lib/date_named_file/template.rb, line 70 def in_dir(dir) DateNamedFile::Directory.new(self, dir) end
Test to see if a filename matches the template @param [String] filename The string to test @return [Boolean]
# File lib/date_named_file/template.rb, line 63 def match?(filename) @matcher.match? filename end
@return [DateNamedFile::File] DateNamedFile::File for today/right now
# File lib/date_named_file/template.rb, line 90 def now at DateTime.now end
# File lib/date_named_file/template.rb, line 131 def template_matcher(template) regexp_string = SUBSTITUTION_REGEXP.each_with_object(Regexp.escape(template)) do |subpair, templ| pct, rxstring = *subpair templ.gsub!(pct, rxstring) end Regexp.new(regexp_string) end
@return [DateNamedFile::File] DateNamedFile::File for tomorrow (+24 hours)
# File lib/date_named_file/template.rb, line 97 def tomorrow at (DateTime.now + 1) end
@return [DateNamedFile::File] DateNamedFile::File for yesterday (-24 hours)
# File lib/date_named_file/template.rb, line 102 def yesterday at (DateTime.now - 1) end