class AdManagerApi::AdManagerDateTime
Attributes
Public Class Methods
Create a new AdManagerDateTime
, a utility class that allows for interoperability between the Ad Manager API's DateTime objects and ruby's Time class. The last argument must be a valid timezone identifier, e.g. “America/New_York”.
Args:
- args: - ([year, [month, [day, [hour, [minute, [second,]]]]]] timezone) - (time, timezone), a native Time object and a timezone identifier - (ad_manager_datetime), an Ad Manager DateTime hash representation
Returns:
- ad_manager_datetime: an instance of AdManagerDateTime
# File lib/ad_manager_api/ad_manager_api_datetime.rb, line 99 def initialize(api, *args) @api = api @api.utils_reporter.ad_manager_date_time_used() # Handle special cases when an Ad Manager DateTime hash or ruby Time # instance are passed as the first argument to the constructor. case args.first when Hash hash = args.first datetime_args = [hash[:date][:year], hash[:date][:month], hash[:date][:day], hash[:hour], hash[:minute], hash[:second], hash[:time_zone_id]] when AdManagerDateTime, Time, DateTime time = args.first datetime_args = [args.last] [:sec, :min, :hour, :day, :month, :year].each do |duration| datetime_args.unshift(time.send(duration)) end else datetime_args = args end # Check the validity of the timezone parameter, which is required. if not TZInfo::Timezone.all_identifiers.include?(datetime_args.last) raise "Last argument to AdManagerDateTime constructor must be valid" + "timezone" end # Set timezone attribute and pass its utc offset into the Time # constructor. @timezone = TZInfo::Timezone.get(datetime_args.pop) @time = Time.new(*datetime_args, utc_offset=@timezone.current_period.utc_offset) end
Create an AdManagerDateTime
for the current time in the specified timezone.
Args:
- timezone: a valid timezone identifier, e.g. "America/New_York"
Returns:
- ad_manager_datetime: an instance of AdManagerDateTime
# File lib/ad_manager_api/ad_manager_api_datetime.rb, line 140 def self.now(api, timezone) new(api, TZInfo::Timezone.get(timezone).now, timezone) end
Create an AdManagerDateTime
in the “UTC” timezone. Calls the AdManagerDateTime
contstructor with timezone identifier “UTC”.
Args:
- ([year, [month, [day, [hour, [minute, [second]]]]]])
Returns:
- ad_manager_datetime: an instance of AdManagerDateTime
# File lib/ad_manager_api/ad_manager_api_datetime.rb, line 152 def self.utc(api, *args) new(api, *args + ['UTC']) end
Public Instance Methods
When an unrecognized method is applied to AdManagerDateTime
, pass it through to the internal ruby Time.
# File lib/ad_manager_api/ad_manager_api_datetime.rb, line 183 def method_missing(name, *args, &block) # Restrict time zone related functions from being passed to internal ruby # Time attribute, since AdManagerDateTime handles timezones its own way. restricted_functions = [:dst?, :getgm, :getlocal, :getutc, :gmt, :gmtime, :gmtoff, :isdst, :localtime, :utc] if restricted_functions.include? name raise NoMethodError, 'undefined method %s for %s' % [name, self] end result = @time.send(name, *args, &block) if result.is_a? Time return self.class.new(@api, result, @timezone.identifier) else return result end end
Convert AdManagerDateTime
into a hash representation which can be consumed by the Ad Manager API. E.g., a hash that can be passed as PQL DateTime variables.
Returns:
- ad_manager_datetime_hash: a hash representation of an AdManagerDateTime
# File lib/ad_manager_api/ad_manager_api_datetime.rb, line 163 def to_h { :date => AdManagerApi::AdManagerDate.new( @api, @time.year, @time.month, @time.day ).to_h, :hour => @time.hour, :minute => @time.min, :second => @time.sec, :time_zone_id => @timezone.identifier } end
Convert AdManagerDateTime
into a native ruby Time object.
# File lib/ad_manager_api/ad_manager_api_datetime.rb, line 176 def to_time return Time.new(@time.year, @time.month, @time.day, @time.hour, @time.min, @time.sec, @timezone.current_period.utc_offset) end