longevity

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 10 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

Examples

Constants

View Source
const (
	ISO_FORMAT               = "2006-01-02"
	Float64EqualityThreshold = 1e-9
)

Variables

This section is empty.

Functions

func AlmostEqual

func AlmostEqual(a, b float64) bool

AlmostEqual compares two float64 numbers

func FormatPostData

func FormatPostData(sex string, dob time.Time) string

FormatPostData creates a dictionary of the data that will be used in the POST method

func GetDateFields

func GetDateFields(dob time.Time) (monthOfBirth, dayOfBirth, yearOfBirth string)

GetDateFields extracts date fields and formats them in the way the cgi program expects them.

Output fields:

  • monthofbirth is the month field minus 1 (0-11)
  • dayofbirth is the day field with leading zero if less than 10
  • yearofbirth is a 4-digit year field These fields are returned as tuple of 3 strings

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

func PostRequest

func PostRequest(postData string) (string, error)

PostRequest posts the request and gets the response.

Parameters postdata: a string containing the parameters, e.g., for Keith Richards:

  • "sex" : "m",
  • "monthofbirth": "11", # Note: website expects actual month - 1
  • "dayofbirth": "18",
  • "yearofbirth": "1943",

Returns: a string containing the returned HTML

Separated from the mainline so that this function can be mocked and test data used instead of a live call to the URL.

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 Get

func Get(sex string, dob time.Time) (Response, error)

Get issues a POST request to the website, parses the response, and returns the results.

Parameters: - sex: either "m" or "f" - dob: date of birth

Returns: - The response

Example

This example illustrates how to use the longevity package in a standalone mainline

const ISO_FORMAT = "2006-01-02"
sex := "m"
dob, _ := time.Parse(ISO_FORMAT, "1943-12-18")
// Invoke the requester
resp, err := Get(sex, dob)
if err != nil {
	fmt.Fprintln(os.Stderr, err.Error())
	return
}
currentAge := resp.CurrentAge
additionalYears := resp.AdditionalYears
totalYears := resp.TotalYears
deathDate := resp.DeathDate
deathYear, deathMonth, deathDay := deathDate.Date()

fmt.Printf("current age      = %.1f\n", currentAge)
fmt.Printf("additional years = %.1f\n", additionalYears)
fmt.Printf("total years      = %.1f\n", totalYears)
fmt.Printf("expiration date  = %4d-%02d-%02d\n", deathYear, deathMonth, deathDay)

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