The implementation of Persian (Solar Hijri) Calendar in Go

Go Persian Calendar

godoc Build Status goreportcard License

Go Persian Calendar provides functionality for conversion among Persian (Solar Hijri) and Gregorian calendars. A Julian calendar is used as an interface for all conversions. The package name is ptime and it is compatible with the package time. All months are available with both Iranian and Dari Persian names. This source code is licensed under MIT license that can be found in the LICENSE file.

Installation

This assumes you're using go modules, and have set up your go.mod file, possibly using go mod init.

  • Import it in your code:
import ptime github.com/yaa110/go-persian-calendar
  • Building using go build will now automatically get go-persian-calendar and update your go.mod

Changelog

v0.6.0

  • Support standard time format

v0.5.0

  • Add BeginningOfWeek, BeginningOfMonth and BeginningOfYear methods.
  • Format UTC timezone by Z instead of +00:00

v0.4.1

  • Refactor code

v0.4.0

  • Change module import name

v0.3.1

  • Use Go modules

v0.3

  • ptime.Iran and ptime.Afghanistan changed to ptime.Iran() and ptime.Afghanistan(), respectively.

Getting started

1- Import the package ptime. Most of the time you need to import time and fmt packages, too.

import (
    ptime "github.com/yaa110/go-persian-calendar"
    "time"
    "fmt"
)

2- Convert Gregorian calendar to Persian calendar.

// Create a new instance of time.Time
var t time.Time = time.Date(2016, time.January, 1, 12, 1, 1, 0, ptime.Iran())

// Get a new instance of ptime.Time using time.Time
pt := ptime.New(t)

// Get the date in Persian calendar
fmt.Println(pt.Date()) // output: 1394 دی 11

3- Convert Persian calendar to Gregorian calendar.

// Create a new instance of ptime.Time
var pt ptime.Time = ptime.Date(1394, ptime.Mehr, 2, 12, 59, 59, 0, ptime.Iran())

// Get a new instance of time.Time
t := pt.Time()

// Get the date in Gregorian calendar
fmt.Println(t.Date()) // output: 2015 September 24

4- Get the current time.

// Get a new instance of ptime.Time representing the current time
pt := ptime.Now(ptime.Iran())

// Get year, month, day
fmt.Println(pt.Date()) // output: 1394 بهمن 11
fmt.Println(pt.Year(), pt.Month(), pt.Day()) // output: 1394 بهمن 11

// Get hour, minute, second
fmt.Println(pt.Clock()) // output: 21 54 30
fmt.Println(pt.Hour(), pt.Minute(), pt.Second()) // output: 21 54 30

// Get Unix timestamp (the number of seconds since January 1, 1970 UTC)
fmt.Println(pt.Unix()) // output: 1454277270

// Get yesterday, today and tomorrow
fmt.Println(pt.Yesterday().Weekday()) // output: شنبه
fmt.Println(pt.Weekday()) // output: یک‌شنبه
fmt.Println(pt.Tomorrow().Weekday()) // output: دوشنبه

// Get First and last day of week
fmt.Println(pt.FirstWeekDay().Date()) // output: 1394 بهمن 10
fmt.Println(pt.LastWeekday().Date()) // output: 1394 بهمن 16

// Get First and last day of month
fmt.Println(pt.FirstMonthDay().Weekday()) // output: پنج‌شنبه
fmt.Println(pt.LastMonthDay().Weekday()) // output: جمعه

// Get First and last day of year
fmt.Println(pt.FirstYearDay().Weekday()) // output: شنبه
fmt.Println(pt.LastYearDay().Weekday()) // output: شنبه

// Get the week of month
fmt.Println(pt.MonthWeek()) // output: 3

// Get the week of year
fmt.Println(pt.YearWeek()) // output: 46

// Get the number of remaining weeks of the year
fmt.Println(pt.RYearWeek()) // output: 6

5- Format the time.

// Get a new instance of ptime.Time using Unix timestamp
pt := ptime.Unix(1454277270, 0, ptime.Iran())

fmt.Println(pt.Format("yyyy/MM/dd E hh:mm:ss a")) // output: 1394/11/11 یک‌شنبه 09:54:30 ب.ظ

// yyyy, yyy, y     year (e.g. 1394)
// yy               2-digits representation of year (e.g. 94)
// MMM              the Persian name of month (e.g. فروردین)
// MMI              the Dari name of month (e.g. حمل)
// MM               2-digits representation of month (e.g. 01)
// M                month (e.g. 1)
// rw               remaining weeks of year
// w                week of year
// W                week of month
// RD               remaining days of year
// D                day of year
// rd               remaining days of month
// dd               2-digits representation of day (e.g. 01)
// d                day (e.g. 1)
// E                the Persian name of weekday (e.g. شنبه)
// e                the Persian short name of weekday (e.g. ش)
// A                the Persian name of 12-Hour marker (e.g. قبل از ظهر)
// a                the Persian short name of 12-Hour marker (e.g. ق.ظ)
// HH               2-digits representation of hour [00-23]
// H                hour [0-23]
// kk               2-digits representation of hour [01-24]
// k                hour [1-24]
// hh               2-digits representation of hour [01-12]
// h                hour [1-12]
// KK               2-digits representation of hour [00-11]
// K                hour [0-11]
// mm               2-digits representation of minute [00-59]
// m                minute [0-59]
// ss               2-digits representation of seconds [00-59]
// s                seconds [0-59]
// ns               nanoseconds
// S                3-digits representation of milliseconds (e.g. 001)
// z                the name of location
// Z                zone offset (e.g. +03:30)

