This is a golang
implementation of the package from opensource-nepal's python
package nepali.
We'll try to include all the features provided in the aforementioned package. We'll be releasing features in parts to support all or most features given by go
's time
package.
To install this package use go get
$ go get -u github.com/opensource-nepal/go-nepali
Or if there is some error then, use go install
$ go install github.com/opensource-nepal/go-nepali
NOTE: Currently this package is in beta version, so it only includes basic features like date conversion, formatting and parsing. Also, the output is read-only.
In this package, we provide 2 go
packages, nepalitime
and dateConverter
.
-
nepalitime
: The functionalities provided innepalitime
are described below:-
To get a
NepaliTime
object:import "github.com/opensource-nepal/go-nepali/nepalitime" npTime, err := nepalitime.Date(2079, 10, 15, 14, 29, 6, 7)
The
err
object could containing error if you pass in errorneous values into theDate
function.Once you have the
NepaliTime
object, you will be able to access methods, some of which are described below:GetEnglishTime()
-> Returns the corresponding English timeDate()
-> Returns year, month and day as separate return valuesClock()
-> Returns hour, minute and second as separate return values
-
If you already have a
time.Time
object then, you can get the correspondingNepaliTime
object using the functionFromEnglishTime
:import "github.com/opensource-nepal/go-nepali/nepalitime" // just to ensure that the timezone is Asia/Kathmandu // else the time will be adjusted accordingly enTime := time.Date(2023, 1, 29, 14, 29, 6, 7, nepalitime.GetNepaliLocation()) nt, err := nepalitime.FromEnglishTime(enTime)
-
To parse a date string into a
NepaliTime
object theParse
function can be used. This is the Nepali equivalent of thetime.Parse
function of go but instead of using the time parsing format ofMon Jan 2 15:04:05 -0700 MST 2006
we decided to go with the%Y/%m/%d
style parsing. We intend on supporting the go style formatting in the upcoming releases. Please see directives section to know which directives we support.import "github.com/opensource-nepal/go-nepali/nepalitime" datetimeStr := "2079/10/14" format := "%Y/%m/%d" npTime, err := nepalitime.Parse(datetimeStr, format)
-
To get current Nepali time:
import "github.com/opensource-nepal/go-nepali/nepalitime" npTime := nepalitime.Now()
-
To get the current English time in Nepal:
import "github.com/opensource-nepal/go-nepali/nepalitime" enTime := nepalitime.GetCurrentEnglishTime()
-
To format the
NepaliTime
object to a string that you want.import ( "fmt" "github.com/opensource-nepal/go-nepali/nepalitime" ) npTime, _ := nepalitime.Date(2079, 10, 15, 14, 29, 6, 7, 123) fmt.Println(npTime.Format("%Y/%m/%d %H:%M:%S:%f"))
Please see directives section to know which directives we support.
-
-
dateConverter
: The functionalities provided indateConverter
are described below:-
This is one of the core functionalities in which an English date(not an object) is converted to Nepali date in parts i.e. year, month, day in an array:
import "github.com/opensource-nepal/go-nepali/dateConverter" // npDate is an array of 3 length which contains // year, month and day sequential form npDate, err := dateConverter.EnglishToNepali(2023, 1, 28)
-
To convert a Nepali date(not an object) to English date in parts i.e. year, month, day in an array:
import "github.com/opensource-nepal/go-nepali/dateConverter" // enDate is an array of 3 length which contains // year, month and day sequential form enDate, err := dateConverter.NepaliToEnglish(2087, 8, 10)
-
Directive | Meaning | Example |
---|---|---|
%d |
Day of the month as a zero-padded decimal number. | 01, 02, …, 31 |
%-d |
Day of the month as a decimal number. | 1, 2, …, 31 |
%m |
Month as a zero-padded decimal number. | 01, 02, …, 12 |
%-m |
Month as a decimal number. | 1, 2, …, 12 |
%y |
Year without century as a zero-padded decimal number. | 00, 01, …, 99 |
%-y |
Year without century as a decimal number. | 0, 1, …, 99 |
%Y |
Year with century as a zero-padded decimal number. | 0001, 0002, …, 2078, 2079, …, 9998, 9999 |
%-Y |
Year with century as a decimal number. | 1, 2, …, 2078, 2079, …, 9998, 9999 |
%H |
Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, …, 23 |
%-H |
Hour (24-hour clock) as a decimal number. | 0, 1, …, 23 |
%I |
Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, …, 12 |
%-I |
Hour (12-hour clock) as a decimal number. | 1, 2, …, 12 |
%p |
Locale’s equivalent of either AM or PM. | AM, PM (en_US); am, pm (de_DE) |
%M |
Minute as a zero-padded decimal number. | 00, 01, …, 59 |
%-M |
Minute as a decimal number. | 0, 1, …, 59 |
%S |
Second as a zero-padded decimal number. | 00, 01, …, 59 |
%-S |
Second as a decimal number. | 0, 1, …, 59 |
%f |
Nanosecond as a decimal number, zero-padded to 6 digits. | 000000, 000001, …, 999999 |
%-f |
Nanosecond as a decimal number. | 0, 1, …, 999999 |
%% |
A literal '%' character. |
% |
We appreciate feedback and contribution to this package. To get started please see our contribution guide