Skip to content

Instantly share code, notes, and snippets.

@wilburx9
Last active March 4, 2021 22:12
Show Gist options
  • Select an option

  • Save wilburx9/b258f9703ffda866bbd109a44c4b2a90 to your computer and use it in GitHub Desktop.

Select an option

Save wilburx9/b258f9703ffda866bbd109a44c4b2a90 to your computer and use it in GitHub Desktop.

Revisions

  1. @Wilburt Wilburt revised this gist Jul 20, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions card_utils.dart
    Original file line number Diff line number Diff line change
    @@ -64,15 +64,15 @@
    // The month has passed if:
    // 1. The year is in the past. In that case, we just assume that the month
    // has passed
    // 2. Card's month (plus another month) is more than current month.
    // 2. Card's month (plus another month) is less than current month.
    return hasYearPassed(year) ||
    convertYearTo4Digits(year) == now.year && (month < now.month + 1);
    }

    static bool hasYearPassed(int year) {
    int fourDigitsYear = convertYearTo4Digits(year);
    var now = DateTime.now();
    // The year has passed if the year we are currently is more than card's
    // The year has passed if the year we are currently, is greater than card's
    // year
    return fourDigitsYear < now.year;
    }
  2. @Wilburt Wilburt revised this gist Jul 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion card_utils.dart
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@

    var fourDigitsYear = convertYearTo4Digits(year);
    if ((fourDigitsYear < 1) || (fourDigitsYear > 2099)) {
    // We are assuming a valid should be between 1 and 2099.
    // We are assuming a valid year should be between 1 and 2099.
    // Note that, it's valid doesn't mean that it has not expired.
    return 'Expiry year is invalid';
    }
  3. @Wilburt Wilburt created this gist Jul 20, 2018.
    78 changes: 78 additions & 0 deletions card_utils.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    static String validateDate(String value) {
    if (value.isEmpty) {
    return Strings.fieldReq;
    }

    int year;
    int month;
    // The value contains a forward slash if the month and year has been
    // entered.
    if (value.contains(new RegExp(r'(\/)'))) {
    var split = value.split(new RegExp(r'(\/)'));
    // The value before the slash is the month while the value to right of
    // it is the year.
    month = int.parse(split[0]);
    year = int.parse(split[1]);

    } else { // Only the month was entered
    month = int.parse(value.substring(0, (value.length)));
    year = -1; // Lets use an invalid year intentionally
    }

    if ((month < 1) || (month > 12)) {
    // A valid month is between 1 (January) and 12 (December)
    return 'Expiry month is invalid';
    }

    var fourDigitsYear = convertYearTo4Digits(year);
    if ((fourDigitsYear < 1) || (fourDigitsYear > 2099)) {
    // We are assuming a valid should be between 1 and 2099.
    // Note that, it's valid doesn't mean that it has not expired.
    return 'Expiry year is invalid';
    }

    if (!hasDateExpired(month, year)) {
    return "Card has expired";
    }
    return null;
    }


    /// Convert the two-digit year to four-digit year if necessary
    static int convertYearTo4Digits(int year) {
    if (year < 100 && year >= 0) {
    var now = DateTime.now();
    String currentYear = now.year.toString();
    String prefix = currentYear.substring(0, currentYear.length - 2);
    year = int.parse('$prefix${year.toString().padLeft(2, '0')}');
    }
    return year;
    }


    static bool hasDateExpired(int month, int year) {
    return !(month == null || year == null) && isNotExpired(year, month);
    }

    static bool isNotExpired(int year, int month) {
    // It has not expired if both the year and date has not passed
    return !hasYearPassed(year) && !hasMonthPassed(year, month);
    }

    static bool hasMonthPassed(int year, int month) {
    var now = DateTime.now();
    // The month has passed if:
    // 1. The year is in the past. In that case, we just assume that the month
    // has passed
    // 2. Card's month (plus another month) is more than current month.
    return hasYearPassed(year) ||
    convertYearTo4Digits(year) == now.year && (month < now.month + 1);
    }

    static bool hasYearPassed(int year) {
    int fourDigitsYear = convertYearTo4Digits(year);
    var now = DateTime.now();
    // The year has passed if the year we are currently is more than card's
    // year
    return fourDigitsYear < now.year;
    }