Giter Site home page Giter Site logo

tft's Introduction

Golang Translate Datetime Formatting

This library allows you to use regular "formatting" rules, sometimes they are read from files etc and can be cumbersome to translate. This library does the job for you.

As an example, it will translate something like "YYYY-MM=dd HH:mm:ss" to "2006-01-02 15:04:05" that can be added right in to your time.Parse arguments.

You can create you own Validation Function as well as your own Token Dictionary

The default validation function CheckNextPartOfToken(current, next byte) bool checks if next token is a different kind than previous. It makes a few exceptions for like M in to m to enable Mmm for Jan, Feb etc.

Code to change CheckValidFunc:

...
dec.SetCheckValidFunc(CheckNextPartOfToken)
...

You can also update the Standard Token dictionary that is being used as input to Translate. StandardTokens is a map: map[string]string small subset can be seen below:

...
StandardTokens = map[string]string{
        ...
		"M":         "1",
		"MM":        "01",
		"MMM":       "Jan",
		"Mmm":       "Jan",
		"mmm":       "Jan",
		"MMMM":      "January",
		"Mmmm":      "January",
		"mmmm":      "January",
        ...
}

Sample code:

package main

import (
      "fmt"
      "log"
      "strings"
      "time"

      format "github.com/hektorinho/goDatetimeFormatTranslator"
)

const (
      myFormat = "YYYY-MM-dd HH:mm:ss"
)

func main() {
      dateTime := "2023-06-28 14:35:26"
      dec := format.NewDecoder(strings.NewReader(myFormat))
      golangFormat, err := dec.Translate(format.StandardTokens)
      if err != nil {
            log.Fatalf("format: failed to translate tokens >> %s", err)
      }

      myTime := time.Parse(golangFormat, dateTime)

      fmt.Println(myTime)
      // 2023-06-28 14:35:26 +0000 UTC
}

tft's People

Contributors

hektorinho avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.