|
| 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 | +} |
0 commit comments