Skip to content

Commit 35aa597

Browse files
author
Isaque Neves
committed
fix decode timestamp without timezone as local DateTime, ignore past timezone transitions
1 parent b670e5e commit 35aa597

File tree

5 files changed

+303
-14
lines changed

5 files changed

+303
-14
lines changed

.gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ doc/api/
2929
main.exe
3030
teste_concorrencia.dart
3131
teste_utils.dart
32-
teste_insert_get_id.dart
32+
teste_insert_get_id.dart
33+
34+
Main.java
35+
Main.class
36+
Main.cs
37+
Microsoft.Net.Compilers.4.2.0
38+
csharp/
39+
java/

example/bin/explorations.dart

+283
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
// ignore_for_file: dead_code
2+
3+
import 'dart:io';
4+
5+
import 'package:eloquent/eloquent.dart';
6+
//nano mytimezone
7+
//zic -d /usr/share/zoneinfo/ mytimezone
8+
//cd /usr/share/zoneinfo/
9+
//nano /usr/share/zoneinfo/America/Sao_Paulo
10+
// cp /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime
11+
// zdump -v America/Sao_Paulo
12+
//apt install --reinstall tzdata
13+
14+
class CustomDateTime {
15+
int year = DateTime.now().year;
16+
int month = 1;
17+
int day = 1;
18+
int hour = 0;
19+
int minute = 0;
20+
int second = 0;
21+
int millisecond = 0;
22+
int microsecond = 0;
23+
double offsetHours = 0.0;
24+
25+
CustomDateTime(this.year,
26+
[this.month = 1,
27+
this.day = 1,
28+
this.hour = 0,
29+
this.minute = 0,
30+
this.second = 0,
31+
this.millisecond = 0,
32+
this.microsecond = 0,
33+
this.offsetHours = 0]);
34+
35+
CustomDateTime.now({this.offsetHours = 0}) {
36+
final now = DateTime.now().toUtc();
37+
final adjusted = now.add(Duration(
38+
hours: offsetHours.floor(),
39+
minutes: ((offsetHours - offsetHours.floor()) * 60).round()));
40+
year = adjusted.year;
41+
month = adjusted.month;
42+
day = adjusted.day;
43+
hour = adjusted.hour;
44+
minute = adjusted.minute;
45+
second = adjusted.second;
46+
millisecond = adjusted.millisecond;
47+
microsecond = adjusted.microsecond;
48+
}
49+
50+
@override
51+
String toString() {
52+
int offsetHour = offsetHours.floor();
53+
int offsetMinute = ((offsetHours - offsetHour) * 60).round();
54+
String offsetSign = offsetHours < 0 ? '-' : '+';
55+
return '$year-${_twoDigits(month)}-${_twoDigits(day)} ${_twoDigits(hour)}:${_twoDigits(minute)}:${_twoDigits(second)}.${_threeDigits(millisecond)}${_threeDigits(microsecond)} (UTC$offsetSign${_twoDigits(offsetHour.abs())}:${_twoDigits(offsetMinute.abs())})';
56+
}
57+
58+
String toIso8601String() {
59+
int offsetHour = offsetHours.floor();
60+
int offsetMinute = ((offsetHours - offsetHour) * 60).round();
61+
String offsetSign = offsetHours < 0 ? '-' : '+';
62+
return '$year-${_twoDigits(month)}-${_twoDigits(day)}T${_twoDigits(hour)}:${_twoDigits(minute)}:${_twoDigits(second)}.${_threeDigits(millisecond)}${_threeDigits(microsecond)}Z$offsetSign${_twoDigits(offsetHour.abs())}:${_twoDigits(offsetMinute.abs())}';
63+
}
64+
65+
String _twoDigits(int n) {
66+
if (n >= 10) return "$n";
67+
return "0$n";
68+
}
69+
70+
String _threeDigits(int n) {
71+
if (n >= 100) return "$n";
72+
if (n >= 10) return "0$n";
73+
return "00$n";
74+
}
75+
76+
CustomDateTime add(Duration duration) {
77+
DateTime utcDateTime = DateTime(
78+
year, month, day, hour, minute, second, millisecond, microsecond)
79+
.toUtc();
80+
DateTime newDateTime = utcDateTime.add(duration).toUtc();
81+
return CustomDateTime(
82+
newDateTime.year,
83+
newDateTime.month,
84+
newDateTime.day,
85+
newDateTime.hour,
86+
newDateTime.minute,
87+
newDateTime.second,
88+
newDateTime.millisecond,
89+
newDateTime.microsecond,
90+
offsetHours,
91+
);
92+
}
93+
94+
CustomDateTime subtract(Duration duration) {
95+
return add(-duration);
96+
}
97+
98+
Duration difference(CustomDateTime other) {
99+
DateTime thisDateTime = DateTime(
100+
year, month, day, hour, minute, second, millisecond, microsecond)
101+
.toUtc();
102+
DateTime otherDateTime = DateTime(
103+
other.year,
104+
other.month,
105+
other.day,
106+
other.hour,
107+
other.minute,
108+
other.second,
109+
other.millisecond,
110+
other.microsecond)
111+
.toUtc();
112+
return thisDateTime.difference(otherDateTime);
113+
}
114+
115+
bool isBefore(CustomDateTime other) {
116+
DateTime thisDateTime = DateTime(
117+
year, month, day, hour, minute, second, millisecond, microsecond)
118+
.toUtc();
119+
DateTime otherDateTime = DateTime(
120+
other.year,
121+
other.month,
122+
other.day,
123+
other.hour,
124+
other.minute,
125+
other.second,
126+
other.millisecond,
127+
other.microsecond)
128+
.toUtc();
129+
return thisDateTime.isBefore(otherDateTime);
130+
}
131+
132+
bool isAfter(CustomDateTime other) {
133+
return !isBefore(other) && !isAtSameMomentAs(other);
134+
}
135+
136+
bool isAtSameMomentAs(CustomDateTime other) {
137+
return !isBefore(other) && !isAfter(other);
138+
}
139+
140+
int compareTo(CustomDateTime other) {
141+
DateTime thisDateTime = DateTime(
142+
year, month, day, hour, minute, second, millisecond, microsecond)
143+
.toUtc();
144+
DateTime otherDateTime = DateTime(
145+
other.year,
146+
other.month,
147+
other.day,
148+
other.hour,
149+
other.minute,
150+
other.second,
151+
other.millisecond,
152+
other.microsecond)
153+
.toUtc();
154+
return thisDateTime.compareTo(otherDateTime);
155+
}
156+
}
157+
158+
void main3() {
159+
CustomDateTime customDateTime =
160+
CustomDateTime(2024, 7, 25, 17, 00, 0, 0, 0, -3.5);
161+
print('customDateTime $customDateTime');
162+
163+
CustomDateTime customNow = CustomDateTime.now(offsetHours: -3.5);
164+
print(customNow);
165+
166+
// Adicionando e subtraindo duração
167+
CustomDateTime addedDateTime =
168+
customDateTime.add(Duration(hours: 5, minutes: 30));
169+
print(addedDateTime);
170+
171+
CustomDateTime subtractedDateTime =
172+
customDateTime.subtract(Duration(hours: 2, minutes: 15));
173+
print(subtractedDateTime);
174+
175+
// Comparando datas
176+
print(customDateTime.isBefore(customNow));
177+
print(customDateTime.isAfter(customNow));
178+
print(customDateTime.isAtSameMomentAs(customNow));
179+
print(customDateTime.compareTo(customNow));
180+
}
181+
182+
extension DateTimeExtension on DateTime {
183+
DateTime asLocal() {
184+
///return DateTime(year,month, day, hour, minute, second, millisecond, microsecond);
185+
final now = DateTime.now();
186+
final timeZoneOffset = now.timeZoneOffset;
187+
// dt.copyWith(
188+
// year: year,
189+
// month: month,
190+
// day: day,
191+
// hour: hour,
192+
// minute: minute,
193+
// second: second,
194+
// millisecond: millisecond,
195+
// microsecond: microsecond);
196+
return add(timeZoneOffset);
197+
}
198+
}
199+
200+
void main(List<String> args) async {
201+
//774702600000000 = 2024-07-19 11:10:00 = DateTime(2024, 07, 19, 11, 10, 00)
202+
final dur = Duration(microseconds: 774702600000000);
203+
final dtUtc = DateTime.utc(2000).add(dur);
204+
205+
final nowDt = DateTime.now();
206+
var baseDt = DateTime(2000);
207+
208+
if (baseDt.timeZoneOffset != nowDt.timeZoneOffset) {
209+
final difference = baseDt.timeZoneOffset - nowDt.timeZoneOffset;
210+
baseDt = baseDt.add(difference);
211+
}
212+
final dtLocalDecode = baseDt.add(dur);
213+
214+
final dartDt = DateTime(2000, 1, 1, 0, 0, 0, 0, 0);
215+
216+
print('dtUtc $dtUtc ${dtUtc.timeZoneOffset} ${dtUtc.timeZoneName}');
217+
218+
print(
219+
'dtLocal decode $dtLocalDecode ${dtLocalDecode.timeZoneOffset} ${dtLocalDecode.timeZoneName}');
220+
221+
print('dartDt $dartDt ${dartDt.timeZoneOffset} ${dartDt.timeZoneName}');
222+
223+
print('dartNow $nowDt ${nowDt.timeZoneOffset} ${nowDt.timeZoneName}');
224+
return;
225+
226+
final manager = Manager();
227+
manager.addConnection({
228+
'driver': 'pgsql',
229+
'driver_implementation': 'postgres', // postgres | dargres | postgres_v3
230+
'timezone': 'America/Sao_Paulo',
231+
'forceDecodeTimestamptzAsUTC': false,
232+
'forceDecodeTimestampAsUTC': false,
233+
'forceDecodeDateAsUTC': false,
234+
'pool': true,
235+
'poolsize': 2,
236+
'host': 'localhost',
237+
'port': '5435',
238+
'database': 'sistemas',
239+
'username': 'dart',
240+
'password': 'dart',
241+
'charset': 'win1252',
242+
'prefix': '',
243+
'schema': ['public'],
244+
//'sslmode' : 'require',
245+
});
246+
247+
manager.setAsGlobal();
248+
249+
final connection = await manager.connection();
250+
251+
// var results =
252+
// await connection.select("select current_timestamp, current_date ");
253+
// print('results: ${results}');
254+
// var currentTimestamp = results.first['current_timestamp'] as DateTime;
255+
// print('dafault: $currentTimestamp ${currentTimestamp.timeZoneName}');
256+
// print('local: ${currentTimestamp.toLocal()}');
257+
258+
// final id = await connection.table('sigep.inscricoes').insertGetId({
259+
// 'titulo': 'teste',
260+
// 'anoExercicio': 2024,
261+
// 'dataInicial': DateTime(2024, 07, 19, 11, 10, 00),
262+
// 'dataFinal': DateTime(2024, 07, 19, 11, 10, 00),
263+
// });
264+
265+
final result =
266+
await connection.table('sigep.inscricoes').where('id', '=', 21).first();
267+
print('result ${result}');
268+
269+
// await connection.execute("set timezone to 'America/Sao_Paulo'");
270+
// results = await connection.execute("select current_timestamp");
271+
// currentTimestamp = results.first.first as DateTime;
272+
// print(
273+
// 'America/Sao_Paulo: $currentTimestamp ${currentTimestamp.timeZoneName}');
274+
275+
// final res = await db.transaction((ctx) async {
276+
// await ctx.table('test_table').insert({'id':10,'name':'Jane Doe'});
277+
// final res = await ctx.table('test_table').limit(2).get();
278+
// return res;
279+
// });
280+
// print('res $res');
281+
282+
exit(0);
283+
}