6- Format the time using standard format.

pt := ptime.Date(1394, 7, 2, 14, 7, 8, 0, Iran())

fmt.Println(pt.TimeFormat("2 Jan 2006")) // output: 2 مهر 1394

// 2006        four digit year (e.g. 1399)
// 06          two digit year (e.g. 99)
// 01          two digit month (e.g. 01)
// 1           one digit month (e.g. 1)
// Jan         month name (e.g. آذر)
// January     month name (e.g. آذر)
// 02          two digit day (e.g. 07)
// 2           one digit day (e.g. 7)
// _2          right justified two character day (e.g.  7)
// Mon         weekday (e.g. شنبه)
// Monday      weekday (e.g. شنبه)
// 03          two digit 12 hour format (e.g. 03)
// 3           one digit 12 hour format (e.g. 3)
// 15          two digit 24 hour format (e.g. 15)
// 04          two digit minute (e.g. 03)
// 4           one digit minute (e.g. 03)
// 05          two digit minute (e.g. 09)
// 5           one digit minute (e.g. 9)
// .000        millisecond (e.g. .120)
// .000000     microsecond (e.g. .123400)
// .000000000  nanosecond (e.g. .123456000)
// .999        trailing zeros removed millisecond (e.g. .12)
// .999999     trailing zeros removed microsecond (e.g. .1234)
// .999999999  trailing zeros removed nanosecond (e.g. .123456)
// PM          full 12-Hour marker (e.g. قبل از ظهر)
// pm          short 12-Hour marker (e.g. ق.ظ)
// MST         the name of location
// -0700       zone offset (e.g. +0330)
// -07         zone offset (e.g. +03)
// -07:00      zone offset (e.g. +03:30)
// Z0700       zone offset (e.g. +0330)
// Z07:00      zone offset (e.g. +03:30)

Documentation

Use GoDoc documentation for more information about methods and functionality available for ptime.Time, ptime.Month, ptime.Weekday and ptime.AmPm.

