pub struct Timestamp { /* private fields */ }
Expand description
Oracle-specific Datetime data type
This struct doesn’t have arithmetic methods and they won’t be added to avoid
reinventing the wheel. If you need methods such as adding an interval to a
timestamp, enable chrono
feature and use chrono::Date, chrono::DateTime,
chrono::naive::NaiveDate or chrono::naive::NaiveDateTime instead.
§Formatting Parameters
The following format parameters are supported since 0.7.0.
"{:.n}"
wheren
is 0~9 specifies the number of fractional seconds displayed."{:#}"
formats timestamps in ISO 8601 extended format."{:-#}"
formats timestamps in ISO 8601 basic format.
§Examples
// Create a timestamp.
let ts1 = Timestamp::new(2017, 8, 9, 11, 22, 33, 500000000)?;
// Convert to string.
assert_eq!(ts1.to_string(), "2017-08-09 11:22:33.500000000");
// Create a timestamp with time zone (-8:00).
let ts2 = Timestamp::new(2017, 8, 9, 11, 22, 33, 500000000)?.and_tz_hm_offset(-8, 0)?;
// Convert to string.
assert_eq!(ts2.to_string(), "2017-08-09 11:22:33.500000000 -08:00");
// Create a timestamp with precision
let ts3 = Timestamp::new(2017, 8, 9, 11, 22, 33, 500000000)?.and_prec(3)?;
// The string representation depends on the precision.
assert_eq!(ts3.to_string(), "2017-08-09 11:22:33.500");
// The precision can be specified in format parameter.
assert_eq!(format!("{:.6}", ts3), "2017-08-09 11:22:33.500000");
// ISO 8601 extended format
assert_eq!(format!("{:#}", ts3), "2017-08-09T11:22:33.500");
// ISO 8601 basic format
assert_eq!(format!("{:-#}", ts3), "20170809T112233.500");
// Precisions are ignored when intervals are compared.
assert_eq!(ts1, ts3);
// Create a timestamp from string.
let ts4: Timestamp = "2017-08-09 11:22:33.500 -08:00".parse()?;
// The precision is determined by number of decimal digits in the string.
assert_eq!(ts4.precision(), 3);
Fetch and bind interval values.
// Fetch Timestamp
let sql = "select TIMESTAMP '2017-08-09 11:22:33.500' from dual";
let ts = conn.query_row_as::<Timestamp>(sql, &[])?;
assert_eq!(ts.to_string(), "2017-08-09 11:22:33.500000000");
// Bind Timestamp
let sql = "begin \
:outval := :inval + interval '+1 02:03:04.5' day to second; \
end;";
let mut stmt = conn.statement(sql).build()?;
stmt.execute(&[&OracleType::Timestamp(3), // bind null as timestamp(3)
&ts, // bind the ts variable
])?;
let outval: Timestamp = stmt.bind_value(1)?; // get the first bind value.
// ts + (1 day, 2 hours, 3 minutes and 4.5 seconds)
assert_eq!(outval.to_string(), "2017-08-10 13:25:38.000");
Implementations§
Source§impl Timestamp
impl Timestamp
Sourcepub fn new(
year: i32,
month: u32,
day: u32,
hour: u32,
minute: u32,
second: u32,
nanosecond: u32,
) -> Result<Timestamp>
pub fn new( year: i32, month: u32, day: u32, hour: u32, minute: u32, second: u32, nanosecond: u32, ) -> Result<Timestamp>
Creates a timestamp.
Valid values are:
argument | valid values |
---|---|
year | -4713 to 9999 |
month | 1 to 12 |
day | 1 to 31 |
hour | 0 to 23 |
minute | 0 to 59 |
second | 0 to 59 |
nanosecond | 0 to 999,999,999 |
Sourcepub fn and_tz_offset(&self, offset: i32) -> Result<Timestamp>
pub fn and_tz_offset(&self, offset: i32) -> Result<Timestamp>
Creates a timestamp with time zone.
offset
is time zone offset seconds from UTC.
Sourcepub fn and_tz_hm_offset(
&self,
hour_offset: i32,
minute_offset: i32,
) -> Result<Timestamp>
pub fn and_tz_hm_offset( &self, hour_offset: i32, minute_offset: i32, ) -> Result<Timestamp>
Creates a timestamp with time zone.
hour_offset
and minute_offset
are time zone offset in hours and minutes from UTC.
All arguments must be zero or positive in the eastern hemisphere. They must be
zero or negative in the western hemisphere.
Sourcepub fn and_prec(&self, precision: u8) -> Result<Timestamp>
pub fn and_prec(&self, precision: u8) -> Result<Timestamp>
Creates a timestamp with precision.
The precision affects text representation of Timestamp. It doesn’t affect comparison.
Sourcepub fn nanosecond(&self) -> u32
pub fn nanosecond(&self) -> u32
Returns the nanosecond number from 0 to 999,999,999.
Sourcepub fn tz_hour_offset(&self) -> i32
pub fn tz_hour_offset(&self) -> i32
Returns hour component of time zone.
Sourcepub fn tz_minute_offset(&self) -> i32
pub fn tz_minute_offset(&self) -> i32
Returns minute component of time zone.