Mrinal Raj

Mrinal Raj

1602635700

Introducing kotlinx-datetime by Ilya Gorbunov

Recording brought to you by American Express. https://americanexpress.io/kotlin-jobs

Join the kotlinx-datetime library talk to learn about different types of time and how our library helps you cope with them. Sometimes this is simple, but sometimes it can be really tricky.

#kotlin #java #programming #developer #mobile-apps

What is GEEK

Buddha Community

Introducing kotlinx-datetime by Ilya Gorbunov
Mrinal Raj

Mrinal Raj

1602635700

Introducing kotlinx-datetime by Ilya Gorbunov

Recording brought to you by American Express. https://americanexpress.io/kotlin-jobs

Join the kotlinx-datetime library talk to learn about different types of time and how our library helps you cope with them. Sometimes this is simple, but sometimes it can be really tricky.

#kotlin #java #programming #developer #mobile-apps

August  Larson

August Larson

1624286940

Datetime module in Python

A guide to understanding the datetime module in Python.

Time

This module helps deal with timestamps. The time class has attributes like hour, minute, second and microsecond.

>>> import datetime

Let’s create a timestamp. The arguments are in the following order — hour, minutes, seconds, microseconds, time zone information.

>>> t1 = datetime.time(6, 4, 2)

>>> print(t1)
06:04:02

As you can there are various methods which can be used.

>>> t1.hour
6

>>> t1.minute
4
>>> t1.microsecond
0
>>> print(t1.tzinfo)
None

We can also get the minimum and maximum time we can have.

>>> print(datetime.time.min)
00:00:00

>>> print(datetime.time.max)
23:59:59.999999

#programming #coding #python3 #datetime #python #datetime module in python

Adelle  Hackett

Adelle Hackett

1589912280

Introducing MongoDB Developer Hub

We are excited to announce the launch of our new Developer Hub: https://developer.mongodb.com. The Developer Hub will serve as the central location for all MongoDB developer content, programs, and resources. We built the Developer Hub from the ground up to serve the needs of developers using MongoDB products like MongoDB Atlas and MongoDB Stitch.

#introducing #mongodb #developer

Gostradamus: Better DateTimes for Go

Gostradamus: Better DateTimes for Go  

Introduction

Gostradamus is a Go library that offers a lightweight and human-friendly way to create, transform, format, and parse datetimes. It uses the underlying Go time library and the main gostradamus' type DateTime can be easily converted to and from time.Time.

Gostradamus is named after the french pharmacist Nostradamus. He is known for his prophecies, therefore he worked a lot with time, like Gostradamus.

Features

✅ Easy conversion between time.Time and gostradamus.DateTime

✅ Format with common and known format tokens like YYYY-MM-DD HH:mm:ss

✅ Generates time spans, floors, ceilings from second to year (weeks included)

✅ Weeks manipulation and helper functions

✅ Timezone-aware with conversion

✅ Fully tested and ready for production

Basic Usage

package main

import "github.com/bykof/gostradamus"

func main() {
    // Easy parsing
    dateTime, err := gostradamus.Parse("14.07.2017 02:40:00", "DD.MM.YYYY HH:mm:ss")
    if err != nil {
        panic(err)
    }
    
    // Easy manipulation 
    dateTime = dateTime.ShiftMonths(-5).ShiftDays(2)
    
    // Easy formatting
    println(dateTime.Format("DD.MM.YYYY HH:mm:ss"))
    // 16.02.2017 02:40:00
    
    // Easy helper functions
    start, end := dateTime.SpanWeek()
    
    println(start.String(), end.String())
    // 2017-02-13T00:00:00.000000Z 2017-02-19T23:59:59.999999Z
}

Table of Contents

Usage

This part introduces all basic features of gostradamus. Surely there are more, just look them up in the offical documentation.

Types

There are two types in this package, which are important to know:

type DateTime time.Time
type Timezone string

DateTime contains all the creation, transforming, formatting and parsing functions.

Timezone is just a string type but gostradamus has all timezones defined as constants. Look here.

Conversion between time.Time and gostradamus.DateTime

You can easily convert between gostradamus.DateTime and time.Time package. Either with helper functions or with golang's type conversion

import "time"

// From gostradamus.DateTime to time.Time
dateTime := gostradamus.Now()
firstTime := dateTime.Time()
secondTime := time.Time(dateTime)

// From time.Time to gostradamus.DateTime
t := time.Now()
dateTime = gostradamus.DateTimeFromTime(t)
dateTime = gostradamus.DateTime(t)

Creation

