Path: blob/master/Utilities/cmlibarchive/libarchive/archive_parse_date.c
5012 views
/*1* This code is in the public domain and has no copyright.2*3* This is a plain C recursive-descent translation of an old4* public-domain YACC grammar that has been used for parsing dates in5* very many open-source projects.6*7* Since the original authors were generous enough to donate their8* work to the public domain, I feel compelled to match their9* generosity.10*11* Tim Kientzle, February 2009.12*/1314/*15* Header comment from original getdate.y:16*/1718/*19** Originally written by Steven M. Bellovin <[email protected]> while20** at the University of North Carolina at Chapel Hill. Later tweaked by21** a couple of people on Usenet. Completely overhauled by Rich $alz22** <[email protected]> and Jim Berets <[email protected]> in August, 1990;23**24** This grammar has 10 shift/reduce conflicts.25**26** This code is in the public domain and has no copyright.27*/2829#ifndef CM_PARSE_DATE30#include "archive_platform.h"31#endif3233#include <ctype.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <time.h>3839#include "archive.h"4041/* Basic time units. */42#define EPOCH 197043#define MINUTE (60L)44#define HOUR (60L * MINUTE)45#define DAY (24L * HOUR)4647/* Daylight-savings mode: on, off, or not yet known. */48enum DSTMODE { DSTon, DSToff, DSTmaybe };49/* Meridian: am or pm. */50enum { tAM, tPM };51/* Token types returned by nexttoken() */52enum { tAGO = 260, tDAY, tDAYZONE, tAMPM, tMONTH, tMONTH_UNIT, tSEC_UNIT,53tUNUMBER, tZONE, tDST };54struct token { int token; time_t value; };5556/*57* Parser state.58*/59struct gdstate {60struct token *tokenp; /* Pointer to next token. */61/* HaveXxxx counts how many of this kind of phrase we've seen;62* it's a fatal error to have more than one time, zone, day,63* or date phrase. */64int HaveYear;65int HaveMonth;66int HaveDay;67int HaveWeekDay; /* Day of week */68int HaveTime; /* Hour/minute/second */69int HaveZone; /* timezone and/or DST info */70int HaveRel; /* time offset; we can have more than one */71/* Absolute time values. */72time_t Timezone; /* Seconds offset from GMT */73time_t Day;74time_t Hour;75time_t Minutes;76time_t Month;77time_t Seconds;78time_t Year;79/* DST selection */80enum DSTMODE DSTmode;81/* Day of week accounting, e.g., "3rd Tuesday" */82time_t DayOrdinal; /* "3" in "3rd Tuesday" */83time_t DayNumber; /* "Tuesday" in "3rd Tuesday" */84/* Relative time values: hour/day/week offsets are measured in85* seconds, month/year are counted in months. */86time_t RelMonth;87time_t RelSeconds;88};8990/*91* A series of functions that recognize certain common time phrases.92* Each function returns 1 if it managed to make sense of some of the93* tokens, zero otherwise.94*/9596/*97* hour:minute or hour:minute:second with optional AM, PM, or numeric98* timezone offset99*/100static int101timephrase(struct gdstate *gds)102{103if (gds->tokenp[0].token == tUNUMBER104&& gds->tokenp[1].token == ':'105&& gds->tokenp[2].token == tUNUMBER106&& gds->tokenp[3].token == ':'107&& gds->tokenp[4].token == tUNUMBER) {108/* "12:14:18" or "22:08:07" */109++gds->HaveTime;110gds->Hour = gds->tokenp[0].value;111gds->Minutes = gds->tokenp[2].value;112gds->Seconds = gds->tokenp[4].value;113gds->tokenp += 5;114}115else if (gds->tokenp[0].token == tUNUMBER116&& gds->tokenp[1].token == ':'117&& gds->tokenp[2].token == tUNUMBER) {118/* "12:14" or "22:08" */119++gds->HaveTime;120gds->Hour = gds->tokenp[0].value;121gds->Minutes = gds->tokenp[2].value;122gds->Seconds = 0;123gds->tokenp += 3;124}125else if (gds->tokenp[0].token == tUNUMBER126&& gds->tokenp[1].token == tAMPM) {127/* "7" is a time if it's followed by "am" or "pm" */128++gds->HaveTime;129gds->Hour = gds->tokenp[0].value;130gds->Minutes = gds->Seconds = 0;131/* We'll handle the AM/PM below. */132gds->tokenp += 1;133} else {134/* We can't handle this. */135return 0;136}137138if (gds->tokenp[0].token == tAMPM) {139/* "7:12pm", "12:20:13am" */140if (gds->Hour == 12)141gds->Hour = 0;142if (gds->tokenp[0].value == tPM)143gds->Hour += 12;144gds->tokenp += 1;145}146if (gds->tokenp[0].token == '+'147&& gds->tokenp[1].token == tUNUMBER) {148/* "7:14+0700" */149gds->HaveZone++;150gds->DSTmode = DSToff;151gds->Timezone = - ((gds->tokenp[1].value / 100) * HOUR152+ (gds->tokenp[1].value % 100) * MINUTE);153gds->tokenp += 2;154}155if (gds->tokenp[0].token == '-'156&& gds->tokenp[1].token == tUNUMBER) {157/* "19:14:12-0530" */158gds->HaveZone++;159gds->DSTmode = DSToff;160gds->Timezone = + ((gds->tokenp[1].value / 100) * HOUR161+ (gds->tokenp[1].value % 100) * MINUTE);162gds->tokenp += 2;163}164return 1;165}166167/*168* Timezone name, possibly including DST.169*/170static int171zonephrase(struct gdstate *gds)172{173if (gds->tokenp[0].token == tZONE174&& gds->tokenp[1].token == tDST) {175gds->HaveZone++;176gds->Timezone = gds->tokenp[0].value;177gds->DSTmode = DSTon;178gds->tokenp += 1;179return 1;180}181182if (gds->tokenp[0].token == tZONE) {183gds->HaveZone++;184gds->Timezone = gds->tokenp[0].value;185gds->DSTmode = DSToff;186gds->tokenp += 1;187return 1;188}189190if (gds->tokenp[0].token == tDAYZONE) {191gds->HaveZone++;192gds->Timezone = gds->tokenp[0].value;193gds->DSTmode = DSTon;194gds->tokenp += 1;195return 1;196}197return 0;198}199200/*201* Year/month/day in various combinations.202*/203static int204datephrase(struct gdstate *gds)205{206if (gds->tokenp[0].token == tUNUMBER207&& gds->tokenp[1].token == '/'208&& gds->tokenp[2].token == tUNUMBER209&& gds->tokenp[3].token == '/'210&& gds->tokenp[4].token == tUNUMBER) {211gds->HaveYear++;212gds->HaveMonth++;213gds->HaveDay++;214if (gds->tokenp[0].value >= 13) {215/* First number is big: 2004/01/29, 99/02/17 */216gds->Year = gds->tokenp[0].value;217gds->Month = gds->tokenp[2].value;218gds->Day = gds->tokenp[4].value;219} else if ((gds->tokenp[4].value >= 13)220|| (gds->tokenp[2].value >= 13)) {221/* Last number is big: 01/07/98 */222/* Middle number is big: 01/29/04 */223gds->Month = gds->tokenp[0].value;224gds->Day = gds->tokenp[2].value;225gds->Year = gds->tokenp[4].value;226} else {227/* No significant clues: 02/03/04 */228gds->Month = gds->tokenp[0].value;229gds->Day = gds->tokenp[2].value;230gds->Year = gds->tokenp[4].value;231}232gds->tokenp += 5;233return 1;234}235236if (gds->tokenp[0].token == tUNUMBER237&& gds->tokenp[1].token == '/'238&& gds->tokenp[2].token == tUNUMBER) {239/* "1/15" */240gds->HaveMonth++;241gds->HaveDay++;242gds->Month = gds->tokenp[0].value;243gds->Day = gds->tokenp[2].value;244gds->tokenp += 3;245return 1;246}247248if (gds->tokenp[0].token == tUNUMBER249&& gds->tokenp[1].token == '-'250&& gds->tokenp[2].token == tUNUMBER251&& gds->tokenp[3].token == '-'252&& gds->tokenp[4].token == tUNUMBER) {253/* ISO 8601 format. yyyy-mm-dd. */254gds->HaveYear++;255gds->HaveMonth++;256gds->HaveDay++;257gds->Year = gds->tokenp[0].value;258gds->Month = gds->tokenp[2].value;259gds->Day = gds->tokenp[4].value;260gds->tokenp += 5;261return 1;262}263264if (gds->tokenp[0].token == tUNUMBER265&& gds->tokenp[1].token == '-'266&& gds->tokenp[2].token == tMONTH267&& gds->tokenp[3].token == '-'268&& gds->tokenp[4].token == tUNUMBER) {269gds->HaveYear++;270gds->HaveMonth++;271gds->HaveDay++;272if (gds->tokenp[0].value > 31) {273/* e.g. 1992-Jun-17 */274gds->Year = gds->tokenp[0].value;275gds->Month = gds->tokenp[2].value;276gds->Day = gds->tokenp[4].value;277} else {278/* e.g. 17-JUN-1992. */279gds->Day = gds->tokenp[0].value;280gds->Month = gds->tokenp[2].value;281gds->Year = gds->tokenp[4].value;282}283gds->tokenp += 5;284return 1;285}286287if (gds->tokenp[0].token == tMONTH288&& gds->tokenp[1].token == tUNUMBER289&& gds->tokenp[2].token == ','290&& gds->tokenp[3].token == tUNUMBER) {291/* "June 17, 2001" */292gds->HaveYear++;293gds->HaveMonth++;294gds->HaveDay++;295gds->Month = gds->tokenp[0].value;296gds->Day = gds->tokenp[1].value;297gds->Year = gds->tokenp[3].value;298gds->tokenp += 4;299return 1;300}301302if (gds->tokenp[0].token == tMONTH303&& gds->tokenp[1].token == tUNUMBER) {304/* "May 3" */305gds->HaveMonth++;306gds->HaveDay++;307gds->Month = gds->tokenp[0].value;308gds->Day = gds->tokenp[1].value;309gds->tokenp += 2;310return 1;311}312313if (gds->tokenp[0].token == tUNUMBER314&& gds->tokenp[1].token == tMONTH315&& gds->tokenp[2].token == tUNUMBER) {316/* "12 Sept 1997" */317gds->HaveYear++;318gds->HaveMonth++;319gds->HaveDay++;320gds->Day = gds->tokenp[0].value;321gds->Month = gds->tokenp[1].value;322gds->Year = gds->tokenp[2].value;323gds->tokenp += 3;324return 1;325}326327if (gds->tokenp[0].token == tUNUMBER328&& gds->tokenp[1].token == tMONTH) {329/* "12 Sept" */330gds->HaveMonth++;331gds->HaveDay++;332gds->Day = gds->tokenp[0].value;333gds->Month = gds->tokenp[1].value;334gds->tokenp += 2;335return 1;336}337338return 0;339}340341/*342* Relative time phrase: "tomorrow", "yesterday", "+1 hour", etc.343*/344static int345relunitphrase(struct gdstate *gds)346{347if (gds->tokenp[0].token == '-'348&& gds->tokenp[1].token == tUNUMBER349&& gds->tokenp[2].token == tSEC_UNIT) {350/* "-3 hours" */351gds->HaveRel++;352gds->RelSeconds -= gds->tokenp[1].value * gds->tokenp[2].value;353gds->tokenp += 3;354return 1;355}356if (gds->tokenp[0].token == '+'357&& gds->tokenp[1].token == tUNUMBER358&& gds->tokenp[2].token == tSEC_UNIT) {359/* "+1 minute" */360gds->HaveRel++;361gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;362gds->tokenp += 3;363return 1;364}365if (gds->tokenp[0].token == tUNUMBER366&& gds->tokenp[1].token == tSEC_UNIT) {367/* "1 day" */368gds->HaveRel++;369gds->RelSeconds += gds->tokenp[0].value * gds->tokenp[1].value;370gds->tokenp += 2;371return 1;372}373if (gds->tokenp[0].token == '-'374&& gds->tokenp[1].token == tUNUMBER375&& gds->tokenp[2].token == tMONTH_UNIT) {376/* "-3 months" */377gds->HaveRel++;378gds->RelMonth -= gds->tokenp[1].value * gds->tokenp[2].value;379gds->tokenp += 3;380return 1;381}382if (gds->tokenp[0].token == '+'383&& gds->tokenp[1].token == tUNUMBER384&& gds->tokenp[2].token == tMONTH_UNIT) {385/* "+5 years" */386gds->HaveRel++;387gds->RelMonth += gds->tokenp[1].value * gds->tokenp[2].value;388gds->tokenp += 3;389return 1;390}391if (gds->tokenp[0].token == tUNUMBER392&& gds->tokenp[1].token == tMONTH_UNIT) {393/* "2 years" */394gds->HaveRel++;395gds->RelMonth += gds->tokenp[0].value * gds->tokenp[1].value;396gds->tokenp += 2;397return 1;398}399if (gds->tokenp[0].token == tSEC_UNIT) {400/* "now", "tomorrow" */401gds->HaveRel++;402gds->RelSeconds += gds->tokenp[0].value;403gds->tokenp += 1;404return 1;405}406if (gds->tokenp[0].token == tMONTH_UNIT) {407/* "month" */408gds->HaveRel++;409gds->RelMonth += gds->tokenp[0].value;410gds->tokenp += 1;411return 1;412}413return 0;414}415416/*417* Day of the week specification.418*/419static int420dayphrase(struct gdstate *gds)421{422if (gds->tokenp[0].token == tDAY) {423/* "tues", "wednesday," */424gds->HaveWeekDay++;425gds->DayOrdinal = 1;426gds->DayNumber = gds->tokenp[0].value;427gds->tokenp += 1;428if (gds->tokenp[0].token == ',')429gds->tokenp += 1;430return 1;431}432if (gds->tokenp[0].token == tUNUMBER433&& gds->tokenp[1].token == tDAY) {434/* "second tues" "3 wed" */435gds->HaveWeekDay++;436gds->DayOrdinal = gds->tokenp[0].value;437gds->DayNumber = gds->tokenp[1].value;438gds->tokenp += 2;439return 1;440}441return 0;442}443444/*445* Try to match a phrase using one of the above functions.446* This layer also deals with a couple of generic issues.447*/448static int449phrase(struct gdstate *gds)450{451if (timephrase(gds))452return 1;453if (zonephrase(gds))454return 1;455if (datephrase(gds))456return 1;457if (dayphrase(gds))458return 1;459if (relunitphrase(gds)) {460if (gds->tokenp[0].token == tAGO) {461gds->RelSeconds = -gds->RelSeconds;462gds->RelMonth = -gds->RelMonth;463gds->tokenp += 1;464}465return 1;466}467468/* Bare numbers sometimes have meaning. */469if (gds->tokenp[0].token == tUNUMBER) {470if (gds->HaveTime && !gds->HaveYear && !gds->HaveRel) {471gds->HaveYear++;472gds->Year = gds->tokenp[0].value;473gds->tokenp += 1;474return 1;475}476477if(gds->tokenp[0].value > 10000) {478/* "20040301" */479gds->HaveYear++;480gds->HaveMonth++;481gds->HaveDay++;482gds->Day= (gds->tokenp[0].value)%100;483gds->Month= (gds->tokenp[0].value/100)%100;484gds->Year = gds->tokenp[0].value/10000;485gds->tokenp += 1;486return 1;487}488489if (gds->tokenp[0].value < 24) {490gds->HaveTime++;491gds->Hour = gds->tokenp[0].value;492gds->Minutes = 0;493gds->Seconds = 0;494gds->tokenp += 1;495return 1;496}497498if ((gds->tokenp[0].value / 100 < 24)499&& (gds->tokenp[0].value % 100 < 60)) {500/* "513" is same as "5:13" */501gds->Hour = gds->tokenp[0].value / 100;502gds->Minutes = gds->tokenp[0].value % 100;503gds->Seconds = 0;504gds->tokenp += 1;505return 1;506}507}508509return 0;510}511512/*513* A dictionary of time words.514*/515static struct LEXICON {516size_t abbrev;517const char *name;518int type;519time_t value;520} const TimeWords[] = {521/* am/pm */522{ 0, "am", tAMPM, tAM },523{ 0, "pm", tAMPM, tPM },524525/* Month names. */526{ 3, "january", tMONTH, 1 },527{ 3, "february", tMONTH, 2 },528{ 3, "march", tMONTH, 3 },529{ 3, "april", tMONTH, 4 },530{ 3, "may", tMONTH, 5 },531{ 3, "june", tMONTH, 6 },532{ 3, "july", tMONTH, 7 },533{ 3, "august", tMONTH, 8 },534{ 3, "september", tMONTH, 9 },535{ 3, "october", tMONTH, 10 },536{ 3, "november", tMONTH, 11 },537{ 3, "december", tMONTH, 12 },538539/* Days of the week. */540{ 2, "sunday", tDAY, 0 },541{ 3, "monday", tDAY, 1 },542{ 2, "tuesday", tDAY, 2 },543{ 3, "wednesday", tDAY, 3 },544{ 2, "thursday", tDAY, 4 },545{ 2, "friday", tDAY, 5 },546{ 2, "saturday", tDAY, 6 },547548/* Timezones: Offsets are in seconds. */549{ 0, "gmt", tZONE, 0*HOUR }, /* Greenwich Mean */550{ 0, "ut", tZONE, 0*HOUR }, /* Universal (Coordinated) */551{ 0, "utc", tZONE, 0*HOUR },552{ 0, "wet", tZONE, 0*HOUR }, /* Western European */553{ 0, "bst", tDAYZONE, 0*HOUR }, /* British Summer */554{ 0, "wat", tZONE, 1*HOUR }, /* West Africa */555{ 0, "at", tZONE, 2*HOUR }, /* Azores */556/* { 0, "bst", tZONE, 3*HOUR }, */ /* Brazil Standard: Conflict */557/* { 0, "gst", tZONE, 3*HOUR }, */ /* Greenland Standard: Conflict*/558{ 0, "nft", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland */559{ 0, "nst", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Standard */560{ 0, "ndt", tDAYZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Daylight */561{ 0, "ast", tZONE, 4*HOUR }, /* Atlantic Standard */562{ 0, "adt", tDAYZONE, 4*HOUR }, /* Atlantic Daylight */563{ 0, "est", tZONE, 5*HOUR }, /* Eastern Standard */564{ 0, "edt", tDAYZONE, 5*HOUR }, /* Eastern Daylight */565{ 0, "cst", tZONE, 6*HOUR }, /* Central Standard */566{ 0, "cdt", tDAYZONE, 6*HOUR }, /* Central Daylight */567{ 0, "mst", tZONE, 7*HOUR }, /* Mountain Standard */568{ 0, "mdt", tDAYZONE, 7*HOUR }, /* Mountain Daylight */569{ 0, "pst", tZONE, 8*HOUR }, /* Pacific Standard */570{ 0, "pdt", tDAYZONE, 8*HOUR }, /* Pacific Daylight */571{ 0, "yst", tZONE, 9*HOUR }, /* Yukon Standard */572{ 0, "ydt", tDAYZONE, 9*HOUR }, /* Yukon Daylight */573{ 0, "hst", tZONE, 10*HOUR }, /* Hawaii Standard */574{ 0, "hdt", tDAYZONE, 10*HOUR }, /* Hawaii Daylight */575{ 0, "cat", tZONE, 10*HOUR }, /* Central Alaska */576{ 0, "ahst", tZONE, 10*HOUR }, /* Alaska-Hawaii Standard */577{ 0, "nt", tZONE, 11*HOUR }, /* Nome */578{ 0, "idlw", tZONE, 12*HOUR }, /* Intl Date Line West */579{ 0, "cet", tZONE, -1*HOUR }, /* Central European */580{ 0, "met", tZONE, -1*HOUR }, /* Middle European */581{ 0, "mewt", tZONE, -1*HOUR }, /* Middle European Winter */582{ 0, "mest", tDAYZONE, -1*HOUR }, /* Middle European Summer */583{ 0, "swt", tZONE, -1*HOUR }, /* Swedish Winter */584{ 0, "sst", tDAYZONE, -1*HOUR }, /* Swedish Summer */585{ 0, "fwt", tZONE, -1*HOUR }, /* French Winter */586{ 0, "fst", tDAYZONE, -1*HOUR }, /* French Summer */587{ 0, "eet", tZONE, -2*HOUR }, /* Eastern Eur, USSR Zone 1 */588{ 0, "bt", tZONE, -3*HOUR }, /* Baghdad, USSR Zone 2 */589{ 0, "it", tZONE, -3*HOUR-30*MINUTE },/* Iran */590{ 0, "zp4", tZONE, -4*HOUR }, /* USSR Zone 3 */591{ 0, "zp5", tZONE, -5*HOUR }, /* USSR Zone 4 */592{ 0, "ist", tZONE, -5*HOUR-30*MINUTE },/* Indian Standard */593{ 0, "zp6", tZONE, -6*HOUR }, /* USSR Zone 5 */594/* { 0, "nst", tZONE, -6.5*HOUR }, */ /* North Sumatra: Conflict */595/* { 0, "sst", tZONE, -7*HOUR }, */ /* So Sumatra, USSR 6: Conflict */596{ 0, "wast", tZONE, -7*HOUR }, /* West Australian Standard */597{ 0, "wadt", tDAYZONE, -7*HOUR }, /* West Australian Daylight */598{ 0, "jt", tZONE, -7*HOUR-30*MINUTE },/* Java (3pm in Cronusland!)*/599{ 0, "cct", tZONE, -8*HOUR }, /* China Coast, USSR Zone 7 */600{ 0, "jst", tZONE, -9*HOUR }, /* Japan Std, USSR Zone 8 */601{ 0, "cast", tZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Std */602{ 0, "cadt", tDAYZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Daylt */603{ 0, "east", tZONE, -10*HOUR }, /* Eastern Australian Std */604{ 0, "eadt", tDAYZONE, -10*HOUR }, /* Eastern Australian Daylt */605{ 0, "gst", tZONE, -10*HOUR }, /* Guam Std, USSR Zone 9 */606{ 0, "nzt", tZONE, -12*HOUR }, /* New Zealand */607{ 0, "nzst", tZONE, -12*HOUR }, /* New Zealand Standard */608{ 0, "nzdt", tDAYZONE, -12*HOUR }, /* New Zealand Daylight */609{ 0, "idle", tZONE, -12*HOUR }, /* Intl Date Line East */610611{ 0, "dst", tDST, 0 },612613/* Time units. */614{ 4, "years", tMONTH_UNIT, 12 },615{ 5, "months", tMONTH_UNIT, 1 },616{ 9, "fortnights", tSEC_UNIT, 14 * DAY },617{ 4, "weeks", tSEC_UNIT, 7 * DAY },618{ 3, "days", tSEC_UNIT, DAY },619{ 4, "hours", tSEC_UNIT, HOUR },620{ 3, "minutes", tSEC_UNIT, MINUTE },621{ 3, "seconds", tSEC_UNIT, 1 },622623/* Relative-time words. */624{ 0, "tomorrow", tSEC_UNIT, DAY },625{ 0, "yesterday", tSEC_UNIT, -DAY },626{ 0, "today", tSEC_UNIT, 0 },627{ 0, "now", tSEC_UNIT, 0 },628{ 0, "last", tUNUMBER, -1 },629{ 0, "this", tSEC_UNIT, 0 },630{ 0, "next", tUNUMBER, 2 },631{ 0, "first", tUNUMBER, 1 },632{ 0, "1st", tUNUMBER, 1 },633/* { 0, "second", tUNUMBER, 2 }, */634{ 0, "2nd", tUNUMBER, 2 },635{ 0, "third", tUNUMBER, 3 },636{ 0, "3rd", tUNUMBER, 3 },637{ 0, "fourth", tUNUMBER, 4 },638{ 0, "4th", tUNUMBER, 4 },639{ 0, "fifth", tUNUMBER, 5 },640{ 0, "5th", tUNUMBER, 5 },641{ 0, "sixth", tUNUMBER, 6 },642{ 0, "seventh", tUNUMBER, 7 },643{ 0, "eighth", tUNUMBER, 8 },644{ 0, "ninth", tUNUMBER, 9 },645{ 0, "tenth", tUNUMBER, 10 },646{ 0, "eleventh", tUNUMBER, 11 },647{ 0, "twelfth", tUNUMBER, 12 },648{ 0, "ago", tAGO, 1 },649650/* Military timezones. */651{ 0, "a", tZONE, 1*HOUR },652{ 0, "b", tZONE, 2*HOUR },653{ 0, "c", tZONE, 3*HOUR },654{ 0, "d", tZONE, 4*HOUR },655{ 0, "e", tZONE, 5*HOUR },656{ 0, "f", tZONE, 6*HOUR },657{ 0, "g", tZONE, 7*HOUR },658{ 0, "h", tZONE, 8*HOUR },659{ 0, "i", tZONE, 9*HOUR },660{ 0, "k", tZONE, 10*HOUR },661{ 0, "l", tZONE, 11*HOUR },662{ 0, "m", tZONE, 12*HOUR },663{ 0, "n", tZONE, -1*HOUR },664{ 0, "o", tZONE, -2*HOUR },665{ 0, "p", tZONE, -3*HOUR },666{ 0, "q", tZONE, -4*HOUR },667{ 0, "r", tZONE, -5*HOUR },668{ 0, "s", tZONE, -6*HOUR },669{ 0, "t", tZONE, -7*HOUR },670{ 0, "u", tZONE, -8*HOUR },671{ 0, "v", tZONE, -9*HOUR },672{ 0, "w", tZONE, -10*HOUR },673{ 0, "x", tZONE, -11*HOUR },674{ 0, "y", tZONE, -12*HOUR },675{ 0, "z", tZONE, 0*HOUR },676677/* End of table. */678{ 0, NULL, 0, 0 }679};680681/*682* Year is either:683* = A number from 0 to 99, which means a year from 1970 to 2069, or684* = The actual year (>=100).685*/686static time_t687Convert(time_t Month, time_t Day, time_t Year,688time_t Hours, time_t Minutes, time_t Seconds,689time_t Timezone, enum DSTMODE DSTmode)690{691signed char DaysInMonth[12] = {69231, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31693};694time_t Julian;695int i;696struct tm *ltime;697#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)698struct tm tmbuf;699#endif700701if (Year < 69)702Year += 2000;703else if (Year < 100)704Year += 1900;705DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)706? 29 : 28;707if (Year < EPOCH || (sizeof(time_t) <= 4 && Year >= 2038)708|| Month < 1 || Month > 12709/* Lint fluff: "conversion from long may lose accuracy" */710|| Day < 1 || Day > DaysInMonth[(int)--Month]711|| Hours < 0 || Hours > 23712|| Minutes < 0 || Minutes > 59713|| Seconds < 0 || Seconds > 59)714return -1;715716Julian = Day - 1;717for (i = 0; i < Month; i++)718Julian += DaysInMonth[i];719for (i = EPOCH; i < Year; i++)720Julian += 365 + (i % 4 == 0);721Julian *= DAY;722Julian += Timezone;723Julian += Hours * HOUR + Minutes * MINUTE + Seconds;724#if defined(HAVE_LOCALTIME_S)725ltime = localtime_s(&tmbuf, &Julian) ? NULL : &tmbuf;726#elif defined(HAVE_LOCALTIME_R)727ltime = localtime_r(&Julian, &tmbuf);728#else729ltime = localtime(&Julian);730#endif731if (DSTmode == DSTon732|| (DSTmode == DSTmaybe && ltime->tm_isdst))733Julian -= HOUR;734return Julian;735}736737static time_t738DSTcorrect(time_t Start, time_t Future)739{740time_t StartDay;741time_t FutureDay;742struct tm *ltime;743#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)744struct tm tmbuf;745#endif746#if defined(HAVE_LOCALTIME_S)747ltime = localtime_s(&tmbuf, &Start) ? NULL : &tmbuf;748#elif defined(HAVE_LOCALTIME_R)749ltime = localtime_r(&Start, &tmbuf);750#else751ltime = localtime(&Start);752#endif753StartDay = (ltime->tm_hour + 1) % 24;754#if defined(HAVE_LOCALTIME_S)755ltime = localtime_s(&tmbuf, &Future) ? NULL : &tmbuf;756#elif defined(HAVE_LOCALTIME_R)757ltime = localtime_r(&Future, &tmbuf);758#else759ltime = localtime(&Future);760#endif761FutureDay = (ltime->tm_hour + 1) % 24;762return (Future - Start) + (StartDay - FutureDay) * HOUR;763}764765766static time_t767RelativeDate(time_t Start, time_t zone, int dstmode,768time_t DayOrdinal, time_t DayNumber)769{770struct tm *tm;771time_t t, now;772#if defined(HAVE_GMTIME_R) || defined(HAVE_GMTIME_S)773struct tm tmbuf;774#endif775776t = Start - zone;777#if defined(HAVE_GMTIME_S)778tm = gmtime_s(&tmbuf, &t) ? NULL : &tmbuf;779#elif defined(HAVE_GMTIME_R)780tm = gmtime_r(&t, &tmbuf);781#else782tm = gmtime(&t);783#endif784now = Start;785now += DAY * ((DayNumber - tm->tm_wday + 7) % 7);786now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);787if (dstmode == DSTmaybe)788return DSTcorrect(Start, now);789return now - Start;790}791792793static time_t794RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth)795{796struct tm *tm;797time_t Month;798time_t Year;799#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)800struct tm tmbuf;801#endif802803if (RelMonth == 0)804return 0;805#if defined(HAVE_LOCALTIME_S)806tm = localtime_s(&tmbuf, &Start) ? NULL : &tmbuf;807#elif defined(HAVE_LOCALTIME_R)808tm = localtime_r(&Start, &tmbuf);809#else810tm = localtime(&Start);811#endif812Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;813Year = Month / 12;814Month = Month % 12 + 1;815return DSTcorrect(Start,816Convert(Month, (time_t)tm->tm_mday, Year,817(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,818Timezone, DSTmaybe));819}820821/*822* Parses and consumes an unsigned number.823* Returns 1 if any number is parsed. Otherwise, *value is unchanged.824*/825static char826consume_unsigned_number(const char **in, time_t *value)827{828char c;829if (isdigit((unsigned char)(c = **in))) {830for (*value = 0; isdigit((unsigned char)(c = *(*in)++)); )831*value = 10 * *value + c - '0';832(*in)--;833return 1;834}835return 0;836}837838/*839* Tokenizer.840*/841static int842nexttoken(const char **in, time_t *value)843{844char c;845char buff[64];846847for ( ; ; ) {848while (isspace((unsigned char)**in))849++*in;850851/* Skip parenthesized comments. */852if (**in == '(') {853int Count = 0;854do {855c = *(*in)++;856if (c == '\0')857return c;858if (c == '(')859Count++;860else if (c == ')')861Count--;862} while (Count > 0);863continue;864}865866/* Try the next token in the word table first. */867/* This allows us to match "2nd", for example. */868{869const char *src = *in;870const struct LEXICON *tp;871unsigned i = 0;872873/* Force to lowercase and strip '.' characters. */874while (*src != '\0'875&& (isalnum((unsigned char)*src) || *src == '.')876&& i < sizeof(buff)-1) {877if (*src != '.') {878if (isupper((unsigned char)*src))879buff[i++] = (char)tolower(880(unsigned char)*src);881else882buff[i++] = *src;883}884src++;885}886buff[i] = '\0';887888/*889* Find the first match. If the word can be890* abbreviated, make sure we match at least891* the minimum abbreviation.892*/893for (tp = TimeWords; tp->name; tp++) {894size_t abbrev = tp->abbrev;895if (abbrev == 0)896abbrev = strlen(tp->name);897if (strlen(buff) >= abbrev898&& strncmp(tp->name, buff, strlen(buff))899== 0) {900/* Skip over token. */901*in = src;902/* Return the match. */903*value = tp->value;904return tp->type;905}906}907}908909/*910* Not in the word table, maybe it's a number. Note:911* Because '-' and '+' have other special meanings, I912* don't deal with signed numbers here.913*/914if (consume_unsigned_number(in, value)) {915return (tUNUMBER);916}917918return *(*in)++;919}920}921922#define TM_YEAR_ORIGIN 1900923924/* Yield A - B, measured in seconds. */925static long926difftm (struct tm *a, struct tm *b)927{928int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);929int by = b->tm_year + (TM_YEAR_ORIGIN - 1);930long days = (931/* difference in day of year */932a->tm_yday - b->tm_yday933/* + intervening leap days */934+ ((ay >> 2) - (by >> 2))935- (ay/100 - by/100)936+ ((ay/100 >> 2) - (by/100 >> 2))937/* + difference in years * 365 */938+ (long)(ay-by) * 365939);940return (days * DAY + (a->tm_hour - b->tm_hour) * HOUR941+ (a->tm_min - b->tm_min) * MINUTE942+ (a->tm_sec - b->tm_sec));943}944945/*946* Parses a Unix epoch timestamp (seconds).947* This supports a subset of what GNU tar accepts from black box testing,948* but covers common use cases.949*/950static time_t951parse_unix_epoch(const char *p)952{953time_t epoch;954955/* may begin with + */956if (*p == '+') {957p++;958}959960/* followed by some number */961if (!consume_unsigned_number(&p, &epoch))962return (time_t)-1;963964/* ...and nothing else */965if (*p != '\0')966return (time_t)-1;967968return epoch;969}970971/*972*973* The public function.974*975* TODO: tokens[] array should be dynamically sized.976*/977time_t978archive_parse_date(time_t now, const char *p)979{980struct token tokens[256];981struct gdstate _gds;982struct token *lasttoken;983struct gdstate *gds;984struct tm local, *tm;985struct tm gmt, *gmt_ptr;986time_t Start;987time_t tod;988long tzone;989990/*991* @-prefixed Unix epoch timestamps (seconds)992* Skip the complex tokenizer - We do not want to accept strings like "@tenth"993*/994if (*p == '@')995return parse_unix_epoch(p + 1);996997/* Clear out the parsed token array. */998memset(tokens, 0, sizeof(tokens));999/* Initialize the parser state. */1000memset(&_gds, 0, sizeof(_gds));1001gds = &_gds;10021003/* Look up the current time. */1004#if defined(HAVE_LOCALTIME_S)1005tm = localtime_s(&local, &now) ? NULL : &local;1006#elif defined(HAVE_LOCALTIME_R)1007tm = localtime_r(&now, &local);1008#else1009memset(&local, 0, sizeof(local));1010tm = localtime(&now);1011#endif1012if (tm == NULL)1013return -1;1014#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S)1015local = *tm;1016#endif10171018/* Look up UTC if we can and use that to determine the current1019* timezone offset. */1020#if defined(HAVE_GMTIME_S)1021gmt_ptr = gmtime_s(&gmt, &now) ? NULL : &gmt;1022#elif defined(HAVE_GMTIME_R)1023gmt_ptr = gmtime_r(&now, &gmt);1024#else1025memset(&gmt, 0, sizeof(gmt));1026gmt_ptr = gmtime(&now);1027if (gmt_ptr != NULL) {1028/* Copy, in case localtime and gmtime use the same buffer. */1029gmt = *gmt_ptr;1030}1031#endif1032if (gmt_ptr != NULL)1033tzone = difftm (&gmt, &local);1034else1035/* This system doesn't understand timezones; fake it. */1036tzone = 0;1037if(local.tm_isdst)1038tzone += HOUR;10391040/* Tokenize the input string. */1041lasttoken = tokens;1042while ((lasttoken->token = nexttoken(&p, &lasttoken->value)) != 0) {1043++lasttoken;1044if (lasttoken > tokens + 255)1045return -1;1046}1047gds->tokenp = tokens;10481049/* Match phrases until we run out of input tokens. */1050while (gds->tokenp < lasttoken) {1051if (!phrase(gds))1052return -1;1053}10541055/* Use current local timezone if none was specified. */1056if (!gds->HaveZone) {1057gds->Timezone = tzone;1058gds->DSTmode = DSTmaybe;1059}10601061/* If a timezone was specified, use that for generating the default1062* time components instead of the local timezone. */1063if (gds->HaveZone && gmt_ptr != NULL) {1064now -= gds->Timezone;1065#if defined(HAVE_GMTIME_S)1066gmt_ptr = gmtime_s(&gmt, &now) ? NULL : &gmt;1067#elif defined(HAVE_GMTIME_R)1068gmt_ptr = gmtime_r(&now, &gmt);1069#else1070gmt_ptr = gmtime(&now);1071#endif1072if (gmt_ptr != NULL)1073local = *gmt_ptr;1074now += gds->Timezone;1075}10761077if (!gds->HaveYear)1078gds->Year = local.tm_year + 1900;1079if (!gds->HaveMonth)1080gds->Month = local.tm_mon + 1;1081if (!gds->HaveDay)1082gds->Day = local.tm_mday;1083/* Note: No default for hour/min/sec; a specifier that just1084* gives date always refers to 00:00 on that date. */10851086/* If we saw more than one time, timezone, weekday, year, month,1087* or day, then give up. */1088if (gds->HaveTime > 1 || gds->HaveZone > 1 || gds->HaveWeekDay > 11089|| gds->HaveYear > 1 || gds->HaveMonth > 1 || gds->HaveDay > 1)1090return -1;10911092/* Compute an absolute time based on whatever absolute information1093* we collected. */1094if (gds->HaveYear || gds->HaveMonth || gds->HaveDay1095|| gds->HaveTime || gds->HaveWeekDay) {1096Start = Convert(gds->Month, gds->Day, gds->Year,1097gds->Hour, gds->Minutes, gds->Seconds,1098gds->Timezone, gds->DSTmode);1099if (Start < 0)1100return -1;1101} else {1102Start = now;1103if (!gds->HaveRel)1104Start -= local.tm_hour * HOUR + local.tm_min * MINUTE1105+ local.tm_sec;1106}11071108/* Add the relative offset. */1109Start += gds->RelSeconds;1110Start += RelativeMonth(Start, gds->Timezone, gds->RelMonth);11111112/* Adjust for day-of-week offsets. */1113if (gds->HaveWeekDay1114&& !(gds->HaveYear || gds->HaveMonth || gds->HaveDay)) {1115tod = RelativeDate(Start, gds->Timezone,1116gds->DSTmode, gds->DayOrdinal, gds->DayNumber);1117Start += tod;1118}11191120/* -1 is an error indicator, so return 0 instead of -1 if1121* that's the actual time. */1122return Start == -1 ? 0 : Start;1123}112411251126#if defined(TEST)11271128/* ARGSUSED */1129int1130main(int argc, char **argv)1131{1132time_t d;1133time_t now = time(NULL);11341135while (*++argv != NULL) {1136(void)printf("Input: %s\n", *argv);1137d = get_date(now, *argv);1138if (d == -1)1139(void)printf("Bad format - couldn't convert.\n");1140else1141(void)printf("Output: %s\n", ctime(&d));1142}1143exit(0);1144/* NOTREACHED */1145}1146#endif /* defined(TEST) */114711481149