1DBIx::Class::InflateColUusmenr::CDoantterTiibmuet(e3d)PDeBrIlx:D:oCcluamsesn:t:aItnifolnateColumn::DateTime(3)
2
3
4
6 DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects
7 from date and datetime columns.
8
10 Load this component and then declare one or more columns to be of the
11 datetime, timestamp or date datatype.
12
13 package Event;
14 use base 'DBIx::Class::Core';
15
16 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
17 __PACKAGE__->add_columns(
18 starts_when => { data_type => 'datetime' }
19 create_date => { data_type => 'date' }
20 );
21
22 Then you can treat the specified column as a DateTime object.
23
24 print "This event starts the month of ".
25 $event->starts_when->month_name();
26
27 If you want to set a specific timezone and locale for that field, use:
28
29 __PACKAGE__->add_columns(
30 starts_when => { data_type => 'datetime', timezone => "America/Chicago", locale => "de_DE" }
31 );
32
33 If you want to inflate no matter what data_type your column is, use
34 inflate_datetime or inflate_date:
35
36 __PACKAGE__->add_columns(
37 starts_when => { data_type => 'varchar', inflate_datetime => 1 }
38 );
39
40 __PACKAGE__->add_columns(
41 starts_when => { data_type => 'varchar', inflate_date => 1 }
42 );
43
44 It's also possible to explicitly skip inflation:
45
46 __PACKAGE__->add_columns(
47 starts_when => { data_type => 'datetime', inflate_datetime => 0 }
48 );
49
50 NOTE: Don't rely on "InflateColumn::DateTime" to parse date strings for
51 you. The column is set directly for any non-references and
52 "InflateColumn::DateTime" is completely bypassed. Instead, use an
53 input parser to create a DateTime object. For instance, if your user
54 input comes as a 'YYYY-MM-DD' string, you can use
55 "DateTime::Format::ISO8601" thusly:
56
57 use DateTime::Format::ISO8601;
58 my $dt = DateTime::Format::ISO8601->parse_datetime('YYYY-MM-DD');
59
61 This module figures out the type of DateTime::Format::* class to
62 inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
63 that you are using. If you switch from one database to a different one
64 your code should continue to work without modification (though note
65 that this feature is new as of 0.07, so it may not be perfect yet - bug
66 reports to the list very much welcome).
67
68 If the data_type of a field is "date", "datetime" or "timestamp" (or a
69 derivative of these datatypes, e.g. "timestamp with timezone"), this
70 module will automatically call the appropriate parse/format method for
71 deflation/inflation as defined in the storage class. For instance, for
72 a "datetime" field the methods "parse_datetime" and "format_datetime"
73 would be called on deflation/inflation. If the storage class does not
74 provide a specialized inflator/deflator, "[parse|format]_datetime" will
75 be used as a fallback. See "Formatters And Stringification" in DateTime
76 for more information on date formatting.
77
78 For more help with using components, see "USING" in
79 DBIx::Class::Manual::Component.
80
81 register_column
82 Chains with the "register_column" in DBIx::Class::Row method, and sets
83 up datetime columns appropriately. This would not normally be directly
84 called by end users.
85
86 In the case of an invalid date, DateTime will throw an exception. To
87 bypass these exceptions and just have the inflation return undef, use
88 the "datetime_undef_if_invalid" option in the column info:
89
90 "broken_date",
91 {
92 data_type => "datetime",
93 default_value => '0000-00-00',
94 is_nullable => 1,
95 datetime_undef_if_invalid => 1
96 }
97
99 If you have a datetime column with an associated "timezone", and
100 subsequently create/update this column with a DateTime object in the
101 DateTime::TimeZone::Floating timezone, you will get a warning (as there
102 is a very good chance this will not have the result you expect). For
103 example:
104
105 __PACKAGE__->add_columns(
106 starts_when => { data_type => 'datetime', timezone => "America/Chicago" }
107 );
108
109 my $event = $schema->resultset('EventTZ')->create({
110 starts_at => DateTime->new(year=>2007, month=>12, day=>31, ),
111 });
112
113 The warning can be avoided in several ways:
114
115 Fix your broken code
116 When calling "set_time_zone" on a Floating DateTime object, the
117 timezone is simply set to the requested value, and no time
118 conversion takes place. It is always a good idea to be supply
119 explicit times to the database:
120
121 my $event = $schema->resultset('EventTZ')->create({
122 starts_at => DateTime->new(year=>2007, month=>12, day=>31, time_zone => "America/Chicago" ),
123 });
124
125 Suppress the check on per-column basis
126 __PACKAGE__->add_columns(
127 starts_when => { data_type => 'datetime', timezone => "America/Chicago", floating_tz_ok => 1 }
128 );
129
130 Suppress the check globally
131 Set the environment variable DBIC_FLOATING_TZ_OK to some true
132 value.
133
134 Putting extra attributes like timezone, locale or floating_tz_ok into
135 extra => {} has been DEPRECATED because this gets you into trouble
136 using DBIx::Class::Schema::Versioned. Instead put it directly into the
137 columns definition like in the examples above. If you still use the old
138 way you'll see a warning - please fix your code then!
139
141 More information about the add_columns method, and column metadata, can
142 be found in the documentation for DBIx::Class::ResultSource.
143 Further discussion of problems inherent to the Floating timezone:
144 Floating DateTimes and $dt->set_time_zone
145
147 Check the list of additional DBIC resources.
148
150 This module is free software copyright by the DBIx::Class (DBIC)
151 authors. You can redistribute it and/or modify it under the same terms
152 as the DBIx::Class library.
153
154
155
156perl v5.36.0 2022-0D7B-I2x2::Class::InflateColumn::DateTime(3)