If you want to create a gostradamus.DateTime you have several ways:

Just create the DateTime from scratch:

// Create it with a defined timezone as you know it
dateTime := gostradamus.NewDateTime(2020, 1, 1, 12, 0, 0, 0, gostradamus.EuropeBerlin)

// Create it with predefined UTC timezone
dateTime := gostradamus.NewUTCDateTime(2020, 1, 1, 12, 0, 0, 0)

// Create it with local timzone
dateTime := gostradamus.NewLocalDateTime(2020, 1, 1, 12, 0, 0, 0)

Or create a DateTime from an ISO-8601 format:

dateTime := gostradamus.Parse("2017-07-14T02:40:00.000000+0200", gostradamus.Iso8601)

Or from a custom format:

dateTime := gostradamus.Parse("10.02.2010 14:59:53", "DD.MM.YYYY HH:mm:ss")

Or an UNIX timestamp for example:

dateTime := gostradamus.FromUnixTimestamp(1500000000)

Or different ways of the current datetime:

// Current DateTime in local timezone
dateTime := gostradamus.Now()

// Current DateTime in UTC timezone
dateTime = gostradamus.UTCNow()

// Current DateTime in given timezone
dateTime = gostradamus.NowInTimezone(gostradamus.EuropeParis)

Timezones

Feel free to use all available timezones, defined here:

gostradamus.EuropeParis // Europe/Paris
gostradamus.EuropeBerlin // Europe/Berlin
gostradamus.AmericaNewYork // America/New_York
... and many more

Convert between timezones easily:

dateTime := gostradamus.NewUTC(2020, 1, 1, 12, 12, 12, 0).InTimezone(gostradamus.EuropeBerlin)
println(dateTime.String())
// 2020-02-15T13:12:12.000000+0100

dateTime = dateTime.InTimeZone(America_New_York)
println(dateTime.String())
// 2020-02-15T07:12:12.000000-0500

Shift

Shifting helps you to add or subtract years, months, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds.

To add a value use positive integer, to subtract use negative integer.

dateTime := gostradamus.NewUTCDateTime(2020, 1, 1, 1, 1, 1, 1)
dateTime = dateTime.ShiftYears(10)
println(dateTime.String())
// 2030-01-01T01:01:01.000000+0000

dateTime = gostradamus.NewUTCDateTime(2020, 1, 1, 1, 1, 1, 1)
dateTime = dateTime.ShiftDays(-10)
println(dateTime.String())
// 2019-12-22T01:01:01.000000+0000

dateTime = gostradamus.NewUTCDateTime(2020, 1, 1, 1, 1, 1, 1)
dateTime = dateTime.ShiftWeeks(2)
println(dateTime.String())
// 2020-01-15T01:01:01.000000+0000

dateTime = gostradamus.NewUTCDateTime(2020, 1, 1, 1, 1, 1, 1)
dateTime = dateTime.Shift(0, 1, 10, 0, 0, 0, 0)
println(dateTime.String())
// 2020-02-11T01:01:01.000000+0000

Replace

Replacing values can be done easily.

dateTime := gostradamus.NewUTCDateTime(2020, 1, 1, 1, 1, 1, 1)
dateTime = dateTime.ReplaceYear(2010)
println(dateTime.String())
// 2010-01-01T01:01:01.000000+0000

dateTime = gostradamus.NewUTCDateTime(2020, 1, 1, 1, 1, 1, 1)
dateTime = dateTime.ReplaceYear(2010).ReplaceMonth(2)
println(dateTime.String())
// 2010-02-01T01:01:01.000000+0000

Token Table

 TokenOutput
YearYYYY2000, 2001, 2002 … 2012, 2013
 YY00, 01, 02 … 12, 13
MonthMMMMJanuary, February, March …
 MMMJan, Feb, Mar …
 MM01, 02, 03 … 11, 12
 M1, 2, 3 … 11, 12
Day of YearDDDD001, 002, 003 … 364, 365
Day of MonthDD01, 02, 03 … 30, 31
 D1, 2, 3 … 30, 31
Day of WeekddddMonday, Tuesday, Wednesday …
 dddMon, Tue, Wed …
HourHH00, 01, 02 … 23, 24
 hh01, 02, 03 … 11, 12
 h1, 2, 3 … 11, 12
AM / PMAAM, PM
 aam, pm
Minutemm00, 01, 02 … 58, 59
 m0, 1, 2 … 58, 59
Secondss00, 01, 02 … 58, 59
 s0, 1, 2 … 58, 59
