AKushWarrior / instant

A package that exports functions for converting, formatting, and nicening of dates/times in Dart.
https://pub.dev/packages/instant
MIT License
11 stars 6 forks source link

How to create a specific datetime in a specific zone #1

Open bsutton opened 4 years ago

bsutton commented 4 years ago

All the classes I can find seem to do a 'conversion' of a local datetime to a specific zone.

I know the time in the zone I just need to create a DateTime instance from that.

Essentially I have a time in a string: 2020-10-27 06:10:05+00:00

I need to parse the string into a datetime.

I'm currently doing expiryDate = DateTime.parse('2020-10-27 06:10:05+00:00');

But I would like to do something like: DateTime dt = dateTimeWithZone(2020, 10, 27, 06, 10, 05, 'GMT');

AKushWarrior commented 4 years ago

GMT is the same as UTC. I'm pretty sure Dart allows you to create DateTimes in UTC.

bsutton commented 4 years ago

OK found it: DateTime.utc( int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0] )

bsutton commented 4 years ago

I'm a little confused on how you library works in storing/converting timezones.

As I understand it a DateTime does not store a timezone.

However all of your methods seem to only allow the target timezone to be specified.

So how does the library correctly convert from one timezone to another unless you pass both the target and the source timezones?

AKushWarrior commented 4 years ago

I'm a little confused on how you library works in storing/converting timezones.

As I understand it a DateTime does not store a timezone.

However all of your methods seem to only allow the target timezone to be specified.

So how does the library correctly convert from one timezone to another unless you pass both the target and the source timezones?

This is late but... Dart does store the timezone, but doesn't make the offsets publicly available. However, Dart is always capable of converting a given DateTime to UTC time (assuming that you didn't mess with it already). That's what this library uses: it converts the passed DateTime to UTC using Dart's built in method, and then applies the offset onto the converted time.