Method: TZInfo::Timezone#local_datetime
- Defined in:
- lib/tzinfo/timezone.rb
#local_datetime(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, dst = Timezone.default_dst) {|periods| ... } ⇒ DateTimeWithOffset
Creates a DateTime object based on the given (Gregorian calendar) date
and time parameters. The parameters are interpreted as a local time in the
time zone. The result has the appropriate offset and
timezone_offset.
Warning: There are time values that are not valid as local times in a time zone (for example, during the transition from standard time to daylight savings time). There are also time values that are ambiguous, occurring more than once with different offsets to UTC (for example, during the transition from daylight savings time to standard time).
In the first case (an invalid local time), a PeriodNotFound exception will be raised.
In the second case (more than one occurrence), an AmbiguousTime
exception will be raised unless the optional dst parameter or block
handles the ambiguity.
If the ambiguity is due to a transition from daylight savings time to
standard time, the dst parameter can be used to select whether the
daylight savings time or local time is used. For example, the following
code would raise an AmbiguousTime exception:
tz = TZInfo::Timezone.get('America/New_York')
tz.local_datetime(2004,10,31,1,30,0,0)
Specifying dst = true would return a Time with a UTC offset of -4
hours and abbreviation EDT (Eastern Daylight Time). Specifying dst =
false would return a Time with a UTC offset of -5 hours and
abbreviation EST (Eastern Standard Time).
The dst parameter will not be able to resolve an ambiguity resulting
from the clocks being set back without changing from daylight savings time
to standard time. In this case, if a block is specified, it will be called
to resolve the ambiguity. The block must take a single parameter - an
Array of TZInfo::TimezonePeriods that need to be resolved. The block can
select and return a single TZInfo::TimezonePeriod or return nil or an empty
Array to cause an AmbiguousTime exception to be raised.
The default value of the dst parameter can be specified using
default_dst=.
values, interpreted as a local time in the time zone.
831 832 833 |
# File 'lib/tzinfo/timezone.rb', line 831 def local_datetime(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, sub_second = 0, dst = Timezone.default_dst, &block) (year, month, day, hour, minute, second, sub_second, dst, &block).to_datetime end |