🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #92423

Re: INN laxmid clarification

#92423
From: Stefan Ram
Date: Thu, 04 Sep 2025 19:25
240 lines
8700 bytes
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
> * ctime_parser.c

  Documentation:

  Thank you for selecting this parser! By doing so, you have chosen
  a reliable and carefully written tool for handling ctime(3) date
  strings. This document serves as the reference and user guide
  to help you make full and confident use of the library.

  The ctime(3) Format

  The C standard library provides the function ctime(3), which takes a
  time_t value and produces a human-readable string representing local
  time. The format of the resulting string is fixed and well-known:

"Wed Jun 30 21:49:08 1993\n"

  Breaking it down:

  - Day of week: Three-letter abbreviation (Sun .. Sat)

  - Month: Three-letter abbreviation (Jan .. Dec)

  - Day of month: Either space + digit (for 1–9) or two digits (10–31)

  - Time: hh:mm:ss, with leading zeroes

  - Year: Four digits

  - Newline: Always present at the end

  This is the same layout produced by asctime(3). Many programs
  rely on this representation, and having a robust parser for
  it is therefore useful. 

  What is a Parser?

  A parser is a piece of software that reads input according
  to a grammar and produces structured information from it.
  In other words, instead of handling raw text, a parser lets
  you work with well-defined fields (weekday, month, day, time,
  year) as integers or codes.

  What This Parser Does

  This parser accepts a string in strict ctime(3) format and extracts
  the following fields:

  - weekday (0 = Sunday … 6 = Saturday)

  - month (1 = January … 12 = December)

  - day (1–31)

  - hour (0–23)

  - minute (0–59)

  - second (0–60, allowing for leap seconds)

  - year (integer, typically four digits)

  It also provides status information, so your program knows
  whether the parse succeeded or failed, and why. 

  Preparing Input for the Parser

  The parser expects a C string (const char *) that contains the
  exact format produced by ctime(3). Common ways to obtain such
  a string include:

  - Obtaining one directly from ctime() or asctime().

  - Reading a log file that stores ctime-style timestamps.

  - Generating the format in a controlled environment for testing.

  Make sure the input is complete, including the final newline,
  before passing it to the parser. 

  The Main Interface

  The entry point is:

CTimeFields parse_ctime(const char *s);

  Parameter:

  - s is a NUL-terminated C string in ctime(3) format.

  Result:

  - A CTimeFields struct with the following fields:

typedef struct {
    int weekday;   /* 0=Sun .. 6=Sat */
    int month;     /* 1=Jan .. 12=Dec */
    int day;       /* 1..31 */
    int hour;      /* 0..23 */
    int minute;    /* 0..59 */
    int second;    /* 0..60 */
    int year;      /* typically four digits */
    CTimeStatus status; /* parse result */
} CTimeFields;

  Status codes (CTimeStatus):

  - CTIME_OK — parsing was successful.

  - CTIME_ERR_SYNTAX — format was invalid.

  - CTIME_ERR_RANGE — a numeric field was outside its legal range.

  You should always check the status field before using the parsed
  values.

  Structure of the Implementation

  The parser is recursive-descent style, directly matching
  the structure of the ctime(3) grammar. Each grammatical unit
  (such as day of week, month, day number, or time fields) has
  a dedicated parsing function. A helper Parser object carries
  the input position and result reference.

  This approach keeps the code straightforward, predictable,
  and clear to follow, closely reflecting the format we
  expect. Resource Management

  This parser does not perform any dynamic memory allocation.

  No malloc() or free() calls are involved. The entire parse is
  stack-based and returns results by value.

  This means:

  - No need to release resources after a parse.

  - Thread-safety as long as you provide independent input strings.

  Example Usage

  Here is a simple example that shows typical use:

#include <stdio.h>

