GS1.DateUtils (gs1_barcode v0.1.2)

View Source

Utilities for handling date formats specified by GS1.

Provides functionality to validate and convert 6-char GS1 date strings (in YYMMDD or YYMMD0 format) into Date.t(). Includes logic for resolving the century based on the GS1 genspec and handling zeroed DD (day) fields, which signify the last day of the month.

GS1 GenSpec Note on Day Field (DD): If only the year and month are available, the DD field must be filled with two zeroes ("00"), unless otherwise noted by the specific Application Identifier (AI).

Summary

Types

GS1 date format specifier.

Functions

Converts a 6-char GS1 date string (YYMMDD or YYMMD0) into a Date.t().

Checks if a 6-char string is a valid date in the GS1 YYMMDD (YYMMD0) format.

Types

date_format()

@type date_format() :: :yymmdd | :yymmd0

GS1 date format specifier.

  • :yymmdd - standard 6-character date format requiring a specific day. The day field (DD) must be a valid day of the month (01-31) and cannot be zeroed ("00").

  • :yymmd0 - extended format that additionally allows a zeroed day field ("00"). When DD is "00", the date is interpreted as the last day of the specified month.

Functions

to_date(arg1, bin)

@spec to_date(date_format(), String.t()) :: {:ok, Date.t()} | {:error, term()}

Converts a 6-char GS1 date string (YYMMDD or YYMMD0) into a Date.t().

  • For :yymmdd, the date must be a specific day.

  • For :yymmd0, if DD is "00", the resultant date is interpreted as the last day of the month, including any adjustments for leap years.

  • GS1 GenSpec Note on Zeroed Day: If the day field is "00", the date SHALL be interpreted as the last day of the noted month. e.g., "130200" is "2013-02-28", "160200" is "2016-02-29"

Examples

iex> GS1.DateUtils.to_date(:yymmdd, "251231")
{:ok, ~D[2025-12-31]}
iex> GS1.DateUtils.to_date(:yymmdd, "250230")
{:error, :invalid_date}
iex> GS1.DateUtils.to_date(:yymmdd, "251200")
{:error, :invalid_date}
iex> GS1.DateUtils.to_date(:yymmd0, "240200") # must return Feb 29 on leap year
{:ok, ~D[2024-02-29]}

valid?(date_format, date)

@spec valid?(date_format(), String.t()) :: boolean()

Checks if a 6-char string is a valid date in the GS1 YYMMDD (YYMMD0) format.

The :yymmdd format does not allow a zeroed DD part ("00"). The :yymmd0 format does allow a zeroed DD part ("00").

Examples

iex> GS1.DateUtils.valid?(:yymmdd, "251231")
true
iex> GS1.DateUtils.valid?(:yymmdd, "250230") # invalid (Feb 30)
false
iex> GS1.DateUtils.valid?(:yymmdd, "25123") # invalid length
false
iex> GS1.DateUtils.valid?(:yymmdd, "250200") # :yymmdd doesn't allows zeroed `DD`
false
iex> GS1.DateUtils.valid?(:yymmd0, "250200") # but :yymmd0 allows zeroed `DD`
true