Path: blob/master/Utilities/cmlibarchive/libarchive/archive_getdate.c
3153 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_GET_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#define __LIBARCHIVE_BUILD 140#include "archive_getdate.h"4142/* Basic time units. */43#define EPOCH 197044#define MINUTE (60L)45#define HOUR (60L * MINUTE)46#define DAY (24L * HOUR)4748/* Daylight-savings mode: on, off, or not yet known. */49enum DSTMODE { DSTon, DSToff, DSTmaybe };50/* Meridian: am or pm. */51enum { tAM, tPM };52/* Token types returned by nexttoken() */53enum { tAGO = 260, tDAY, tDAYZONE, tAMPM, tMONTH, tMONTH_UNIT, tSEC_UNIT,54tUNUMBER, tZONE, tDST };55struct token { int token; time_t value; };5657/*58* Parser state.59*/60struct gdstate {61struct token *tokenp; /* Pointer to next token. */62/* HaveXxxx counts how many of this kind of phrase we've seen;63* it's a fatal error to have more than one time, zone, day,64* or date phrase. */65int HaveYear;66int HaveMonth;67int HaveDay;68int HaveWeekDay; /* Day of week */69int HaveTime; /* Hour/minute/second */70int HaveZone; /* timezone and/or DST info */71int HaveRel; /* time offset; we can have more than one */72/* Absolute time values. */73time_t Timezone; /* Seconds offset from GMT */74time_t Day;75time_t Hour;76time_t Minutes;77time_t Month;78time_t Seconds;79time_t Year;80/* DST selection */81enum DSTMODE DSTmode;82/* Day of week accounting, e.g., "3rd Tuesday" */83time_t DayOrdinal; /* "3" in "3rd Tuesday" */84time_t DayNumber; /* "Tuesday" in "3rd Tuesday" */85/* Relative time values: hour/day/week offsets are measured in86* seconds, month/year are counted in months. */87time_t RelMonth;88time_t RelSeconds;89};9091/*92* A series of functions that recognize certain common time phrases.93* Each function returns 1 if it managed to make sense of some of the94* tokens, zero otherwise.95*/9697/*98* hour:minute or hour:minute:second with optional AM, PM, or numeric99* timezone offset100*/101static int102timephrase(struct gdstate *gds)103{104if (gds->tokenp[0].token == tUNUMBER105&& gds->tokenp[1].token == ':'106&& gds->tokenp[2].token == tUNUMBER107&& gds->tokenp[3].token == ':'108&& gds->tokenp[4].token == tUNUMBER) {109/* "12:14:18" or "22:08:07" */110++gds->HaveTime;111gds->Hour = gds->tokenp[0].value;112gds->Minutes = gds->tokenp[2].value;113gds->Seconds = gds->tokenp[4].value;114gds->tokenp += 5;115}116else if (gds->tokenp[0].token == tUNUMBER117&& gds->tokenp[1].token == ':'118&& gds->tokenp[2].token == tUNUMBER) {119/* "12:14" or "22:08" */120++gds->HaveTime;121gds->Hour = gds->tokenp[0].value;122gds->Minutes = gds->tokenp[2].value;123gds->Seconds = 0;124gds->tokenp += 3;125}126else if (gds->tokenp[0].token == tUNUMBER127&& gds->tokenp[1].token == tAMPM) {128/* "7" is a time if it's followed by "am" or "pm" */129++gds->HaveTime;130gds->Hour = gds->tokenp[0].value;131gds->Minutes = gds->Seconds = 0;132/* We'll handle the AM/PM below. */133gds->tokenp += 1;134} else {135/* We can't handle this. */136return 0;137}138139if (gds->tokenp[0].token == tAMPM) {140/* "7:12pm", "12:20:13am" */141if (gds->Hour == 12)142gds->Hour = 0;143if (gds->tokenp[0].value == tPM)144gds->Hour += 12;145gds->tokenp += 1;146}147if (gds->tokenp[0].token == '+'148&& gds->tokenp[1].token == tUNUMBER) {149/* "7:14+0700" */150gds->HaveZone++;151gds->DSTmode = DSToff;152gds->Timezone = - ((gds->tokenp[1].value / 100) * HOUR153+ (gds->tokenp[1].value % 100) * MINUTE);154gds->tokenp += 2;155}156if (gds->tokenp[0].token == '-'157&& gds->tokenp[1].token == tUNUMBER) {158/* "19:14:12-0530" */159gds->HaveZone++;160gds->DSTmode = DSToff;161gds->Timezone = + ((gds->tokenp[1].value / 100) * HOUR162+ (gds->tokenp[1].value % 100) * MINUTE);163gds->tokenp += 2;164}165return 1;166}167168/*169* Timezone name, possibly including DST.170*/171static int172zonephrase(struct gdstate *gds)173{174if (gds->tokenp[0].token == tZONE175&& gds->tokenp[1].token == tDST) {176gds->HaveZone++;177gds->Timezone = gds->tokenp[0].value;178gds->DSTmode = DSTon;179gds->tokenp += 1;180return 1;181}182183if (gds->tokenp[0].token == tZONE) {184gds->HaveZone++;185gds->Timezone = gds->tokenp[0].value;186gds->DSTmode = DSToff;187gds->tokenp += 1;188return 1;189}190191if (gds->tokenp[0].token == tDAYZONE) {192gds->HaveZone++;193gds->Timezone = gds->tokenp[0].value;194gds->DSTmode = DSTon;195gds->tokenp += 1;196return 1;197}198return 0;199}200201/*202* Year/month/day in various combinations.203*/204static int205datephrase(struct gdstate *gds)206{207if (gds->tokenp[0].token == tUNUMBER208&& gds->tokenp[1].token == '/'209&& gds->tokenp[2].token == tUNUMBER210&& gds->tokenp[3].token == '/'211&& gds->tokenp[4].token == tUNUMBER) {212gds->HaveYear++;213gds->HaveMonth++;214gds->HaveDay++;215if (gds->tokenp[0].value >= 13) {216/* First number is big: 2004/01/29, 99/02/17 */217gds->Year = gds->tokenp[0].value;218gds->Month = gds->tokenp[2].value;219gds->Day = gds->tokenp[4].value;220} else if ((gds->tokenp[4].value >= 13)221|| (gds->tokenp[2].value >= 13)) {222/* Last number is big: 01/07/98 */223/* Middle number is big: 01/29/04 */224gds->Month = gds->tokenp[0].value;225gds->Day = gds->tokenp[2].value;226gds->Year = gds->tokenp[4].value;227} else {228/* No significant clues: 02/03/04 */229gds->Month = gds->tokenp[0].value;230gds->Day = gds->tokenp[2].value;231gds->Year = gds->tokenp[4].value;232}233gds->tokenp += 5;234return 1;235}236237if (gds->tokenp[0].token == tUNUMBER238&& gds->tokenp[1].token == '/'239&& gds->tokenp[2].token == tUNUMBER) {240/* "1/15" */241gds->HaveMonth++;242gds->HaveDay++;243gds->Month = gds->tokenp[0].value;244gds->Day = gds->tokenp[2].value;245gds->tokenp += 3;246return 1;247}248249if (gds->tokenp[0].token == tUNUMBER250&& gds->tokenp[1].token == '-'251&& gds->tokenp[2].token == tUNUMBER252&& gds->tokenp[3].token == '-'253&& gds->tokenp[4].token == tUNUMBER) {254/* ISO 8601 format. yyyy-mm-dd. */255gds->HaveYear++;256gds->HaveMonth++;257gds->HaveDay++;258gds->Year = gds->tokenp[0].value;259gds->Month = gds->tokenp[2].value;260gds->Day = gds->tokenp[4].value;261gds->tokenp += 5;262return 1;263}264265if (gds->tokenp[0].token == tUNUMBER266&& gds->tokenp[1].token == '-'267&& gds->tokenp[2].token == tMONTH268&& gds->tokenp[3].token == '-'269&& gds->tokenp[4].token == tUNUMBER) {270gds->HaveYear++;271gds->HaveMonth++;272gds->HaveDay++;273if (gds->tokenp[0].value > 31) {274/* e.g. 1992-Jun-17 */275gds->Year = gds->tokenp[0].value;276gds->Month = gds->tokenp[2].value;277gds->Day = gds->tokenp[4].value;278} else {279/* e.g. 17-JUN-1992. */280gds->Day = gds->tokenp[0].value;281gds->Month = gds->tokenp[2].value;282gds->Year = gds->tokenp[4].value;283}284gds->tokenp += 5;285return 1;286}287288if (gds->tokenp[0].token == tMONTH289&& gds->tokenp[1].token == tUNUMBER290&& gds->tokenp[2].token == ','291&& gds->tokenp[3].token == tUNUMBER) {292/* "June 17, 2001" */293gds->HaveYear++;294gds->HaveMonth++;295gds->HaveDay++;296gds->Month = gds->tokenp[0].value;297gds->Day = gds->tokenp[1].value;298gds->Year = gds->tokenp[3].value;299gds->tokenp += 4;300return 1;301}302303if (gds->tokenp[0].token == tMONTH304&& gds->tokenp[1].token == tUNUMBER) {305/* "May 3" */306gds->HaveMonth++;307gds->HaveDay++;308gds->Month = gds->tokenp[0].value;309gds->Day = gds->tokenp[1].value;310gds->tokenp += 2;311return 1;312}313314if (gds->tokenp[0].token == tUNUMBER315&& gds->tokenp[1].token == tMONTH316&& gds->tokenp[2].token == tUNUMBER) {317/* "12 Sept 1997" */318gds->HaveYear++;319gds->HaveMonth++;320gds->HaveDay++;321gds->Day = gds->tokenp[0].value;322gds->Month = gds->tokenp[1].value;323gds->Year = gds->tokenp[2].value;324gds->tokenp += 3;325return 1;326}327328if (gds->tokenp[0].token == tUNUMBER329&& gds->tokenp[1].token == tMONTH) {330/* "12 Sept" */331gds->HaveMonth++;332gds->HaveDay++;333gds->Day = gds->tokenp[0].value;334gds->Month = gds->tokenp[1].value;335gds->tokenp += 2;336return 1;337}338339return 0;340}341342/*343* Relative time phrase: "tomorrow", "yesterday", "+1 hour", etc.344*/345static int346relunitphrase(struct gdstate *gds)347{348if (gds->tokenp[0].token == '-'349&& gds->tokenp[1].token == tUNUMBER350&& gds->tokenp[2].token == tSEC_UNIT) {351/* "-3 hours" */352gds->HaveRel++;353gds->RelSeconds -= gds->tokenp[1].value * gds->tokenp[2].value;354gds->tokenp += 3;355return 1;356}357if (gds->tokenp[0].token == '+'358&& gds->tokenp[1].token == tUNUMBER359&& gds->tokenp[2].token == tSEC_UNIT) {360/* "+1 minute" */361gds->HaveRel++;362gds->RelSeconds += gds->tokenp[1].value * gds->tokenp[2].value;363gds->tokenp += 3;364return 1;365}366if (gds->tokenp[0].token == tUNUMBER367&& gds->tokenp[1].token == tSEC_UNIT) {368/* "1 day" */369gds->HaveRel++;370gds->RelSeconds += gds->tokenp[0].value * gds->tokenp[1].value;371gds->tokenp += 2;372return 1;373}374if (gds->tokenp[0].token == '-'375&& gds->tokenp[1].token == tUNUMBER376&& gds->tokenp[2].token == tMONTH_UNIT) {377/* "-3 months" */378gds->HaveRel++;379gds->RelMonth -= gds->tokenp[1].value * gds->tokenp[2].value;380gds->tokenp += 3;381return 1;382}383if (gds->tokenp[0].token == '+'384&& gds->tokenp[1].token == tUNUMBER385&& gds->tokenp[2].token == tMONTH_UNIT) {386/* "+5 years" */387gds->HaveRel++;388gds->RelMonth += gds->tokenp[1].value * gds->tokenp[2].value;389gds->tokenp += 3;390return 1;391}392if (gds->tokenp[0].token == tUNUMBER393&& gds->tokenp[1].token == tMONTH_UNIT) {394/* "2 years" */395gds->HaveRel++;396gds->RelMonth += gds->tokenp[0].value * gds->tokenp[1].value;397gds->tokenp += 2;398return 1;399}400if (gds->tokenp[0].token == tSEC_UNIT) {401/* "now", "tomorrow" */402gds->HaveRel++;403gds->RelSeconds += gds->tokenp[0].value;404gds->tokenp += 1;405return 1;406}407if (gds->tokenp[0].token == tMONTH_UNIT) {408/* "month" */409gds->HaveRel++;410gds->RelMonth += gds->tokenp[0].value;411gds->tokenp += 1;412return 1;413}414return 0;415}416417/*418* Day of the week specification.419*/420static int421dayphrase(struct gdstate *gds)422{423if (gds->tokenp[0].token == tDAY) {424/* "tues", "wednesday," */425gds->HaveWeekDay++;426gds->DayOrdinal = 1;427gds->DayNumber = gds->tokenp[0].value;428gds->tokenp += 1;429if (gds->tokenp[0].token == ',')430gds->tokenp += 1;431return 1;432}433if (gds->tokenp[0].token == tUNUMBER434&& gds->tokenp[1].token == tDAY) {435/* "second tues" "3 wed" */436gds->HaveWeekDay++;437gds->DayOrdinal = gds->tokenp[0].value;438gds->DayNumber = gds->tokenp[1].value;439gds->tokenp += 2;440return 1;441}442return 0;443}444445/*446* Try to match a phrase using one of the above functions.447* This layer also deals with a couple of generic issues.448*/449static int450phrase(struct gdstate *gds)451{452if (timephrase(gds))453return 1;454if (zonephrase(gds))455return 1;456if (datephrase(gds))457return 1;458if (dayphrase(gds))459return 1;460if (relunitphrase(gds)) {461if (gds->tokenp[0].token == tAGO) {462gds->RelSeconds = -gds->RelSeconds;463gds->RelMonth = -gds->RelMonth;464gds->tokenp += 1;465}466return 1;467}468469/* Bare numbers sometimes have meaning. */470if (gds->tokenp[0].token == tUNUMBER) {471if (gds->HaveTime && !gds->HaveYear && !gds->HaveRel) {472gds->HaveYear++;473gds->Year = gds->tokenp[0].value;474gds->tokenp += 1;475return 1;476}477478if(gds->tokenp[0].value > 10000) {479/* "20040301" */480gds->HaveYear++;481gds->HaveMonth++;482gds->HaveDay++;483gds->Day= (gds->tokenp[0].value)%100;484gds->Month= (gds->tokenp[0].value/100)%100;485gds->Year = gds->tokenp[0].value/10000;486gds->tokenp += 1;487return 1;488}489490if (gds->tokenp[0].value < 24) {491gds->HaveTime++;492gds->Hour = gds->tokenp[0].value;493gds->Minutes = 0;494gds->Seconds = 0;495gds->tokenp += 1;496return 1;497}498499if ((gds->tokenp[0].value / 100 < 24)500&& (gds->tokenp[0].value % 100 < 60)) {501/* "513" is same as "5:13" */502gds->Hour = gds->tokenp[0].value / 100;503gds->Minutes = gds->tokenp[0].value % 100;504gds->Seconds = 0;505gds->tokenp += 1;506return 1;507}508}509510return 0;511}512513/*514* A dictionary of time words.515*/516static struct LEXICON {517size_t abbrev;518const char *name;519int type;520time_t value;521} const TimeWords[] = {522/* am/pm */523{ 0, "am", tAMPM, tAM },524{ 0, "pm", tAMPM, tPM },525526/* Month names. */527{ 3, "january", tMONTH, 1 },528{ 3, "february", tMONTH, 2 },529{ 3, "march", tMONTH, 3 },530{ 3, "april", tMONTH, 4 },531{ 3, "may", tMONTH, 5 },532{ 3, "june", tMONTH, 6 },533{ 3, "july", tMONTH, 7 },534{ 3, "august", tMONTH, 8 },535{ 3, "september", tMONTH, 9 },536{ 3, "october", tMONTH, 10 },537{ 3, "november", tMONTH, 11 },538{ 3, "december", tMONTH, 12 },539540/* Days of the week. */541{ 2, "sunday", tDAY, 0 },542{ 3, "monday", tDAY, 1 },543{ 2, "tuesday", tDAY, 2 },544{ 3, "wednesday", tDAY, 3 },545{ 2, "thursday", tDAY, 4 },546{ 2, "friday", tDAY, 5 },547{ 2, "saturday", tDAY, 6 },548549/* Timezones: Offsets are in seconds. */550{ 0, "gmt", tZONE, 0*HOUR }, /* Greenwich Mean */551{ 0, "ut", tZONE, 0*HOUR }, /* Universal (Coordinated) */552{ 0, "utc", tZONE, 0*HOUR },553{ 0, "wet", tZONE, 0*HOUR }, /* Western European */554{ 0, "bst", tDAYZONE, 0*HOUR }, /* British Summer */555{ 0, "wat", tZONE, 1*HOUR }, /* West Africa */556{ 0, "at", tZONE, 2*HOUR }, /* Azores */557/* { 0, "bst", tZONE, 3*HOUR }, */ /* Brazil Standard: Conflict */558/* { 0, "gst", tZONE, 3*HOUR }, */ /* Greenland Standard: Conflict*/559{ 0, "nft", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland */560{ 0, "nst", tZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Standard */561{ 0, "ndt", tDAYZONE, 3*HOUR+30*MINUTE }, /* Newfoundland Daylight */562{ 0, "ast", tZONE, 4*HOUR }, /* Atlantic Standard */563{ 0, "adt", tDAYZONE, 4*HOUR }, /* Atlantic Daylight */564{ 0, "est", tZONE, 5*HOUR }, /* Eastern Standard */565{ 0, "edt", tDAYZONE, 5*HOUR }, /* Eastern Daylight */566{ 0, "cst", tZONE, 6*HOUR }, /* Central Standard */567{ 0, "cdt", tDAYZONE, 6*HOUR }, /* Central Daylight */568{ 0, "mst", tZONE, 7*HOUR }, /* Mountain Standard */569{ 0, "mdt", tDAYZONE, 7*HOUR }, /* Mountain Daylight */570{ 0, "pst", tZONE, 8*HOUR }, /* Pacific Standard */571{ 0, "pdt", tDAYZONE, 8*HOUR }, /* Pacific Daylight */572{ 0, "yst", tZONE, 9*HOUR }, /* Yukon Standard */573{ 0, "ydt", tDAYZONE, 9*HOUR }, /* Yukon Daylight */574{ 0, "hst", tZONE, 10*HOUR }, /* Hawaii Standard */575{ 0, "hdt", tDAYZONE, 10*HOUR }, /* Hawaii Daylight */576{ 0, "cat", tZONE, 10*HOUR }, /* Central Alaska */577{ 0, "ahst", tZONE, 10*HOUR }, /* Alaska-Hawaii Standard */578{ 0, "nt", tZONE, 11*HOUR }, /* Nome */579{ 0, "idlw", tZONE, 12*HOUR }, /* Intl Date Line West */580{ 0, "cet", tZONE, -1*HOUR }, /* Central European */581{ 0, "met", tZONE, -1*HOUR }, /* Middle European */582{ 0, "mewt", tZONE, -1*HOUR }, /* Middle European Winter */583{ 0, "mest", tDAYZONE, -1*HOUR }, /* Middle European Summer */584{ 0, "swt", tZONE, -1*HOUR }, /* Swedish Winter */585{ 0, "sst", tDAYZONE, -1*HOUR }, /* Swedish Summer */586{ 0, "fwt", tZONE, -1*HOUR }, /* French Winter */587{ 0, "fst", tDAYZONE, -1*HOUR }, /* French Summer */588{ 0, "eet", tZONE, -2*HOUR }, /* Eastern Eur, USSR Zone 1 */589{ 0, "bt", tZONE, -3*HOUR }, /* Baghdad, USSR Zone 2 */590{ 0, "it", tZONE, -3*HOUR-30*MINUTE },/* Iran */591{ 0, "zp4", tZONE, -4*HOUR }, /* USSR Zone 3 */592{ 0, "zp5", tZONE, -5*HOUR }, /* USSR Zone 4 */593{ 0, "ist", tZONE, -5*HOUR-30*MINUTE },/* Indian Standard */594{ 0, "zp6", tZONE, -6*HOUR }, /* USSR Zone 5 */595/* { 0, "nst", tZONE, -6.5*HOUR }, */ /* North Sumatra: Conflict */596/* { 0, "sst", tZONE, -7*HOUR }, */ /* So Sumatra, USSR 6: Conflict */597{ 0, "wast", tZONE, -7*HOUR }, /* West Australian Standard */598{ 0, "wadt", tDAYZONE, -7*HOUR }, /* West Australian Daylight */599{ 0, "jt", tZONE, -7*HOUR-30*MINUTE },/* Java (3pm in Cronusland!)*/600{ 0, "cct", tZONE, -8*HOUR }, /* China Coast, USSR Zone 7 */601{ 0, "jst", tZONE, -9*HOUR }, /* Japan Std, USSR Zone 8 */602{ 0, "cast", tZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Std */603{ 0, "cadt", tDAYZONE, -9*HOUR-30*MINUTE },/* Ctrl Australian Daylt */604{ 0, "east", tZONE, -10*HOUR }, /* Eastern Australian Std */605{ 0, "eadt", tDAYZONE, -10*HOUR }, /* Eastern Australian Daylt */606{ 0, "gst", tZONE, -10*HOUR }, /* Guam Std, USSR Zone 9 */607{ 0, "nzt", tZONE, -12*HOUR }, /* New Zealand */608{ 0, "nzst", tZONE, -12*HOUR }, /* New Zealand Standard */609{ 0, "nzdt", tDAYZONE, -12*HOUR }, /* New Zealand Daylight */610{ 0, "idle", tZONE, -12*HOUR }, /* Intl Date Line East */611612{ 0, "dst", tDST, 0 },613614/* Time units. */615{ 4, "years", tMONTH_UNIT, 12 },616{ 5, "months", tMONTH_UNIT, 1 },617{ 9, "fortnights", tSEC_UNIT, 14 * DAY },618{ 4, "weeks", tSEC_UNIT, 7 * DAY },619{ 3, "days", tSEC_UNIT, DAY },620{ 4, "hours", tSEC_UNIT, HOUR },621{ 3, "minutes", tSEC_UNIT, MINUTE },622{ 3, "seconds", tSEC_UNIT, 1 },623624/* Relative-time words. */625{ 0, "tomorrow", tSEC_UNIT, DAY },626{ 0, "yesterday", tSEC_UNIT, -DAY },627{ 0, "today", tSEC_UNIT, 0 },628{ 0, "now", tSEC_UNIT, 0 },629{ 0, "last", tUNUMBER, -1 },630{ 0, "this", tSEC_UNIT, 0 },631{ 0, "next", tUNUMBER, 2 },632{ 0, "first", tUNUMBER, 1 },633{ 0, "1st", tUNUMBER, 1 },634/* { 0, "second", tUNUMBER, 2 }, */635{ 0, "2nd", tUNUMBER, 2 },636{ 0, "third", tUNUMBER, 3 },637{ 0, "3rd", tUNUMBER, 3 },638{ 0, "fourth", tUNUMBER, 4 },639{ 0, "4th", tUNUMBER, 4 },640{ 0, "fifth", tUNUMBER, 5 },641{ 0, "5th", tUNUMBER, 5 },642{ 0, "sixth", tUNUMBER, 6 },643{ 0, "seventh", tUNUMBER, 7 },644{ 0, "eighth", tUNUMBER, 8 },645{ 0, "ninth", tUNUMBER, 9 },646{ 0, "tenth", tUNUMBER, 10 },647{ 0, "eleventh", tUNUMBER, 11 },648{ 0, "twelfth", tUNUMBER, 12 },649{ 0, "ago", tAGO, 1 },650651/* Military timezones. */652{ 0, "a", tZONE, 1*HOUR },653{ 0, "b", tZONE, 2*HOUR },654{ 0, "c", tZONE, 3*HOUR },655{ 0, "d", tZONE, 4*HOUR },656{ 0, "e", tZONE, 5*HOUR },657{ 0, "f", tZONE, 6*HOUR },658{ 0, "g", tZONE, 7*HOUR },659{ 0, "h", tZONE, 8*HOUR },660{ 0, "i", tZONE, 9*HOUR },661{ 0, "k", tZONE, 10*HOUR },662{ 0, "l", tZONE, 11*HOUR },663{ 0, "m", tZONE, 12*HOUR },664{ 0, "n", tZONE, -1*HOUR },665{ 0, "o", tZONE, -2*HOUR },666{ 0, "p", tZONE, -3*HOUR },667{ 0, "q", tZONE, -4*HOUR },668{ 0, "r", tZONE, -5*HOUR },669{ 0, "s", tZONE, -6*HOUR },670{ 0, "t", tZONE, -7*HOUR },671{ 0, "u", tZONE, -8*HOUR },672{ 0, "v", tZONE, -9*HOUR },673{ 0, "w", tZONE, -10*HOUR },674{ 0, "x", tZONE, -11*HOUR },675{ 0, "y", tZONE, -12*HOUR },676{ 0, "z", tZONE, 0*HOUR },677678/* End of table. */679{ 0, NULL, 0, 0 }680};681682/*683* Year is either:684* = A number from 0 to 99, which means a year from 1970 to 2069, or685* = The actual year (>=100).686*/687static time_t688Convert(time_t Month, time_t Day, time_t Year,689time_t Hours, time_t Minutes, time_t Seconds,690time_t Timezone, enum DSTMODE DSTmode)691{692signed char DaysInMonth[12] = {69331, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31694};695time_t Julian;696int i;697struct tm *ltime;698#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)699struct tm tmbuf;700#endif701702if (Year < 69)703Year += 2000;704else if (Year < 100)705Year += 1900;706DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)707? 29 : 28;708/* Checking for 2038 bogusly assumes that time_t is 32 bits. But709I'm too lazy to try to check for time_t overflow in another way. */710if (Year < EPOCH || Year >= 2038711|| Month < 1 || Month > 12712/* Lint fluff: "conversion from long may lose accuracy" */713|| Day < 1 || Day > DaysInMonth[(int)--Month]714|| Hours < 0 || Hours > 23715|| Minutes < 0 || Minutes > 59716|| Seconds < 0 || Seconds > 59)717return -1;718719Julian = Day - 1;720for (i = 0; i < Month; i++)721Julian += DaysInMonth[i];722for (i = EPOCH; i < Year; i++)723Julian += 365 + (i % 4 == 0);724Julian *= DAY;725Julian += Timezone;726Julian += Hours * HOUR + Minutes * MINUTE + Seconds;727#if defined(HAVE_LOCALTIME_S)728ltime = localtime_s(&tmbuf, &Julian) ? NULL : &tmbuf;729#elif defined(HAVE_LOCALTIME_R)730ltime = localtime_r(&Julian, &tmbuf);731#else732ltime = localtime(&Julian);733#endif734if (DSTmode == DSTon735|| (DSTmode == DSTmaybe && ltime->tm_isdst))736Julian -= HOUR;737return Julian;738}739740static time_t741DSTcorrect(time_t Start, time_t Future)742{743time_t StartDay;744time_t FutureDay;745struct tm *ltime;746#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)747struct tm tmbuf;748#endif749#if defined(HAVE_LOCALTIME_S)750ltime = localtime_s(&tmbuf, &Start) ? NULL : &tmbuf;751#elif defined(HAVE_LOCALTIME_R)752ltime = localtime_r(&Start, &tmbuf);753#else754ltime = localtime(&Start);755#endif756StartDay = (ltime->tm_hour + 1) % 24;757#if defined(HAVE_LOCALTIME_S)758ltime = localtime_s(&tmbuf, &Future) ? NULL : &tmbuf;759#elif defined(HAVE_LOCALTIME_R)760ltime = localtime_r(&Future, &tmbuf);761#else762ltime = localtime(&Future);763#endif764FutureDay = (ltime->tm_hour + 1) % 24;765return (Future - Start) + (StartDay - FutureDay) * HOUR;766}767768769static time_t770RelativeDate(time_t Start, time_t zone, int dstmode,771time_t DayOrdinal, time_t DayNumber)772{773struct tm *tm;774time_t t, now;775#if defined(HAVE_GMTIME_R) || defined(HAVE_GMTIME_S)776struct tm tmbuf;777#endif778779t = Start - zone;780#if defined(HAVE_GMTIME_S)781tm = gmtime_s(&tmbuf, &t) ? NULL : &tmbuf;782#elif defined(HAVE_GMTIME_R)783tm = gmtime_r(&t, &tmbuf);784#else785tm = gmtime(&t);786#endif787now = Start;788now += DAY * ((DayNumber - tm->tm_wday + 7) % 7);789now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);790if (dstmode == DSTmaybe)791return DSTcorrect(Start, now);792return now - Start;793}794795796static time_t797RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth)798{799struct tm *tm;800time_t Month;801time_t Year;802#if defined(HAVE_LOCALTIME_R) || defined(HAVE_LOCALTIME_S)803struct tm tmbuf;804#endif805806if (RelMonth == 0)807return 0;808#if defined(HAVE_LOCALTIME_S)809tm = localtime_s(&tmbuf, &Start) ? NULL : &tmbuf;810#elif defined(HAVE_LOCALTIME_R)811tm = localtime_r(&Start, &tmbuf);812#else813tm = localtime(&Start);814#endif815Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;816Year = Month / 12;817Month = Month % 12 + 1;818return DSTcorrect(Start,819Convert(Month, (time_t)tm->tm_mday, Year,820(time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,821Timezone, DSTmaybe));822}823824/*825* Tokenizer.826*/827static int828nexttoken(const char **in, time_t *value)829{830char c;831char buff[64];832833for ( ; ; ) {834while (isspace((unsigned char)**in))835++*in;836837/* Skip parenthesized comments. */838if (**in == '(') {839int Count = 0;840do {841c = *(*in)++;842if (c == '\0')843return c;844if (c == '(')845Count++;846else if (c == ')')847Count--;848} while (Count > 0);849continue;850}851852/* Try the next token in the word table first. */853/* This allows us to match "2nd", for example. */854{855const char *src = *in;856const struct LEXICON *tp;857unsigned i = 0;858859/* Force to lowercase and strip '.' characters. */860while (*src != '\0'861&& (isalnum((unsigned char)*src) || *src == '.')862&& i < sizeof(buff)-1) {863if (*src != '.') {864if (isupper((unsigned char)*src))865buff[i++] = tolower((unsigned char)*src);866else867buff[i++] = *src;868}869src++;870}871buff[i] = '\0';872873/*874* Find the first match. If the word can be875* abbreviated, make sure we match at least876* the minimum abbreviation.877*/878for (tp = TimeWords; tp->name; tp++) {879size_t abbrev = tp->abbrev;880if (abbrev == 0)881abbrev = strlen(tp->name);882if (strlen(buff) >= abbrev883&& strncmp(tp->name, buff, strlen(buff))884== 0) {885/* Skip over token. */886*in = src;887/* Return the match. */888*value = tp->value;889return tp->type;890}891}892}893894/*895* Not in the word table, maybe it's a number. Note:896* Because '-' and '+' have other special meanings, I897* don't deal with signed numbers here.898*/899if (isdigit((unsigned char)(c = **in))) {900for (*value = 0; isdigit((unsigned char)(c = *(*in)++)); )901*value = 10 * *value + c - '0';902(*in)--;903return (tUNUMBER);904}905906return *(*in)++;907}908}909910#define TM_YEAR_ORIGIN 1900911912/* Yield A - B, measured in seconds. */913static long914difftm (struct tm *a, struct tm *b)915{916int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);917int by = b->tm_year + (TM_YEAR_ORIGIN - 1);918int days = (919/* difference in day of year */920a->tm_yday - b->tm_yday921/* + intervening leap days */922+ ((ay >> 2) - (by >> 2))923- (ay/100 - by/100)924+ ((ay/100 >> 2) - (by/100 >> 2))925/* + difference in years * 365 */926+ (long)(ay-by) * 365927);928return (days * DAY + (a->tm_hour - b->tm_hour) * HOUR929+ (a->tm_min - b->tm_min) * MINUTE930+ (a->tm_sec - b->tm_sec));931}932933/*934*935* The public function.936*937* TODO: tokens[] array should be dynamically sized.938*/939time_t940__archive_get_date(time_t now, const char *p)941{942struct token tokens[256];943struct gdstate _gds;944struct token *lasttoken;945struct gdstate *gds;946struct tm local, *tm;947struct tm gmt, *gmt_ptr;948time_t Start;949time_t tod;950long tzone;951952/* Clear out the parsed token array. */953memset(tokens, 0, sizeof(tokens));954/* Initialize the parser state. */955memset(&_gds, 0, sizeof(_gds));956gds = &_gds;957958/* Look up the current time. */959#if defined(HAVE_LOCALTIME_S)960tm = localtime_s(&local, &now) ? NULL : &local;961#elif defined(HAVE_LOCALTIME_R)962tm = localtime_r(&now, &local);963#else964memset(&local, 0, sizeof(local));965tm = localtime(&now);966#endif967if (tm == NULL)968return -1;969#if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S)970local = *tm;971#endif972973/* Look up UTC if we can and use that to determine the current974* timezone offset. */975#if defined(HAVE_GMTIME_S)976gmt_ptr = gmtime_s(&gmt, &now) ? NULL : &gmt;977#elif defined(HAVE_GMTIME_R)978gmt_ptr = gmtime_r(&now, &gmt);979#else980memset(&gmt, 0, sizeof(gmt));981gmt_ptr = gmtime(&now);982if (gmt_ptr != NULL) {983/* Copy, in case localtime and gmtime use the same buffer. */984gmt = *gmt_ptr;985}986#endif987if (gmt_ptr != NULL)988tzone = difftm (&gmt, &local);989else990/* This system doesn't understand timezones; fake it. */991tzone = 0;992if(local.tm_isdst)993tzone += HOUR;994995/* Tokenize the input string. */996lasttoken = tokens;997while ((lasttoken->token = nexttoken(&p, &lasttoken->value)) != 0) {998++lasttoken;999if (lasttoken > tokens + 255)1000return -1;1001}1002gds->tokenp = tokens;10031004/* Match phrases until we run out of input tokens. */1005while (gds->tokenp < lasttoken) {1006if (!phrase(gds))1007return -1;1008}10091010/* Use current local timezone if none was specified. */1011if (!gds->HaveZone) {1012gds->Timezone = tzone;1013gds->DSTmode = DSTmaybe;1014}10151016/* If a timezone was specified, use that for generating the default1017* time components instead of the local timezone. */1018if (gds->HaveZone && gmt_ptr != NULL) {1019now -= gds->Timezone;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#else1025gmt_ptr = gmtime(&now);1026#endif1027if (gmt_ptr != NULL)1028local = *gmt_ptr;1029now += gds->Timezone;1030}10311032if (!gds->HaveYear)1033gds->Year = local.tm_year + 1900;1034if (!gds->HaveMonth)1035gds->Month = local.tm_mon + 1;1036if (!gds->HaveDay)1037gds->Day = local.tm_mday;1038/* Note: No default for hour/min/sec; a specifier that just1039* gives date always refers to 00:00 on that date. */10401041/* If we saw more than one time, timezone, weekday, year, month,1042* or day, then give up. */1043if (gds->HaveTime > 1 || gds->HaveZone > 1 || gds->HaveWeekDay > 11044|| gds->HaveYear > 1 || gds->HaveMonth > 1 || gds->HaveDay > 1)1045return -1;10461047/* Compute an absolute time based on whatever absolute information1048* we collected. */1049if (gds->HaveYear || gds->HaveMonth || gds->HaveDay1050|| gds->HaveTime || gds->HaveWeekDay) {1051Start = Convert(gds->Month, gds->Day, gds->Year,1052gds->Hour, gds->Minutes, gds->Seconds,1053gds->Timezone, gds->DSTmode);1054if (Start < 0)1055return -1;1056} else {1057Start = now;1058if (!gds->HaveRel)1059Start -= local.tm_hour * HOUR + local.tm_min * MINUTE1060+ local.tm_sec;1061}10621063/* Add the relative offset. */1064Start += gds->RelSeconds;1065Start += RelativeMonth(Start, gds->Timezone, gds->RelMonth);10661067/* Adjust for day-of-week offsets. */1068if (gds->HaveWeekDay1069&& !(gds->HaveYear || gds->HaveMonth || gds->HaveDay)) {1070tod = RelativeDate(Start, gds->Timezone,1071gds->DSTmode, gds->DayOrdinal, gds->DayNumber);1072Start += tod;1073}10741075/* -1 is an error indicator, so return 0 instead of -1 if1076* that's the actual time. */1077return Start == -1 ? 0 : Start;1078}107910801081#if defined(TEST)10821083/* ARGSUSED */1084int1085main(int argc, char **argv)1086{1087time_t d;1088time_t now = time(NULL);10891090while (*++argv != NULL) {1091(void)printf("Input: %s\n", *argv);1092d = get_date(now, *argv);1093if (d == -1)1094(void)printf("Bad format - couldn't convert.\n");1095else1096(void)printf("Output: %s\n", ctime(&d));1097}1098exit(0);1099/* NOTREACHED */1100}1101#endif /* defined(TEST) */110211031104