longevity

package module
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 11 Imported by: 0

README

longevity

Go Report Card PkgGoDev

Calculates life expectancy according to the Social Security Administration website.

References:

Gists:

Documentation

Overview

A web scraper for the Social Security Administration's life expectancy calculation.

Given a gender code ("m" or "f") and a date of birth in YYYY-MM-DD format, it will do a POST request to the Social Security Administrations website using the format defined by its [Life Expectancy Calculator], parse the response, and return the result.

Index

Constants

View Source
const (
	ISO_FORMAT               = "2006-01-02"
	Float64EqualityThreshold = 1e-9
)
View Source
const (
	SSA_URL = "https://www.ssa.gov/cgi-bin/longevity.cgi"
	TIMEOUT = 15
)

Variables

This section is empty.

Functions

func AlmostEqual

func AlmostEqual(a, b float64) bool

AlmostEqual compares two float64 numbers

func DoRequest added in v1.5.0

func DoRequest(sex string, dob time.Time) (string, error)

DoRequest accepts the two required parameters (sex and date of birth) and issues a POST request to the SSA website. It then parses the HTML returned and creates a Response object with what it parses.

func ParseCurrentAge

func ParseCurrentAge(currentAgeString string) float64

ParseCurrentAge finds an age value in a string. This can be a simple integer: "68" or a combination of years and month(s): ("68 and 3 months")

func ParseFloat

func ParseFloat(s string) float64

ParseFloat converts a string to a float, ignoring any errors

Types

type Response

type Response struct {
	CurrentAge      float64
	AdditionalYears float64
	TotalYears      float64
	DeathDate       time.Time
}

Response is a structure that contains the results of parsing a response.

func ParseResponse

func ParseResponse(html string) (Response, error)

ParseResponse parses the response HTML to get the desired output. The section of interest is this:

Inside <table ... summary="life expectancy table" after the first <tr>...</tr> ...

<tr><td style="text-align:left">68 and 6 months<sup>a</sup></td>
<td>16.5</td>
<td>85.0</td></tr>

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL