Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
Fix Your timezone Get Server TimeZone (#49)
Browse files Browse the repository at this point in the history
* Fix Your timezone Get Server TimeZone

* Fix TimeZone Issue For Other Apis

* fixup! Fix TimeZone Issue For Other Apis
  • Loading branch information
amrmostafa800 authored Jan 27, 2024
1 parent 775f316 commit 42d5040
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 38 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/xdev.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions api/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func timeAPI(app *fiber.App) {
if err != nil {
return c.JSON(Response{Success: false, Message: err.Error()})
}
time := internal.ConvertTimeFromEpoch(epoch, internal.ToDateFormat)
var timeZone = c.Query("timezone")
time := internal.ConvertTimeFromEpoch(epoch, internal.ToDateFormat, &timeZone)
return c.JSON(Response{Success: true, Data: map[string]interface{}{"time": time}})
})
app.Post(APIPrefix+TimePath+"/datetime", func(c fiber.Ctx) error {
Expand All @@ -40,14 +41,16 @@ func timeAPI(app *fiber.App) {
if err != nil {
return c.JSON(Response{Success: false, Message: err.Error()})
}
time, err := internal.ConvertTimeFromFormat(dateTimeRequest.DateTime, internal.ParseFormat("dd-MM-yyyy HH:mm:ss"), internal.ToDateFormat)
var timeZone = c.Query("timezone")
time, err := internal.ConvertTimeFromFormat(dateTimeRequest.DateTime, internal.ParseFormat("dd-MM-yyyy HH:mm:ss"), internal.ToDateFormat, &timeZone)
if err != nil {
return c.JSON(Response{Success: false, Message: err.Error()})
}
return c.JSON(Response{Success: true, Data: map[string]interface{}{"time": time}})
})
app.Get(APIPrefix+TimePath, func(c fiber.Ctx) error {
time := internal.Now(internal.ToDateFormat)
var timeZone = c.Query("timezone")
time, _ := internal.Now(internal.ToDateFormat, &timeZone)
return c.JSON(Response{Success: true, Data: map[string]interface{}{"time": time}})
})
}
6 changes: 3 additions & 3 deletions cmd/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var timeCmd = &cobra.Command{
format := internal.ParseFormat(cmd.Flag("format").Value.String())
from := cmd.Flag("from").Value.String()
if len(args) == 0 {
fmt.Println(internal.Now(format))
fmt.Println(internal.Now(format, nil))
return
}
if from == "epoch" {
Expand All @@ -58,13 +58,13 @@ var timeCmd = &cobra.Command{
fmt.Println(err)
return
}
fmt.Println(internal.ConvertTimeFromEpoch(epoch, internal.ToDateFormat))
fmt.Println(internal.ConvertTimeFromEpoch(epoch, internal.ToDateFormat, nil))
} else {
if from == "" {
fmt.Println("Please specify the from format")
return
}
time, err := internal.ConvertTimeFromFormat(args[0], from, internal.ToDateFormat)
time, err := internal.ConvertTimeFromFormat(args[0], from, internal.ToDateFormat, nil)
if err != nil {
fmt.Println(err)
return
Expand Down
6 changes: 3 additions & 3 deletions cmd/yaml2properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

var yaml2propertiesCmd = &cobra.Command{
Use: "yaml2properties",
Use: "yaml2properties",
Aliases: []string{"y2p"},
Short: "Convert YAML to Java Properties, Alias: y2p",
Long: `Convert YAML to Java Properties`,
Short: "Convert YAML to Java Properties, Alias: y2p",
Long: `Convert YAML to Java Properties`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a YAML string.")
Expand Down
2 changes: 1 addition & 1 deletion internal/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ func DecodeFromBase64(input string) string {
return ""
}
return string(decoded)
}
}
2 changes: 1 addition & 1 deletion internal/base64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ func TestEncodeToBase64(t *testing.T) {
if result2 != expected2 {
t.Errorf("Expected encoded string to be %q, but got %q", expected2, result2)
}
}
}
2 changes: 1 addition & 1 deletion internal/guid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import "github.com/google/uuid"

func GenerateGUID() string {
return uuid.NewString()
}
}
6 changes: 4 additions & 2 deletions internal/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package internal
import (
"testing"
)

const (
EXPECTED_ERROR = "Expected error '%s', but got:\n%s"
EXPECTED_ERROR = "Expected error '%s', but got:\n%s"
UNEXPECTED_ERROR = "Unexpected error: %v"
)

func TestIndentJSON(t *testing.T) {
input := `{"name":"John","age":30,"city":"New York"}`
expectedOutput := `{
Expand Down Expand Up @@ -79,4 +81,4 @@ city: New York
if err != nil && err.Error() != "unexpected EOF" {
t.Errorf(EXPECTED_ERROR, "unexpected EOF", err)
}
}
}
2 changes: 1 addition & 1 deletion internal/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ func TestEncodeJWT(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
}
2 changes: 1 addition & 1 deletion internal/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ func TestProperties2Yaml(t *testing.T) {
assert.Equal(t, tt.expectedYaml, result)
})
}
}
}
41 changes: 35 additions & 6 deletions internal/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,49 @@ func (t Time) String() string {
return fmt.Sprintf("UTC: %s\nYour Timezone: %s\nEpoch: %d", t.UTC, t.YourTimezone, t.Epoch)
}

func Now(format string) Time {
func Now(format string, timeZone *string) (Time, error) {
t := time.Now()
return Time{UTC: t.UTC().Format(format), YourTimezone: t.Format(format), Epoch: t.Unix()}

if timeZone == nil {
return Time{UTC: t.UTC().Format(format), YourTimezone: t.Format(format), Epoch: t.Unix()}, nil
}

location, err := time.LoadLocation(*timeZone)
if err != nil {
return Time{}, err
}
return Time{UTC: t.UTC().Format(format), YourTimezone: t.In(location).Format(format), Epoch: t.Unix()}, nil
}

func ConvertTimeFromEpoch(epoch int64, format string) Time {
func ConvertTimeFromEpoch(epoch int64, format string, timeZone *string) Time {
t := time.Unix(epoch, 0)
return Time{UTC: t.UTC().Format(format), YourTimezone: t.Format(format), Epoch: t.Unix()}

if timeZone == nil {
return Time{UTC: t.UTC().Format(format), YourTimezone: t.Format(format), Epoch: t.Unix()}
}

location, err := time.LoadLocation(*timeZone)
if err != nil {
return Time{}
}

return Time{UTC: t.UTC().Format(format), YourTimezone: t.In(location).Format(format), Epoch: t.Unix()}
}

func ConvertTimeFromFormat(datetime string, fromFormat string, toFormat string) (Time, error) {
func ConvertTimeFromFormat(datetime string, fromFormat string, toFormat string, timeZone *string) (Time, error) {
t, err := time.ParseInLocation(fromFormat, datetime, time.Local)
if err != nil {
return Time{}, err
}
return Time{UTC: t.UTC().Format(toFormat), YourTimezone: t.Format(toFormat), Epoch: t.Unix()}, nil

if timeZone == nil {
return Time{UTC: t.UTC().Format(toFormat), YourTimezone: t.Format(toFormat), Epoch: t.Unix()}, nil
}

location, err := time.LoadLocation(*timeZone)
if err != nil {
return Time{}, err
}

return Time{UTC: t.UTC().Format(toFormat), YourTimezone: t.In(location).Format(toFormat), Epoch: t.Unix()}, nil
}
14 changes: 7 additions & 7 deletions internal/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNow(t *testing.T) {
expectedYourTimezone := time.Now().Format(format)
expectedEpoch := time.Now().Unix()

result := Now(format)
result, _ := Now(format, nil)

// Check the UTC time
if result.UTC != expectedUTC {
Expand All @@ -40,7 +40,7 @@ func TestConvertTimeFromEpoch(t *testing.T) {
expectedYourTimezone := expected.Local().Format(format) // Replace with your expected time in your timezone
expectedEpoch := expected.Local().Unix() // Replace with your expected epoch value

result := ConvertTimeFromEpoch(epoch, format)
result := ConvertTimeFromEpoch(epoch, format, nil)

// Check the UTC time
if result.UTC != expectedUTC {
Expand All @@ -62,12 +62,12 @@ func TestConvertTimeFromFormat(t *testing.T) {
toFormat := "01-02-2006 15:04:05"
format := ParseFormat("yyyy-MM-dd HH:mm:ss")

expected, _ := time.ParseInLocation(format, datetime, time.Local) // Check if the expected UTC time is valid
expectedUTC := expected.UTC().Format(toFormat) // Replace with your expected UTC time
expectedYourTimezone := expected.Local().Format(toFormat) // Replace with your expected time in your timezone
expected, _ := time.ParseInLocation(format, datetime, time.Local) // Check if the expected UTC time is valid
expectedUTC := expected.UTC().Format(toFormat) // Replace with your expected UTC time
expectedYourTimezone := expected.Local().Format(toFormat) // Replace with your expected time in your timezone
expectedEpoch := expected.Local().Unix()

result, err := ConvertTimeFromFormat(datetime, format, toFormat)
result, err := ConvertTimeFromFormat(datetime, format, toFormat, nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand All @@ -87,7 +87,7 @@ func TestConvertTimeFromFormat(t *testing.T) {
t.Errorf("Expected epoch value %d, but got %d", expectedEpoch, result.Epoch)
}

result, err = ConvertTimeFromFormat("xyz", "123", toFormat)
result, err = ConvertTimeFromFormat("xyz", "123", toFormat, nil)
if err == nil {
t.Errorf("Unexpected error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import "github.com/oklog/ulid/v2"

func GenerateULID() string {
return ulid.Make().String()
}
}
2 changes: 1 addition & 1 deletion internal/ulid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ func TestGenerateULID(t *testing.T) {
if len(ulid) != 26 {
t.Errorf("Expected ULID length to be 26, but got %d", len(ulid))
}
}
}
4 changes: 2 additions & 2 deletions internal/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package internal
import "net/url"

func EncodeURL(s string) string {
return url.QueryEscape(s)
return url.QueryEscape(s)
}

func DecodeURL(s string) (string, error) {
return url.QueryUnescape(s)
}
}
2 changes: 1 addition & 1 deletion internal/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ func TestDecodeURL(t *testing.T) {
if output3 != expected3 {
t.Errorf("Expected decoded URL to be %s, but got %s", expected3, output3)
}
}
}
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main

Expand Down
11 changes: 8 additions & 3 deletions server/ui/time.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
}
function epcoh() {
$.ajax({
url: "/api/time/epoch",
url: `/api/time/epoch?timezone=${_GetCurrentTimeZone()}`,
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
Expand All @@ -83,7 +83,7 @@
}
function datetime(){
$.ajax({
url: "/api/time/datetime",
url: `/api/time/datetime?timezone=${_GetCurrentTimeZone()}`,
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
Expand All @@ -102,7 +102,7 @@
}
function now(){
$.ajax({
url: "/api/time",
url: `/api/time?timezone=${_GetCurrentTimeZone()}`,
type: 'get',
success: function (data) {
if (!data.success) {
Expand All @@ -118,4 +118,9 @@
$(document).ready(function () {
now();
});

function _GetCurrentTimeZone()
{
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
</script>

0 comments on commit 42d5040

Please sign in to comment.