MicrosecondS000000 … 999999
TimezoneZZZAsia/Baku, Europe/Warsaw, GMT
 zz-07:00, -06:00 … +06:00, +07:00, +08, Z
 Z-0700, -0600 … +0600, +0700, +08, Z

Parsing

Please consider that you cannot put custom tokens or custom letters into the parsing string

Easily parse with Parse:

dateTime, err := gostradamus.Parse("10.02.2010 14:59:53", "DD.MM.YYYY HH:mm:ss")
println(dateTime.String())
// 2010-02-10T14:59:53.000000Z

You can also specify a timezone while parsing:

dateTime, err := gostradamus.ParseInTimezone("10.02.2010 14:59:53", "DD.MM.YYYY HH:mm:ss", gostradamus.EuropeBerlin)
println(dateTime.String())
// 2010-02-10T14:59:53.000000+0100

Formatting

Formatting is as easy as parsing:

dateTimeString := gostradamus.NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).Format("DD.MM.YYYY Time: HH:mm:ss")
println(dateTimeString)
// 14.07.2017 Time: 02:40:00

Floor

dateTimeString := gostradamus.NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).FloorDay()
println(dateTimeString.String())
// 2017-07-14T00:00:00.000000Z

dateTimeString := gostradamus.NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).FlorHour()
println(dateTimeString.String())
// 2017-07-14T02:00:00.000000Z

Ceil

dateTimeString := gostradamus.NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).CeilMonth()
println(dateTimeString.String())
// 2017-07-31T23:59:59.999999Z

dateTimeString := gostradamus.NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).CeilSecond()
println(dateTimeString.String())
// 2017-07-14T02:40:00.999999Z

Spans

Spans can help you to get quickly the current span of the month or the day:

start, end := NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).SpanMonth()
println(start.String())
// 2017-07-01T00:00:00.000000Z
println(end.String())
// 2017-07-31T23:59:59.999999Z

start, end = NewDateTime(2017, 7, 14, 2, 40, 0, 0, UTC).SpanDay()
println(start.String())
// 2017-07-14T00:00:00.000000Z
println(end.String())
// 2017-07-14T23:59:59.999999Z

start, end = NewDateTime(2012, 12, 12, 2, 40, 0, 0, UTC).SpanWeek()
println(start.String())
// 2012-12-10T00:00:00.000000Z
println(end.String())
// 2012-12-16T23:59:59.999999Z

Utils

Here is the section for some nice helper functions that will save you some time.

IsBetween

isBetween := gostradamus.NewUTCDateTime(2020, 1, 1, 12, 0, 0, 0).IsBetween(
    gostradamus.NewUTCDateTime(2020, 1, 1, 11, 0, 0, 0),
    gostradamus.NewUTCDateTime(2020, 1, 1, 13, 0, 0, 0), 
)
println(isBetween)
// true

isBetween = gostradamus.NewUTCDateTime(2020, 1, 1, 12, 0, 0, 0).IsBetween(
    gostradamus.NewUTCDateTime(2020, 1, 1, 13, 0, 0, 0),
    gostradamus.NewUTCDateTime(2020, 1, 1, 14, 0, 0, 0),
)
println(isBetween)
// false

IsoCalendar

Retrieve year, month, day directly as a 3-tuple:

year, month, day := gostradamus.NewUTCDateTime(2020, 1, 1, 12, 0, 0, 0).IsoCalendar()
println(year, month, day)
// 2020 1 1

Contribution

Do you have an idea to improve Gostradamus? -> Create an issue

Do you have already coded something for Gostradamus? -> Create a pull request.

Did you discover a bug? -> Create an issue

Otherwise feel free to contact me: michael@bykovski.de or visit my webpage: bykovski.de

Author: Bykof
Source Code: https://github.com/bykof/gostradamus 
License: MIT License

#go #golang #time #datetime 

A Flutter Plugin to Format DateTime

A Flutter plugin to convert and format DateTime object into get_time_ago format to get
String like 10 seconds ago, a minute ago, 7 hours ago, etc.

Installation

Add get_time_ago as a dependency in your pubspec.yaml file.

dependencies:
  get_time_ago: ^1.0.6

Usage

Format any DateTime object into get_time_ago format by following steps:

// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';

// Pass DateTime object as argument in the method
var _dateTime = DateTime.now().subtract(const Duration(minutes: 10)); // [DateTime] object
print(GetTimeAgo.parse(_dateTime)); // 10 minutes ago

// Formatting with locale
print(GetTimeAgo.parse(_dateTime, locale:'es')); // hace 10 minutos

Formatting String as get_time_ago