int main(void) {
    const char *s = "Wed Jun 30 21:49:08 1993\n";
    CTimeFields f = parse_ctime(s);

    if (f.status == CTIME_OK) {
        printf("Parsed successfully:\n");
        printf("  Weekday: %d\n", f.weekday);
        printf("  Month:   %d\n", f.month);
        printf("  Day:     %d\n", f.day);
        printf("  Time:    %02d:%02d:%02d\n",
               f.hour, f.minute, f.second);
        printf("  Year:    %d\n", f.year);
    } else {
        printf("Parse failed, status=%d\n", f.status);
    }
}

  Source Code Walkthrough

  The file is organized into sections:

  - Status and result definitions

      * Enumerations for error codes.

      * The CTimeFields struct.

  - Parser support

      * Utilities to check expected characters, set error status, 
        and extract fixed-digit numbers.

  - Grammar routines

        parse_day_of_week, parse_month, parse_day, etc., each 
        reflecting the grammar rules.

  - Top-level function

        parse_ctime() orchestrates all parts in sequence.

  - Optional test harness

        Enabled via #define TEST_CTIME_PARSER.

  This design makes it easy both to use and to maintain.

  Additional Notes

  - Input must contain the final newline; otherwise, 
    the parser will reject it.

  - The parser assumes ASCII-compatible encoding.

  - Leap seconds (xx:xx:60) are accepted for completeness.

  - Range checks are included for day values, hours, minutes, and seconds.

  Grammar for ctime(3) strings


<ctime-string>  ::=
<day-of-week> " " <month> " " <day> " " <time> " " <year> "\n"

<day-of-week>   ::= "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat"

<month>         ::= "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun"
                  | "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec"

<day>           ::=
  " " <digit>
| <digit><digit>
(* range  1–31, first form has a leading space when <digit> is 1–9 *)

<time>          ::= <hour> ":" <minute> ":" <second>

<hour>          ::= <digit><digit>   (* 00–23 *)
<minute>        ::= <digit><digit>   (* 00–59 *)
<second>        ::= <digit><digit>   (* 00–60, leap second allowed *)

<year>          ::= <digit><digit><digit><digit>

<digit>         ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

  Closing Remarks

  This library provides a practical and dependable way to convert
  ctime(3) strings into structured fields. The code has been
  designed with simplicity and clarity in mind.

  We wish you success in your projects, confident use of this
  parser, and enjoyment in knowing that date and time strings
  no longer pose a challenge.
Technical Details
Message-ID: <Documentation-20250904202437@ram.dialup.fu-berlin.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.pugleaf.net!ygg-srl.newsdeef.eu!srl.newsdeef.eu!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
References: <u90srm$2mhb$1@nnrp.usenet.blueworldhosting.com> <10906hu$2tvn$1@nnrp.usenet.blueworldhosting.com> <10910cj$12r8f$1@news.trigofacile.com> <87bjnv4ait.fsf@hope.eyrie.org> <10928if$3djku$1@dont-email.me> <1092n83$2c6e$1@nnrp.usenet.blueworldhosting.com> <1094pjt$15nu2$1@news.trigofacile.com> <grammar-20250904190228@ram.dialup.fu-berlin.de> <reply-20250904193120@ram.dialup.fu-berlin.de>

Show Full Headers
Complete Article Headers
Message-ID: <Documentation-20250904202437@ram.dialup.fu-berlin.de>
Subject: Re: INN laxmid clarification
Date: Thu, 04 Sep 2025 19:25:22 +0000
From: ram@zedat.fu-berlin.de (Stefan Ram)
References: <u90srm$2mhb$1@nnrp.usenet.blueworldhosting.com> <10906hu$2tvn$1@nnrp.usenet.blueworldhosting.com> <10910cj$12r8f$1@news.trigofacile.com> <87bjnv4ait.fsf@hope.eyrie.org> <10928if$3djku$1@dont-email.me> <1092n83$2c6e$1@nnrp.usenet.blueworldhosting.com> <1094pjt$15nu2$1@news.trigofacile.com> <grammar-20250904190228@ram.dialup.fu-berlin.de> <reply-20250904193120@ram.dialup.fu-berlin.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.pugleaf.net!ygg-srl.newsdeef.eu!srl.newsdeef.eu!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
Newsgroups: news.software.nntp
Organization: Stefan Ram
Lines: 241
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de dw02+8eZuCQNStZQjprISwyE87kLBOpGxHukRwjwfFsQQH
Cancel-Lock: sha1:Eq2G6bba6m7bsAl+eCn8lylX9pg= sha256:J3CaVGKaOJyy4RSn69CY4Eo/tDMKNEJoVqHrdxVubPU=
X-Copyright: (C) Copyright 2025 Stefan Ram. All rights reserved.
 Distribution through any means other than regular usenet
 channels is forbidden. It is forbidden to publish this
 article in the Web, to change URIs of this article into links,
    and to transfer the body without this notice, but quotations
    of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
 services to mirror the article in the web. But the article may
 be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en