example/bin/postgre_v2_example.dart

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import 'dart:io';
2-
32
import 'package:eloquent/eloquent.dart';
43

54
void main(List<String> args) async {
65
final manager = Manager();
76
manager.addConnection({
87
'driver': 'pgsql',
9-
'driver_implementation': 'postgres_v3', // postgres | dargres | postgres_v3
10-
'timezone': 'America/Sao_Paulo',
8+
'driver_implementation': 'postgres', // postgres | dargres | postgres_v3
9+
'timezone': 'America/Sao_Paulo',
1110
'forceDecodeTimestamptzAsUTC': false,
1211
'forceDecodeTimestampAsUTC': false,
1312
'forceDecodeDateAsUTC': false,
1413
'pool': true,
1514
'poolsize': 2,
1615
'host': 'localhost',
1716
'port': '5435',
18-
'database': 'siamweb',
17+
'database': 'sistemas',
1918
'username': 'dart',
2019
'password': 'dart',
2120
'charset': 'win1252',
@@ -30,16 +29,15 @@ void main(List<String> args) async {
3029

3130
var results =
3231
await connection.select("select current_timestamp, current_date ");
33-
print('results: ${results}');
3432
var currentTimestamp = results.first['current_timestamp'] as DateTime;
3533
print('dafault: $currentTimestamp ${currentTimestamp.timeZoneName}');
3634
print('local: ${currentTimestamp.toLocal()}');
3735

38-
// await connection.execute("set timezone to 'America/Sao_Paulo'");
39-
// results = await connection.execute("select current_timestamp");
40-
// currentTimestamp = results.first.first as DateTime;
41-
// print(
42-
// 'America/Sao_Paulo: $currentTimestamp ${currentTimestamp.timeZoneName}');
36+
await connection.select("set timezone to 'America/Sao_Paulo'");
37+
results = await connection.select("select current_timestamp");
38+
currentTimestamp = results.first['current_timestamp'] as DateTime;
39+
print(
40+
'America/Sao_Paulo: $currentTimestamp ${currentTimestamp.timeZoneName}');
4341

4442
// final res = await db.transaction((ctx) async {
4543
// await ctx.table('test_table').insert({'id':10,'name':'Jane Doe'});

example/pubspec.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ name: example_eloquent
22
version: 1.0.0
33
description: example of eloquent
44
publish_to: none
5+
56
environment:
6-
sdk: '>=2.12.0 <3.0.0'
7+
sdk: '>=3.0.0 <4.0.0'
78

89
dependencies:
910
eloquent:
1011
path: ../
1112

12-
dev_dependencies:
13+
#dev_dependencies:
1314

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dependencies:
1717
#postgres:
1818
#path: ../postgresql-dart
1919
# postgres v2
20-
postgres_fork: ^2.8.3
20+
postgres_fork: ^2.8.4
2121
#postgres_fork:
2222
#path: ../postgresql-fork
2323

0 commit comments

Comments
 (0)