Comments
  • t.nsec length

    t.nsec length

    Hi Navid

    strconv.Itoa(t.nsec) string length rarely goes less than 6 and because of you trying to get a string with lenght 6 of it run time error happening, the problematic code is :

    "{ms}", "." + strconv.Itoa(t.nsec)[:6],

    and the error is : panic: runtime error: slice bounds out of range [:6] with length 5

    I fixed it in my fork by adding "0" charcter as needed to end of it's string.

    Good Luck.

  • add standard time format

    add standard time format

    hi your package missing standard go time format parameters! i add custom function TimeFormat function that format date using standard go time format parameters [https://yourbasic.org/golang/format-parse-string-time-date-example/]

    this pull request has no breaking-change to original repo.

    thanks!

  • پیشنهاد توسعه

    پیشنهاد توسعه

    سلام پیشنهادی دارم برای افزودن قابلیت نمایش رویداد به برنامه ای که توسعه دادید

    الان که همه ی سیستم ها به اینترنت متصل هستند پیشنهاد میکنم ارتباط با ای پی آي های تقویم رو هم در این سورس خوب پیاده سازی کنید خودم از این سرویس زیاد استفاده میکنم

    وب سرویس تقویم فارسی

    اطلاعات خوبی ارائه میده مثل وقایع و مناسبت های هر روز و دریافت رویداد در حالت شمسی و قمری و میلادی
  • FirstDayOfMonth() method

    FirstDayOfMonth() method

    I think The FirstDayOfMonth() method must be returns the first day of month and reset the time to 00:00:00, but the method only reset the date and keeps original time

  • Formatting RFC3339

    Formatting RFC3339

    The main time package when timezone is UTC formats the date in RFC3339 like this: 2019-10-10T10:20:33Z and for example for Tehran timezone formats like this: 2019-10-10T10:20:33+03:30

    But your package for UTC acts as other timezones and formats as 1398-08-15T10:20:22+00:00

    I think for consistency with main package you should format same as main time package

  • [Help wanted] Support for Persian calendar in Hugo

    [Help wanted] Support for Persian calendar in Hugo

    Hi,

    May you take a look at this topic: https://discuss.gohugo.io/t/are-native-non-gregorian-calendars-supported/2963/6 and share your info there.

    • Hugo is a written-Go static site generator.

    Cheers

  • Add Hour Name

    Add Hour Name

    hour name functionality returns the name of that part of the day based on the 0-23 hour value. [0,3) -> midnight [3,6) -> dawn [6,9) -> morning [9,12) -> before noon [12,15) -> noon [15,18) -> afternoon [18,21) -> evening [21,24) -> night

  • Fix function comments based on best practices from Effective Go

    Fix function comments based on best practices from Effective Go

    Hi, we updated some exported function comments based on best practices from Effective Go. It’s admittedly a relatively minor fix up. Does this help you?

  • IsLeap function refined

    IsLeap function refined

    Hi Navid!

    I checked your code and find out that IsLeap function doesn't work correctly for some years after 1407 and before 1371 (and probably for some other ranges)

    I refined the function and it leads to removing divider function.`

  • Name module correctly, update usage

    Name module correctly, update usage

    I failed to use the ptime package, given how you named your module. I'm actually somewhat of a beginner using go modules. It does seem to me though that the module name should be a precise import path. As in, "we're rooted here".

  • Daylight saving time

    Daylight saving time

    Can not calculate correctly Sub Days with AddDate function example set the time to 1400-01-01 and use FirstWeekDay function to return and you see return 1400-06-26 instead of 1400-06-27 I think the problem is with the time zone (+04:30 and +03:30)

Goals calendar is a Seinfeld calendar written in Google's Go (unfinished dead code)

Goals calendar ============== Goals calendar is a Seinfeld calendar written in Google's Go. Mark a red check each day you have done something for you

Jun 5, 2017
Some utilities for Persian language in Go (Golang)

persian Some utilities for Persian language in Go (Golang). Installation go get github.com/mavihq/persian API .ToPersianDigits Converts all English d

Oct 22, 2022
An anthology of a variety of tools for the Persian language in Golang
An anthology of a variety of tools for the Persian language in Golang

Persian tools An anthology of a variety of tools for the Persian language in Golang Todos Bill calculator Digits Validate Bank card number. Find Bank'

Nov 22, 2022
A library for communication with solar power inverters of the RCT power brand, not endorsed by or affiliated with the eponymous company.

rct A library for communication with solar power inverters of the RCT power brand. Tested with the RCT PS 6.0 solar power inverter, battery and grid p

Dec 21, 2022
A simple tool to extract Fronius solar data logger output and output Influx line protocol

telegraf-exec-fronius This is a simple tool to extract Fronius solar data logger output and output Influx line protocol; it is designed to be used wit

Jan 8, 2022
Calendar heatmap inspired by Github contribution activity
Calendar heatmap inspired by Github contribution activity

Self-contained, plain Go implementation of calendar heatmap inspired by Github contribution activity. $ go build $ echo '{ "2020-05-16": 8, "2

Dec 25, 2022
Simple CLI App for creating recurring google calendar events

Kronus A CLI App to help you stay in touch with the people that matter by leveraging the google calender API. You can create touchbase events for cont

Nov 7, 2022
Structural time package for jalali calendar

Jalali Structural time package for jalali calendar. This package support parse from string, json and time. Structures There are three data structures

Mar 21, 2022
An annual Advent calendar of programming puzzles
An annual Advent calendar of programming puzzles

?? Advent of Code 2021 ?? Summary Advent of Code is an annual Advent calendar of programming puzzles. This year I am doing it in Go and Python. Runnin

Jun 16, 2022
A simple microservice designed in Go using Echo Microframework for sending emails and/or calendar invitations to users.

Calenvite A simple microservice designed in GO using Echo Microframework for sending emails and/or calendar invitations to users. Features Send emails

Oct 29, 2022
Plan team's rotation on google calendar

google-rotation-planner Plan rota on google calendar. Usage Get a credentials.json file from GCloud and place it in the same folder as the executable.

Jul 4, 2022
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Advent of Code 2021 Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved

Dec 2, 2021
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star.

Dec 6, 2021
Jan 4, 2022
Namecoin calendar notification daemon (ICS, CalDAV)

nccald nccald is a simple daemon to provide calendar notifications for Namecoin name expirations. The daemon periodically queries a Namecoin Core inst

Dec 11, 2021
API for managing the release calendar

dp-release-calendar-api API for managing the release calendar Getting started Run make debug Dependencies No further dependencies other than those def

Feb 10, 2022
Go language implementation of a blockchain based on the BDLS BFT protocol. The implementation was adapted from Ethereum and Sperax implementation

BDLS protocol based PoS Blockchain Most functionalities of this client is similar to the Ethereum golang implementation. If you do not find your quest

Oct 14, 2022
CVE-2021-4034 - A Golang implementation of clubby789's implementation of CVE-2021-4034

CVE-2021-4034 January 25, 2022 | An00bRektn This is a golang implementation of C

Feb 3, 2022
An implementation of JOSE standards (JWE, JWS, JWT) in Go

Go JOSE Package jose aims to provide an implementation of the Javascript Object Signing and Encryption set of standards. This includes support for JSO

Dec 18, 2022
goRBAC provides a lightweight role-based access control (RBAC) implementation in Golang.

goRBAC goRBAC provides a lightweight role-based access control implementation in Golang. For the purposes of this package: * an identity has one or mo

Dec 29, 2022