1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! The Oracle time-stamp data types: TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE
mod tosql;
use super::{ Ctx, interval::Interval };
use crate::{ Result, oci::{self, *} };
use std::{ mem, ptr, cmp::Ordering, ops::{Deref, DerefMut} };
pub(crate) fn to_string(fmt: &str, fsprec: u8, ts: &OCIDateTime, ctx: &dyn Ctx) -> Result<String> {
let name = mem::MaybeUninit::<[u8;128]>::uninit();
let mut name = unsafe { name.assume_init() };
let mut size = name.len() as u32;
oci::date_time_to_text(
ctx.as_context(), ctx.as_ref(), ts,
if fmt.len() == 0 { ptr::null() } else { fmt.as_ptr() }, fmt.len() as u8, fsprec,
&mut size as *mut u32, name.as_mut_ptr()
)?;
let txt = &name[0..size as usize];
Ok( String::from_utf8_lossy(txt).to_string() )
}
pub(crate) fn from_timestamp<'a,T>(ts: &Descriptor<T>, ctx: &'a dyn Ctx) -> Result<DateTime<'a, T>>
where T: DescriptorType<OCIType=OCIDateTime>
{
let mut datetime = Descriptor::<T>::new(&ctx)?;
oci::date_time_assign(ctx.as_context(), ctx.as_ref(), ts, datetime.as_mut())?;
Ok( DateTime { ctx, datetime } )
}
pub(crate) fn convert_into<'a,T,U>(ts: &Descriptor<T>, ctx: &'a dyn Ctx) -> Result<DateTime<'a, U>>
where T: DescriptorType<OCIType=OCIDateTime>
, U: DescriptorType<OCIType=OCIDateTime>
{
let mut datetime: Descriptor<U> = Descriptor::new(&ctx)?;
oci::date_time_convert(ctx.as_context(), ctx.as_ref(), ts.as_ref(), datetime.as_mut())?;
Ok( DateTime { ctx, datetime } )
}
/// Represents datetime data types.
pub struct DateTime<'a, T> where T: DescriptorType<OCIType=OCIDateTime> {
datetime: Descriptor<T>,
ctx: &'a dyn Ctx,
}
impl<'a,T> AsRef<T::OCIType> for DateTime<'a,T> where T: DescriptorType<OCIType=OCIDateTime> {
fn as_ref(&self) -> &T::OCIType {
self.datetime.as_ref()
}
}
impl<'a,T> AsMut<T::OCIType> for DateTime<'a,T> where T: DescriptorType<OCIType=OCIDateTime> {
fn as_mut(&mut self) -> &mut T::OCIType {
self.datetime.as_mut()
}
}
impl<'a,T> Deref for DateTime<'a,T> where T: DescriptorType<OCIType=OCIDateTime> {
type Target = T::OCIType;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<'a,T> DerefMut for DateTime<'a,T> where T: DescriptorType<OCIType=OCIDateTime> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}
impl<'a, T> DateTime<'a, T> where T: DescriptorType<OCIType=OCIDateTime> {
/**
Creates an uninitialized timestamp.
# Example
```
use sibyl::{ self as oracle, Timestamp };
let env = oracle::env()?;
let _ts = Timestamp::new(&env)?;
# Ok::<(),oracle::Error>(())
```
*/
pub fn new(ctx: &'a dyn Ctx) -> Result<Self> {
let datetime = Descriptor::<T>::new(&ctx)?;
Ok( Self { ctx, datetime } )
}
/**
Creates a timestamp and populates its fields.
Time zone, as a string, is represented in the format "\[+|-\]\[HH:MM\]". If the time zone is not
specified, then the session default time zone is assumed.
Time zone is ignored for timestamps that do not have one.
For timestamps with a time zone, the date and time fields are assumed to be in the local time
of the specified time zone.
# Example
```
use std::cmp::Ordering;
use sibyl::{ self as oracle, Timestamp, TimestampTZ, TimestampLTZ };
let env = oracle::env()?;
let ts = Timestamp::with_date_and_time(1969, 7, 20, 20, 18, 4, 0, "", &env)?;
assert_eq!(ts.date()?, (1969, 7, 20));
assert_eq!(ts.time()?, (20, 18, 4,0));
let res = ts.tz_offset();
assert!(res.is_err());
match res {
Err( oracle::Error::Oracle(errcode, _errmsg) ) => assert_eq!(1878, errcode),
_ => panic!("unexpected error")
}
let ts = oracle::TimestampTZ::with_date_and_time(1969, 7, 20, 20, 18, 4, 0, "UTC", &env)?;
assert_eq!(ts.date()?, (1969, 7, 20));
assert_eq!(ts.time()?, (20, 18, 4,0));
assert_eq!(ts.tz_offset()?, (0,0));
let ts1 = TimestampLTZ::from_string("1969-7-20 8:18:04 pm", "YYYY-MM-DD HH:MI:SS PM", &env)?;
// Here it gets a little tricky... The timestamp above is in the local time zone
// (whatever "local" is on the machine where this code is running).
// To create the same timestamp using `from_datetime` we need to know that time zone
let tzn = ts1.tz_name()?;
// And then provide it to the `from_datetime` method
let ts2 = TimestampLTZ::with_date_and_time(1969, 7, 20, 20, 18, 4, 0, &tzn, &env)?;
assert_eq!(ts2.compare(&ts1)?, Ordering::Equal);
# Ok::<(),oracle::Error>(())
```
*/
pub fn with_date_and_time(year: i16, month: u8, day: u8, hour: u8, min: u8, sec: u8, fsec: u32, tz: &str, ctx: &'a dyn Ctx) -> Result<Self> {
let mut datetime = Descriptor::<T>::new(&ctx)?;
oci::date_time_construct(
ctx.as_context(), ctx.as_ref(), &mut datetime,
year, month, day, hour, min, sec, fsec, tz.as_ptr(), tz.len()
)?;
Ok( Self { ctx, datetime } )
}
/**
Creates new timestamp from the given string according to the specified format.
If the timestamp is in the user session, the conversion occurs in the session's NLS_LANGUAGE and
the session's NLS_CALENDAR; otherwise, the default is used.
Refer to Oracle [Format Models](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Format-Models.html)
for the description of format.
# Example
```
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts = TimestampTZ::from_string("July 20, 1969 8:18:04.16 pm UTC", "MONTH DD, YYYY HH:MI:SS.FF PM TZR", &env)?;
assert_eq!(ts.date()?, (1969,7,20));
assert_eq!(ts.time()?, (20,18,4,160000000));
assert_eq!(ts.tz_offset()?, (0,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_string(txt: &str, fmt: &str, ctx: &'a dyn Ctx) -> Result<Self> {
let mut datetime = Descriptor::<T>::new(&ctx)?;
oci::date_time_from_text(
ctx.as_context(), ctx.as_ref(),
txt.as_ptr(), txt.len(), fmt.as_ptr(), fmt.len() as u8,
ptr::null(), 0,
&mut datetime
)?;
Ok( Self { ctx, datetime } )
}
/**
Converts this datetime type to another.
The session default time zone (ORA_SDTZ) is used when converting a datetime
without a time zone to one with a time zone.
# Example
```
use sibyl::{ self as oracle, Timestamp, TimestampTZ, TimestampLTZ };
let env = oracle::env()?;
let tzts = TimestampTZ::with_date_and_time(1969, 7, 24, 16, 50, 35, 0, "UTC", &env)?;
let ts : Timestamp = tzts.convert_into(&env)?;
// It just discards the timezone
assert_eq!(ts.date_and_time()?, (1969, 7, 24, 16, 50, 35, 0));
let lts : TimestampLTZ = tzts.convert_into(&env)?;
// It just slaps in the local time zone without shifting the time
assert_eq!(lts.date_and_time()?, (1969, 7, 24, 16, 50, 35, 0));
let (tzh, tzm) = lts.tz_offset()?;
assert_ne!((tzh, tzm), (0, 0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn convert_into<U>(&self, ctx: &'a dyn Ctx) -> Result<DateTime<'a, U>>
where U: DescriptorType<OCIType=OCIDateTime>
{
convert_into(&self.datetime, ctx)
}
/**
Creates a copy of the other timestamp
# Example
```
use std::cmp::Ordering;
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts1 = TimestampTZ::from_systimestamp(&env)?;
let ts2 = TimestampTZ::from_timestamp(&ts1, &env)?;
assert_eq!(ts2.compare(&ts1)?, Ordering::Equal);
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_timestamp(other: &Self, ctx: &'a dyn Ctx) -> Result<Self> {
from_timestamp(&other.datetime, ctx)
}
/**
Adds an interval to self and returns the result as a new timestamp.
# Example
```
use sibyl::{ self as oracle, TimestampTZ, IntervalDS };
let env = oracle::env()?;
let ts1 = TimestampTZ::with_date_and_time(1969,7,20,20,18,4,0,"UTC", &env)?;
let int = IntervalDS::with_duration(0,21,35,56,0,&env)?;
let ts2 = ts1.add(&int)?;
assert_eq!(ts2.to_string("YYYY-MM-DD HH24:MI:SS.FF TZR",1)?, "1969-07-21 17:54:00.0 UTC");
# Ok::<(),oracle::Error>(())
```
*/
pub fn add<I: DescriptorType<OCIType=OCIInterval>>(&self, interval: &Interval<I>) -> Result<Self> {
let ctx = self.ctx;
let mut datetime = Descriptor::<T>::new(&ctx)?;
oci::date_time_interval_add(ctx.as_context(), ctx.as_ref(), &self.datetime, interval, &mut datetime)?;
Ok( Self { ctx, datetime } )
}
/**
Subtracts an interval from self and returns the result as a new timestamp.
# Example
```
use sibyl::{ self as oracle, TimestampTZ, IntervalDS };
let env = oracle::env()?;
let ts1 = TimestampTZ::with_date_and_time(1969,7,21,17,54,0,0,"UTC", &env)?;
let int = IntervalDS::with_duration(0,21,35,56,0,&env)?;
let ts2 = ts1.sub(&int)?;
assert_eq!(ts2.to_string("YYYY-MM-DD HH24:MI:SS.FF TZR",1)?, "1969-07-20 20:18:04.0 UTC");
# Ok::<(),oracle::Error>(())
```
*/
pub fn sub<I: DescriptorType<OCIType=OCIInterval>>(&self, interval: &Interval<I>) -> Result<Self> {
let ctx = self.ctx;
let mut datetime = Descriptor::<T>::new(&ctx)?;
oci::date_time_interval_sub(ctx.as_context(), ctx.as_ref(), &self.datetime, interval, &mut datetime)?;
Ok( Self { ctx, datetime } )
}
/**
Returns the differnce between self and the `other` timestamp as an interval.
# Example
```
use sibyl::{ self as oracle, TimestampTZ, IntervalDS };
let env = oracle::env()?;
let ts1 = TimestampTZ::with_date_and_time(1969,7,20,20,18,4,0,"UTC", &env)?;
let ts2 = TimestampTZ::with_date_and_time(1969,7,21,17,54,0,0,"UTC", &env)?;
let int: IntervalDS = ts2.subtract(&ts1)?;
let (days, hours, min, sec, nanosec) = int.duration()?;
assert_eq!((days, hours, min, sec, nanosec), (0, 21, 35, 56, 0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn subtract<U, I>(&self, other: &DateTime<U>) -> Result<Interval<'_,I>>
where U: DescriptorType<OCIType=OCIDateTime>
, I: DescriptorType<OCIType=OCIInterval>
{
let ctx = self.ctx;
let mut interval: Interval<I> = Interval::new(self.ctx)?;
oci::date_time_subtract(ctx.as_context(), ctx.as_ref(), &self.datetime, &other.datetime, &mut interval)?;
Ok( interval )
}
/**
Compares this timestamp with the `other` one.
# Example
```
use std::cmp::Ordering;
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts1 = TimestampTZ::with_date_and_time(1969,7,20,20,18,4,0,"+00:00", &env)?;
let ts2 = TimestampTZ::with_date_and_time(1969,7,20,16,18,4,0,"-04:00", &env)?;
assert_eq!(ts2.compare(&ts1)?, Ordering::Equal);
# Ok::<(),oracle::Error>(())
```
*/
pub fn compare<U>(&self, other: &DateTime<U>) -> Result<Ordering>
where U: DescriptorType<OCIType=OCIDateTime>
{
let mut cmp = 0i32;
oci::date_time_compare(self.ctx.as_context(), self.ctx.as_ref(), self, other, &mut cmp)?;
let ordering = if cmp < 0 { Ordering::Less } else if cmp == 0 { Ordering::Equal } else { Ordering::Greater };
Ok( ordering )
}
/**
Returns the date (year, month, day) portion of a timestamp.
# Example
```
use sibyl::{ self as oracle, Timestamp };
let env = oracle::env()?;
let ts = Timestamp::with_date_and_time(1969,7,20,20,18,4,0,"", &env)?;
assert_eq!(ts.date()?, (1969, 7, 20));
# Ok::<(),oracle::Error>(())
```
*/
pub fn date(&self) -> Result<(i16, u8, u8)> {
let mut year = oci::Aligned::new(0i16);
let mut month = oci::Aligned::new(0u8);
let mut day = oci::Aligned::new(0u8);
oci::date_time_get_date(self.ctx.as_context(), self.ctx.as_ref(), self, year.as_mut_ptr(), month.as_mut_ptr(), day.as_mut_ptr())?;
Ok( (year.into(), month.into(), day.into()) )
}
/**
Returns the time (hour, min, second, nanosecond) of a timestamp.
# Example
```
use sibyl::{ self as oracle, Timestamp };
let env = oracle::env()?;
let ts = Timestamp::with_date_and_time(1969,7,20,20,18,4,0,"", &env)?;
assert_eq!(ts.time()?, (20, 18, 4, 0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn time(&self) -> Result<(u8, u8, u8, u32)> {
let mut hour = oci::Aligned::new(0u8);
let mut min = oci::Aligned::new(0u8);
let mut sec = oci::Aligned::new(0u8);
let mut fsec = 0u32;
oci::date_time_get_time(self.ctx.as_context(), self.ctx.as_ref(), self, hour.as_mut_ptr(), min.as_mut_ptr(), sec.as_mut_ptr(), &mut fsec)?;
Ok( (hour.into(), min.into(), sec.into(), fsec) )
}
/**
Returns the date and the time (year, month, day, hour, min, second, fractional second)
of a timestamp.
# Example
```
use sibyl::{ self as oracle, Timestamp };
let env = oracle::env()?;
let ts = Timestamp::with_date_and_time(1969,7,20,20,18,4,0,"", &env)?;
assert_eq!(ts.date_and_time()?, (1969, 7, 20, 20, 18, 4, 0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn date_and_time(&self) -> Result<(i16, u8, u8, u8, u8, u8, u32)> {
let (year, month, day) = self.date()?;
let (hour, min, sec, nanos) = self.time()?;
Ok((year, month, day, hour, min, sec, nanos))
}
/**
Returns the time zone name portion of a timestamp
# Example
```
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts = TimestampTZ::from_string("July 20, 1969 8:18:04.16 pm UTC", "MONTH DD, YYYY HH:MI:SS.FF PM TZR", &env)?;
assert_eq!(ts.tz_name()?, "UTC");
let ts = TimestampTZ::with_date_and_time(1969,7,20,20,18,4,0,"+00:00", &env)?;
assert_eq!(ts.tz_name()?, "+00:00");
let ts = TimestampTZ::with_date_and_time(1969,7,20,20,18,4,0,"EST", &env)?;
assert_eq!(ts.tz_name()?, "EST");
# Ok::<(),oracle::Error>(())
```
*/
pub fn tz_name(&self) -> Result<String> {
let name = mem::MaybeUninit::<[u8;64]>::uninit();
let mut name = unsafe { name.assume_init() };
let mut size = name.len() as u32;
oci::date_time_get_time_zone_name(self.ctx.as_context(), self.ctx.as_ref(), self, name.as_mut_ptr(), &mut size)?;
let txt = &name[0..size as usize];
Ok( String::from_utf8_lossy(txt).to_string() )
}
/**
Returns the time zone hour and the time zone minute portion from a timestamp.
# Example
```
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts = TimestampTZ::from_string("July 20, 1969 8:18:04.16 pm UTC", "MONTH DD, YYYY HH:MI:SS.FF PM TZR", &env)?;
let (tzh, tzm) = ts.tz_offset()?;
assert_eq!((tzh, tzm), (0,0));
# Ok::<(),oracle::Error>(())
```
*/
pub fn tz_offset(&self) -> Result<(i8, i8)> {
let mut hours = 0i32;
let mut min = 0i32;
oci::date_time_get_time_zone_offset(self.ctx.as_context(), self.ctx.as_ref(), self, &mut hours as *mut i32 as _, &mut min as *mut i32 as _)?;
Ok( (hours as _, min as _) )
}
/**
Converts the given date to a string according to the specified format.
If timestamp originated in the user session, the conversion occurs in
the session's NLS_LANGUAGE and the session's NLS_CALENDAR; otherwise,
the default is used.
If the conversion format is an empty (zero-length) string, then the date is converted to
a character string in the default format for that type.
Refer to Oracle [Format Models](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Format-Models.html)
for the description of format.
# Example
```
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts = TimestampTZ::with_date_and_time(1969,7,20,20,18,4,0, "UTC", &env)?;
let txt = ts.to_string("Dy, Mon DD, YYYY HH:MI:SS.FF PM TZR", 3)?;
assert_eq!(txt, "Sun, Jul 20, 1969 08:18:04.000 PM UTC");
# Ok::<(),oracle::Error>(())
```
*/
pub fn to_string(&self, fmt: &str, fsprec: u8) -> Result<String> {
to_string(fmt, fsprec, self, self.ctx)
}
}
// For some reason timestamps created by OCIDateTimeSysTimeStamp always have a time zone
// even when their descriptors are OCI_DTYPE_TIMESTAMP. That leads to:
// * "ORA-01483: invalid length for DATE or NUMBER bind variable" when they are inserted
// into the appropriate TS columns or
// * "ORA-00932: inconsistent datatypes: expected %s got %s" when a clone is created via
// OCIDateTimeAssign or
// * "ORA-01870: the intervals or datetimes are not mutually comparable" when 2 timestamps
// of the same type, one of which was created via OCIDateTimeSysTimeStamp, are compared.
// For now, let's just limit `from_systimestamp` to `TimestampTZ` where it produces the expected
// results.
impl<'a> DateTime<'a, OCITimestampTZ> {
/**
Creates new timestamp from the system current date and time.
# Example
```
use sibyl::{ self as oracle, TimestampTZ };
let env = oracle::env()?;
let ts = TimestampTZ::from_systimestamp(&env)?;
let (year, _month, _day) = ts.date()?;
assert!(year >= 2021);
# Ok::<(),oracle::Error>(())
```
*/
pub fn from_systimestamp(ctx: &'a dyn Ctx) -> Result<Self> {
let mut datetime = Descriptor::<OCITimestampTZ>::new(&ctx)?;
oci::date_time_sys_time_stamp(ctx.as_context(), ctx.as_ref(), &mut datetime)?;
Ok( Self { ctx, datetime } )
}
}
impl std::fmt::Debug for DateTime<'_, OCITimestampTZ> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.to_string("YYYY-DD-MM HH24:MI:SSXFF TZR", 3) {
Ok(txt) => fmt.write_fmt(format_args!("TimestampTZ({})", txt)),
Err(err) => fmt.write_fmt(format_args!("TimestampTZ({})", err))
}
}
}
impl std::fmt::Debug for DateTime<'_, OCITimestampLTZ> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.to_string("YYYY-DD-MM HH24:MI:SSXFF TZR", 3) {
Ok(txt) => fmt.write_fmt(format_args!("TimestampLTZ({})", txt)),
Err(err) => fmt.write_fmt(format_args!("TimestampLTZ({})", err))
}
}
}
impl std::fmt::Debug for DateTime<'_, OCITimestamp> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.to_string("YYYY-DD-MM HH24:MI:SSXFF", 3) {
Ok(txt) => fmt.write_fmt(format_args!("Timestamp({})", txt)),
Err(err) => fmt.write_fmt(format_args!("Timestamp({})", err))
}
}
}