If you have saved a DateTime object as a String into a variable, database or cloud, then you have to first convert the String into DateTime object and then pass it as argument in parse method of get_time_ago plugin to format it into get_time_ago format by following steps:

// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';

var _timestamp = '2021-05-10 05:21:37.712498'; // [DateTime] formatted as String.
var _convertedTimestamp = DateTime.parse(_timestamp); // Converting into [DateTime] object
var result = GetTimeAgo.parse(_convertedTimestamp); 
print(result);

Setting default locale

If you want to change your default locale, then call setDefaultLocale method and pass the locale code as the argument.

// Import the plugin
import 'package:get_time_ago/get_time_ago.dart';

@override
  void initState() {
    super.initState();
    GetTimeAgo.setDefaultLocale('fr'); // Sets the default locale to French
  }

Migrating to 1.0.3+

Starting with version 1.0.3 of the get_time_ago plugin, the default class name has been changed from TimeAgo to GetTimeAgo and method getTimeAgo has been renamed to parse to make the method call easy and reduce the confusion around class name and method name. It is recommended to migrate from old version to new version for seamless and efficient performance.

Changes regarding the mew method call

Old APINew API
TimeAgo.getTimeAgo(...)GetTimeAgo.parse(...)

Screenshots

Screenshot_1Screenshot_2

Connect With Me

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add get_time_ago

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):


dependencies:
  get_time_ago: ^1.0.6

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:get_time_ago/get_time_ago.dart';

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:get_time_ago/get_time_ago.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(
        title: 'GetTimeAgo Example',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            visualDensity: VisualDensity.adaptivePlatformDensity,
            brightness: Brightness.light),
        darkTheme: ThemeData(
            visualDensity: VisualDensity.adaptivePlatformDensity,
            brightness: Brightness.dark),
        home: HomePage(),
      );
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var _dateTime = DateTime.now().subtract(const Duration(minutes: 10));
  final _sevenHoursAgo = DateTime.now().subtract(const Duration(hours: 7));
  final _timestamp = '2021-05-10 22:21:37.712498';

  Future<void> _selectDate() async {
    var pickedDate = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(1900),
        lastDate: DateTime.now());
    if (pickedDate != null) {
      setState(() {
        _dateTime = pickedDate;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    //GetTimeAgo.setDefaultLocale('hi');
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('GetTimeAgo Example'),
        ),
        body: Container(
          padding: const EdgeInsets.all(10.0),
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CustomContainer(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      const Text(
                        'Converting DateTime into GetTimeAgo with Portuguese Locale',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 18.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 10.0),
                      Text("DateTime.now().subtract(const Duration(hours: 7))"),
                      const SizedBox(height: 5.0),
                      Text(
                        GetTimeAgo.parse(_sevenHoursAgo, locale: 'pt'),
                        style: TextStyle(
                            color: Theme.of(context).accentColor,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 20.0),
                CustomContainer(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      const Text(
                        'Converting String into GetTimeAgo with custom DateTime pattern',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 18.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 10.0),
                      Text(_timestamp),
                      const SizedBox(height: 5.0),
                      Text(
                        GetTimeAgo.parse(
                          DateTime.parse(_timestamp),
                          pattern: "dd-MM-yyyy hh:mm aa",
                        ),
                        style: TextStyle(
                            color: Theme.of(context).accentColor,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 20.0),
                CustomContainer(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      const Text(
                        'Converting Input into GetTimeAgo',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            fontSize: 18.0, fontWeight: FontWeight.bold),
                      ),
                      const SizedBox(height: 5.0),
                      ElevatedButton.icon(
                        onPressed: _selectDate,
                        icon: const Icon(Icons.calendar_today),
                        label: const Text('Select DateTime'),
                      ),
                      const SizedBox(height: 10.0),
                      Text(_dateTime.toString()),
                      const SizedBox(height: 5.0),
                      Text(
                        GetTimeAgo.parse(_dateTime),
                        style: TextStyle(
                          color: Theme.of(context).accentColor,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      );
}

class CustomContainer extends StatelessWidget {
  final Widget child;

  const CustomContainer({required this.child});

  @override
  Widget build(BuildContext context) => Container(
        width: double.infinity,
        padding: const EdgeInsets.all(16.0),
        decoration: BoxDecoration(
            color: Colors.black.withOpacity(0.15),
            borderRadius: const BorderRadius.all(Radius.circular(16.0))),
        child: child,
      );
} 

Download Details:

Author: nixrajput

Source Code: https://github.com/nixrajput/get-time-ago

#flutter #datetime