Converting Dates to Week Dates
Ed Schaefer and John Spurgeon
Dates are often identified by a year, a month, and a
day of the month. An alternative notation, common in European countries and
in manufacturing industries, uses a year, a week number, and a day of the
week to specify a date. The main difficulty with using weeks to specify a date
is identifying when the first week of a year should begin. ISO, the
International Standards Organization, created the ISO 8601 standard to
reduce confusion in the area of date/time representations. Regarding week
dates, the document states that calendar weeks start on Monday; weeks are
numbered starting with the number one; and the first calendar week of a
year is the week that includes the first Thursday of the year. Algorithms based on the ISO 8601 definition of a week
have been developed to convert dates from year-month-day format to
year-week-day format. One example is an algorithm described by Rick McCarty
[1]. But what if the ISO 8601 definition of a week date
doesn't apply? What if you are operating under the assumption that
weeks start on Sunday, not Monday? What if January 1st must fall in the
first calendar week of the year? We developed Korn shell script ymd2ywd (Listing 1)
that converts a date in year-month-day format to an equivalent date in
year-week-day format. The algorithm employed is similar to McCarty's.
Both algorithms rely only on simple math functions and convert a wide range
of dates. (Some algorithms rely on date arithmetic capabilities provided by
the programming environment or significantly limit the range of dates that
can be converted.) However, it is easy to configure our ymd2ywd script to
account for various assumptions about when a week begins and what
constitutes the first week of the year.
|