Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requirement of tryParseDateTime etc not mentioned in documentation #281

Open
JWambaugh opened this issue Sep 21, 2022 · 7 comments
Open

Comments

@JWambaugh
Copy link

Builds now fail unless these functions are defined in the model, yet there's no mention of them in the documentation:

final defaultDateFormat = intl.DateFormat('dd-MMMM-yyyy');

/// Specify a defaultTimeFormat (Optional) default (hh:mm a)
final defaultTimeFormat = intl.DateFormat('hh:mm a');

/// Specify a defaultDateTimeFormat (Optional) default (dd-MM-yyyy - hh:mm a)
final defaultDateTimeFormat = intl.DateFormat('$defaultDateFormat - $defaultTimeFormat');

DateTime toDateTime(TimeOfDay x) {
  return DateTime(2020, 1, 1, x.hour, x.minute);
}

TimeOfDay? tryParseTime(String x) {
  final DateTime? d = tryParseTimeToDate(x);
  return d == null ? null : TimeOfDay.fromDateTime(d);
}

DateTime? tryParseTimeToDate(String x) {
  try {
    return int.tryParse(x) != null ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(x)!) : defaultTimeFormat.parse(x);
  } catch (e) {
    return tryParseDateTime(x);
  }
}

DateTime? tryParseDate(String x) {
  try {
    return defaultDateFormat.parse(x);
  } catch (e) {
    return tryParseDateTime(x);
  }
}

DateTime? tryParseDateTime(String x) {
  try {
    return defaultDateTimeFormat.parse(x);
  } catch (e) {
    return DateTime.tryParse(x);
  }
}
@hhtokpinar
Copy link
Owner

hhtokpinar commented Sep 21, 2022

for now, paste the following code into your model.dart


...
part 'model.g.dart';
part 'model.g.view.dart';


/// region Date Format
///
/// Specify a defaultDateFormat (Optional) default (dd-MM-yyyy)
final defaultDateFormat = intl.DateFormat('dd-MM-yyyy');

/// Specify a defaultTimeFormat (Optional) default (hh:mm a)
final defaultTimeFormat = intl.DateFormat('hh:mm a');

/// Specify a defaultDateTimeFormat (Optional) default (dd-MM-yyyy - hh:mm a)
final defaultDateTimeFormat =
    intl.DateFormat('$defaultDateFormat - $defaultTimeFormat');

DateTime toDateTime(TimeOfDay x) {
  return DateTime(2020, 1, 1, x.hour, x.minute);
}

TimeOfDay? tryParseTime(String x) {
  final DateTime? d = tryParseTimeToDate(x);
  return d == null ? null : TimeOfDay.fromDateTime(d);
}

DateTime? tryParseTimeToDate(String x) {
  try {
    return int.tryParse(x) != null
        ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(x)!)
        : defaultTimeFormat.parse(x);
  } catch (e) {
    return tryParseDateTime(x);
  }
}

DateTime? tryParseDate(String x) {
  try {
    return defaultDateFormat.parse(x);
  } catch (e) {
    return tryParseDateTime(x);
  }
}

DateTime? tryParseDateTime(String x) {
  try {
    return defaultDateTimeFormat.parse(x);
  } catch (e) {
    return DateTime.tryParse(x);
  }
}

/// endregion

@GoedertDalmolin
Copy link

GoedertDalmolin commented Sep 27, 2022

When I insert this line in my code:
final defaultDateTimeFormat = intl.DateFormat('$defaultDateFormat - $defaultTimeFormat');

When viewing the database, it saves the following value in the column of type "DateTime":
In1tAMn1e of DateFormat - In1tAMn1e of DateFormat

But if I enter the following code:
final defaultDateTimeFormat = intl.DateFormat('dd-MMMM-yyyy - hh:mm a');

It works!

However, I found this update a little strange.
Because this standardization of date values ​​should be optional and not mandatory, as it is implemented today

I work with receiving Json info where dates are saved in milliseconds correctly.
But when I use "DateTime.now()" it saves the date in a formatted way, but I would like to have only the date in millisecond format, which this package update made it very difficult.

I want to save iso in milliseconds and read the value in milliseconds converting to DateTime without formatting the date to String.
How can I do this to resave each "DateTime" type in milliseconds?

@hhtokpinar
Copy link
Owner

It's @a7mdragab 's work. We can take back that change if it affects db badly
What do you say @a7mdragab ?

@GoedertDalmolin
Copy link

If it is possible to always save the information in milliseconds and later just read them using these "date formats" in my case it would solve it. Until they make a decision about the future of this package, I will downgrade the version I use in my project.

@JWambaugh
Copy link
Author

JWambaugh commented Sep 28, 2022

I have no idea why the default behavior was changed without documentation.

@GoedertDalmolin This is what I ended up doing so the old milliseconds since epoch would work:
in model.dart:


class IntFormatter {
  int format(DateTime date) {
    return date.millisecondsSinceEpoch;
  }
}


final defaultDateFormat = intl.DateFormat('dd-MMMM-yyyy');

/// Specify a defaultTimeFormat (Optional) default (hh:mm a)
final defaultTimeFormat = intl.DateFormat('hh:mm a');

/// Specify a defaultDateTimeFormat (Optional) default (dd-MM-yyyy - hh:mm a)
final defaultDateTimeFormat = IntFormatter();

DateTime toDateTime(TimeOfDay x) {
  return DateTime(2020, 1, 1, x.hour, x.minute);
}

TimeOfDay? tryParseTime(String x) {
  final DateTime? d = tryParseTimeToDate(x);
  return d == null ? null : TimeOfDay.fromDateTime(d);
}

DateTime? tryParseTimeToDate(String x) {
  try {
    return int.tryParse(x) != null ? DateTime.fromMillisecondsSinceEpoch(int.tryParse(x)!) : defaultTimeFormat.parse(x);
  } catch (e) {
    return null;
  }
}

DateTime? tryParseDate(String x) {
  return tryParseDateTime(x);
}

DateTime? tryParseDateTime(String x) {
  return tryParseTimeToDate(x);
}

@GoedertDalmolin
Copy link

GoedertDalmolin commented Sep 28, 2022

Hi @JWambaugh.

I wanted to thank you from the bottom of my heart!}
Thank you very much.

This worked for me.

I use this package in a production application that has clients, and I decided to update to the latest version.

Unfortunately I came across this situation that you commented, where there is no documentation for such a big change.
On top of that, having this documentation prevents new users from being able to use the package easily.

I hope they don't make decisions like that again.

Add "parse" method by updating your code @JWambaugh:

class IntFormatter {
  int format(DateTime date) {
    return date.millisecondsSinceEpoch;
  }

  DateTime parse(String x) {
    return DateTime.parse(x);
  }
}

/// region Dates Formats
final defaultDateFormat = intl.DateFormat("yyyy-MMMM-dd");
final defaultTimeFormat = intl.DateFormat("H:mm:s");
final defaultDateTimeFormat = IntFormatter();

DateTime? tryParseDateTime(String x) {
  try {
    return defaultDateTimeFormat.parse(x);
  } catch (e) {
    return TryDateTimeParseFromMilliseconds(x);
  }
}

DateTime? TryDateTimeParseFromMilliseconds(String x) {
  try {
    return DateTime.fromMillisecondsSinceEpoch(int.parse(x));
  } catch (e) {
    return TryDateTimeParseFromString(x);
  }
}

@hhtokpinar
Copy link
Owner

hhtokpinar commented Sep 29, 2022

Hi all,
I removed customizing datetime formats. You can update the package

sqfentity_gen: 2.3.0+4

If someone wants to use DefaultDateTime format, must specify the sqfentity_gen version in pubspec.yaml
sqfentity_gen: 2.3.0+3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants